Insertion sort example in C

/* */ # define n 6 int x[]={4, 3, 6, 2, 8, 9}; main( ) { int I, t, j,. printf(“\n\n Input data: \n \n”); for(I=0; I<n; I++) printf(*%3d*, x[I]); printf(*\n*); for I=1; I<n; I++) for(j=1, j>0 && x[j]<x[j-1]; j — —) { t=x[j]; x[j]=x[j-1]; x[j-1]=t; } printf(“\n\n Output data: \n \n”); for(I=0; I<6; I++) […]
Continue reading…

Enjoyed this post? Share it!

 
 

Calculate square of a number in C

#include <iostream> using namespace std;   int main() { double n; cout << "Input a number and press ENTER: "; cin >> n; cout << "The square is: " << n * n << endl; system("PAUSE"); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Advantages of using object properties in c++

Data encapsulation, data hiding: Prevent direct manipulation of data elements. Avoid programming errors related to changing a data value incorrectly or at the wrong time.Example: Protecting list integrity in a multi-threaded program; e.g., transparently apply a lock or a critical section around code that manipulates the list. Data validation, data value enforcement: It’s a way […]
Continue reading…

Enjoyed this post? Share it!

 
 

C Program That Numbers the Lines of a Text File

II Create a listing of a file in which each line is numbered, and each // page has a heading showing the file name and the page number. #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstdlib> using namespace std; const int LINES_PER_PAGE = 50; void headerf ofstream&, const string&, int ); int main() […]
Continue reading…

Enjoyed this post? Share it!

 
 

Write a program to demonstrate selection sort in C

#include<stdio.h> #include<stdlib.h> void selectsort(int sort[],int len); void main() { int len,i=0; int sort[30]; printf(“n enter no<0 to stop>”); scanf(“%d”,&sort[i]); while(sort[i]!=0) {i++; printf(“n enter no<0 to stop>”); scanf(“%d”,&sort[i]); } len=i; //length is taken as i and not as i+1 as 0 is also stored in the array selectsort(sort,len); //call the function selectsort which will sort the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Write a routine to delete element at n th position in a doubly linked List

struct doublellist { int data; struct doublellist *left; struct doublellist *right; }; typedef struct doublellist node; // deleting the node from a doubly linked list node *delet(int n,node *list) { node *temp=list; while(temp->data!=n && temp->left!=NULL) temp=temp->left; //find location oh n in the list if(temp->data!=n) { printf(“node does not exist in listn”); exit(1); } else { […]
Continue reading…

Enjoyed this post? Share it!

 
 

Write a routine to insert element at nth position in doubly linked List

struct doublellist { int data; struct doublellist *left; struct doublellist *right; }; typedef struct doublellist node; node *insert(int n,int val,node *list) { node *p,*temp; temp=list; p=(node *)malloc(sizeof(node)); p->data=val; if(n==0) { p->right=NULL; p->left=list; list->right=p; list=p; } else { while(temp->data!=n && temp->left!=NULL) temp=temp->left; //find location oh n in the list if(temp->data!=n) { printf(“node does not exist in […]
Continue reading…

Enjoyed this post? Share it!

 
 

Write a ‘C’ program to using pointers to determine the length of a character string

#include<stdio.h> //function prototype declarations int length(char x[]); void main() { int ans; char x[20]; // dimension of string array x printf(“enter a string:”); scanf(“%s”,x); /*input string from the user*/ ans =length(x); /*function call*/ printf(“length=%dn”,ans); getch(); }/*end of main*/ int length(char a[]) /*function definition*/ { int i=0; while(a[i]!=’’) /*when the character is not null*/ i++; return […]
Continue reading…

Enjoyed this post? Share it!

 
 

Write a ‘C’ program to compute the sum of all elements stored in an array using pointers

The usage of pointers in arrays gives us the advantage of Dynamic memory Management where the size of the array is decided by the program and memory can also be deallocated. Here is a program to calculate the sum of numbers in an array using pointers. #include<stdio.h> #include<malloc.h> main() { int *a,i,n,result=0; printf(“Enter the no. […]
Continue reading…

Enjoyed this post? Share it!

 
 

What is a union in C

Unions are useful when memory conservation is the criteria. Union , like structure holds data types like int , char , float etc. However , the major difference is that union holds only one data object at a time. Union calculates which of its declarations require maximum storage requirements and allocates memory space accordingly. It […]
Continue reading…

Enjoyed this post? Share it!

 
 

What are structures in C

A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that can be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members also commonly referred […]
Continue reading…

Enjoyed this post? Share it!

 
 

Describe two different ways that floating – point constants can be written in C. What special rules apply in this case?

Integer numbers are inadequate to represent quantities that continuously, such as distance, heights, temperatures, prices, and so on. These quantities are represented by numbers containing fractional parts like 17.548. Such numbers are called real or floating point constants. They are base– 10 number that can be represented either in exponent form or decimal point representation. […]
Continue reading…

Enjoyed this post? Share it!

 
 

What are different types of integer constants?

What are different types of integer constants? What are long integer constants? How do these constants differ from ordinary integer constants? How can they be written and identified? Integer Constants : An integer constant refers to a sequence of digits. There are three types of integer namely, decimal, octal and hexadecimal.They can be sub divided […]
Continue reading…

Enjoyed this post? Share it!