1. Differentiate between primitive data type and non-primitive data type. Primitive data types are the basic data types. These data types are used to represent single values. For example: Character, Integer, Float, Double etc. Data types that are derived from basic data types are known as non-primitive data types. These data types are used to store group of data. For example: Array, Structure, Union, Queue, Stack etc. 2. What is a pointer? Explain how the pointer variable is declared and initialized. A pointer is a variable that contains the address of a variable. Syntax of declaration of pointer: datatype *pointername; For example: int *ip; /* data type is integer and pointer name is ip.*/ char *cp; /* data type is character and pointer name is cp.*/ Initialization of pointer: Initialization is the assigning of address of a variable to a pointer variable. For example: int a=5; int *ip; ip=&a; where & is the unary operator gives the address of an object. 3. Explain preprocessor directives in C with example. The #define is a macro preprocessor directive. Syntax of #define directive: #define identifier token-string OR #define identifier (identifier-list) token-string The #define directive causes compiler to substitute the token-string for each occurrence of the identifier in the source file. The token-string may consist of a series of token such as constant, keyword or complete sentence. Example: #define PI 3.14 #undef is a macro preprocessor directive. Syntax of #undef directive: #undef identifier The #undef directive removes the current definition of the identifier and subsequent occurrence of the identifier is ignored by the processor. Page: 1
Example: #define AGE 40....... #undef AGE The #include directive loads a specific file to the program. Syntax of #include directive: (a) #include <filename> It causes the replacement of the line by the entire content of file filename. The search for the file is made in the standard directories. (b) #include filename It causes the replacement of the line by the entire content of file filename. The search for the file is made in the current directory and standard directories. For example: #include <stdio.h> Conditional Compilation: Parts of the program may be compiled conditionally. The compiler compiles selected portion of the program based on the condition. #ifdef and # are conditional compilation directive. Syntax: #ifdef <identifier> statment1; statement2; # statment3; statement4; #endif If the identifier is defined, then #if block is compiled. If the identifier is not defined, then # block is compiled. For example: #include <stdio.h> #define PI 3.14 float radius,area; printf( \nenter the radius of the circle: ); scanf( %f,&radius); #ifdef PI Page: 2
area=pi*radius*radius; # area=3.14*radius*radius; #endif printf( \n Area of the circle:%0.2f,area); If the PI is defined, then #if block is compiled. If the PI is not defined, then # block is compiled. 4. Write a C program to swap two numbers using call by pointers (address) method. void swap(int *m, int *n); int a=2,b=3; printf( \n Before the function call: a=%d and b=%d\n,a,b); swap(&a,&b); printf( \n After the function call: a=%d and b=%d\n,a,b); void swap(int *m, int *n) int *r; *r=*m; *m=*n; *n=*r; 5. Explain how pointers and arrays are related with examples. There is a strong relationship between pointers and arrays. An operation that can be performed by arrays can also achieved by pointers. Consider the declaration of the array below: int a[5]; defines an array of size 5. A block of consecutive elements named a[0],a[1],a[2],a[3],a[4]. a[0] a[1] a[2] a[3] a[4] The notation a[i] refers to the i th element of the array. Now, let us declare a pointer as below: int *ptr; Then, the assignment ptr=&a[0]; Page: 3
sets ptr to point to the element a[0] of the array. ptr contains the address of a[0]. a[i] can be written as *(ptr+i). ptr ptr+1 ptr+2 ptr+3 ptr+4 a[0] a[1] a[2] a[3] a[4] Further, ptr=&a[0] can also be written as ptr=a ; and (ptr+i) can be written as (a+i). Hence, a[i] can be written as *(ptr+i) and a[i] can also be written as *(a+i). 6. Write C programs to define macros for (i) arithmetic operator(ii)logical operators. (i) #define multiply(a,b) a*b int x,y,res; printf( \n Enter two integers: ); scanf( %d %d, &x, &y); res=multiply(x,y); printf( \n Result=%d,res); (ii) #define LOGIC_AND (a,b) a&&b int x,y,res; printf( \n Enter two boolean(0 or 1) ); scanf( %d %d, &x, &y); res=logic_and(x,y); printf( \n Result=%d,res); 7. Write a C program using pointers to compute the Sum, Mean and Standard deviation of n elements stored in an array of n real numbers. #include<math.h> Page: 4
float sum=0, mean=0, std=0, s=0; float arr[100], *p; int i,n; printf( \n Enter the number of elements: ); scanf( %d,&n); for(i=0; i<n; i++) scanf( %f, arr+i); p=arr; for(i=0; i<n; i++) sum=sum+*p; p=p+1; mean= sum/n; p=arr; for(i=0; i<n; i++) s=s + (*p-mean)*(*p-mean); p=p+1; std=sqrt(s/n); printf( \n sum=%0.2f,sum); printf( \n mean=%0.2f,mean); printf( \n standard deviation=%0.2f,std); 8. Explain the array of pointers with example. Since pointers are variables, they can be stored in arrays similar to any other variables. For example: #include <stdio.h> int i; int arr[5]=5,6,7,8,9; int *ptr[5]; for(i=0; i<5; i++) ptr[i]=&arr[i]; for(i=0; i<5; i++) printf( \n arr[%d]=%d, i,*ptr[i]); Page: 5
9. What is dynamic memory allocation? What is the need for dynamic memory allocation? Dynamic memory allocation enables the programmer to allocate memory at runtime. This allows the programmer to obtain more memory when required and to release the memory when it is not required. Dynamic memory allocation prevents the wastage of memory. A lot of memory is wasted in static memory allocation because all the memory allocated may not be utilized. 10. Explain the functions for dynamic memory allocation in C. Following are the dynamic memory allocation functions: malloc():this function is used to allocate a block of memory in bytes. Syntax for this function is: ptr=malloc(number of elements*size of the elements); Type of pointer returns is void. Therefore, typecast is required. For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)malloc( 5*sizeof(int)); printf( \n Enter 5 integers: ); for(i=0; i<5; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<5; i++) printf( %d,*(ptr+i)); free(ptr); calloc():this function allocate a block of memory for an array. It initializes all bits to 0. Syntax: ptr=calloc(number of elements, size of each element); For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)calloc( 5,sizeof(int)); Page: 6
printf( \n Enter 5 integers: ); for(i=0; i<5; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<5; i++) printf( %d,*(ptr+i)); free(ptr); realloc(): This function is used to resize the memory which is already allocated. Syntax: ptr=realloc(ptr, new size); For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)malloc( 5*sizeof(int)); ptr=(int *)realloc( ptr,6*sizeof(int)); printf( \n Enter 6 integers: ); for(i=0; i<6; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<6; i++) printf( %d,*(ptr+i)); free(ptr); free(): This function is used to de-allocate previously allocated memory. Syntax: Free(ptr); Example: Any of the above example for malloc(), calloc() or realloc(); 11. Explain Stack and Queue data structures along with their applications. Stack is a linear data structure. We can insert or delete an element from one end. We may perform LIFO (Last In First Out) or FILO (First In Last out). The insertion operation of an element onto the stack is called as push and deletion operation is called pop. The end where the insertion or deletion takes place is called top. Page: 7
4 Top of stack 3 2 1 Application of stack: (i) Expression evaluation. (ii)expression conversion, for example, Infix to postfix conversion (iii) Redo-Undo operation (iv) Forward and backward features of web browsers. (v) Used in algorithms like tree traversals, span problem etc (vi) Applications like Backtracking, Queen problem etc. (vii) Parsing (viii)simulation of recursion. Queue is a linear data structure. It is a homogeneous collection of elements in which elements are appended in one end called rear end and elements are deleted at other end called front end. Queue follows FIFO (First In First Out). Application of Queue: (i) Queue is used in operating systems maintain a queue of processes that are waiting for execution. (ii) Queue is used where data is transferred asynchronously between two processes. For example, buffer. (iii) Queue is used for sharing resource among different consumers. (ii) Breadth First Search uses queue. (iii) Queue is performs jobs in a network printer. 12. Write a C program using array to demonstrate the operations on stack. #define MAX 100 void push(); void pop(); void disp(); int stack[max]; int top=-1; int choice; clrscr(); while(1) Page: 8
printf( \n 1.Insert element to the stack ); printf( \n 1.Delete element from the stack ); Printf( \n 1.Display elements of the stack ); printf( \n 4.Quit ); printf( \n Enter your choice: ); scanf( %d,&choice); switch(choice) case 1: push(); case 2: pop(); case 3: disp(); case 4: exit(0); default: printf( \n Wrong choice ); void push() int item; if (top=max-1) printf( \n Stack is full ); printf( \n Enter the element to be inserted: ); scanf( %d,&item); top=top+1; stack[top]=item; void pop() if(top==-1) printf( \n Stack is empty ); Page: 9
printf( \n Element deleted from the stack is :%d, stack[top]); top=top-1; void disp() int i; if(top==-1) printf( \n Stack is empty. ); printf( \n Stack is: ); for(i=top; i>=0; i--) printf( %d, stack[i]); print( \n ); 13. Write a C program using link list to demonstrate the operations on stack. #include <stdio.h> #include<stdlib.h> struct node int info; struct node *next; ; typedef struct node NODEPTR; NODEPTR top,temp; void push(); void pop(); void disp(); int choice; clrscr(); while(1) printf( \n 1.Insert element to the stack ); printf( \n 2.Delete element from the stack ); printf( \n 3.Display elements of the stack ); Page: 10
printf( \n 4.Quit ); printf( \nenter your choice: ); scanf( %d,&choice); switch(choice) case 1; push(); case 2; pop(); case 3; disp(); case 4; exit(0); default: printf( \n Wrong choice ); void push() int item; printf( \nenter the element to be inserted: ); scanf( %d,&item); if(top==null) top=(nodeptr)malloc(sizeof(struct node)); top->next=null; top->info=item; temp=(nodeptr)malloc(sizeof(struct node)); temp->next=top; temp->info=item; top=temp; Page: 11
void pop() if(top==null) printf( \nerror:stack is empty ); if(top->next!=null) temp=top->next; printf( \nelement deleted from the stack is:%d, top->info); free(top); top=temp; printf( \nelement deleted from the stack is:%d, top->info); free(top); top=null; void disp() temp=top; if(top==null) printf( \nstack is empty. ); printf( Stack is: ); while(temp!=null) printf( %d,temp->info); temp=temp->next; printf( \n ); 14. Write a C program using array to demonstrate the operations on queue. #define MAX 100 void insert(); void del(); Page: 12
void disp(); int queue[max]; int rear=-1; int front=-1; int choice; clrscr(); while(1) printf( \n 1.Insert element to the queue ); printf( \n 1.Delete element from the queue ); Printf( \n 1.Display elements of the queue ); printf( \n 4.Quit ); printf( \n Enter your choice: ); scanf( %d,&choice); switch(choice) case 1: insert(); case 2: del(); case 3: disp(); case 4: exit(0); default: printf( \n Wrong choice ); void insert() int item; if (rear=max-1) printf( \n Queue overflows); Page: 13
if(front==-1) front=0; printf( \n Enter the element to be inserted: ); scanf( %d,&item); rear=rear+1; queue[rear]=item; void del() if(front==-1 front>rear) printf( \n Queue is empty ); printf( \n Element deleted from the queue is :%d, queue[front]); front=front+1; void disp() int i; if(front==-1) printf( \n Queue is empty. ); printf( \n Queue is: ); for(i=front; i<=rear; i++) printf( %d, queue[i]); print( \n ); 15. Write a C program using link list to demonstrate the operations on queue. #include <stdio.h> #include<stdlib.h> Page: 14
struct node int info; struct node *next; ; typedef struct node NODEPTR; NODEPTR front,rear,temp; void insert(); void del(); void disp(); int choice; clrscr(); while(1) printf( \n 1.Insert element to the queue ); printf( \n 2.Delete element from the queue ); printf( \n 3.Display elements of the queue ); printf( \n 4.Quit ); printf( \nenter your choice: ); scanf( %d,&choice); switch(choice) case 1; insert(); case 2; del(); case 3; disp(); case 4; exit(0); default: printf( \n Wrong choice ); Page: 15
void insert() int item; printf( \nenter the element to be inserted: ); scanf( %d,&item); if(rear==null) rear=(nodeptr)malloc(sizeof(struct node)); rear->next=null; rear->info=item; front=rear; temp=(nodeptr)malloc(sizeof(struct node)); rear->next=temp; temp->info=item; temp->next=null; rear=temp; void del() if(front==null) printf( \nerror:queue is empty ); if(front->next!=null) temp=front->next; printf( \n Element deleted from the queue is:%d, front->info); free(front); front=temp; printf( \n Element deleted from the queue is:%d, front->info); free(front); front=null; rear=null; Page: 16
void disp() temp=front; if(front==null) printf( \nqueue is empty. ); printf( Queue is: ); while(temp!=rear->next) printf( %d,temp->info); temp=temp->next; printf( \n ); 16. What is tree data structure? State the application of tree data structure. A tree is hierarchical data structure. It is a set of entities called nodes. Nodes are connected by edges. Each node contains data. First node of the tree is called the root. Applications of tree data structure are: (i) Manipulation of data which are arranged in hierarchical manner. (ii)search for data easily. (iii)prepare sorted list of data easily. (iv)router algorithm (v) Operating system maintains disk file system as a tree. (vi) Trees are used in language processing programs. Page: 17