C Programming and Data Structures May/June 2007 SET- 4 1. Write about space requirements for variables of different data types. Sl no 1 2 3 4 5 6 7 8 9 10 11
Type Char or signed char Unsigned char Int or signed int Unsigned int Short int or signed short int Unsigned short int Long int or signed long int Unsigned long int Float Double Long double
Size (bytes)
Range
1 1 2 2 1 1 4 4 4 8 10
128 to 127 0 to 25 32769 to 32767 0 to 65535 128 to 127 0 to 255 2147483648 to 2147483647 0 to 4294967295 3.4E 38 to 3.4E38 1.7E 308 to 1.7E+308 3.4E 4932 to 1.1E+432
2. (a) Distinguish between the following: (i) Actual and formal arguments Actual Parameters
Formal Parameters
Actual parameters are the arguments passed when the function is called.
Formal parameters are the arguments specified during function definition.
Eg: sum(x,y); x,y here are called actual parameters.
Eg: sum(int a,int b) { .. } Here a, b are called formal parameters.
(ii) Global and local variables Global Variables
Local Variables
The variables that are declared outside the function are called global
The variables that are declared inside a function definition are called local variables. variables.
Eg: int a,b,c;
Eg: main() (Contd.)
Appendix-C.p65
33
7/29/08, 5:22 PM
C.34 Solved Question Papers Global Variables
Local Variables
main() { .. } Here a, b, c are global variables.
{ int x,y; .. } Here x, y are called local variables.
A global variable s scope and lifetime is throughout the program
A local variable s scope and lifetime is confined to the function in which it is declared.
(iii) Automatic and static variables Automatic Variables
Static Variables
The default storage class for local
Static variables are to be declared explicitly. variables is auto.
Eg: main() { auto int a,b,c; .. } Here a, b, c are global variables.
Eg: main() { static int x,y; .. } Here x, y are called local variables.
Keyword used is auto Default value of automatic variables is a garbage value.
Keyword used is static Default value of static variables is a zero.
Automatic variables can be initialized any number of times.
Static variables can be initialized only once.
(b) Explain in detail about pass by values and pass by reference. Explain with a sample program. Pass by value: In pass by value the actual values are passed as arguments to the called function. These actual values are copied into the arguments of the called function. The changes that are made in the function do not affect the actual values. #include<stdio.h> main() { int a=10,b=20; clrscr(); printf( The values of a and b before swapping are %d and %d\n ,a,b); swap(a,b); printf( The values of a and b after swapping are %d and %d\n ,a,b); getch(); } swap(int x, int y) {
Appendix-C.p65
34
7/29/08, 5:22 PM
Solved Question Papers
C.35
int temp; temp=x; x=y; y=temp; } Output: The values of a and b before swapping are 10 and 20. The values of a and b after swapping are 10 and 20.
Pass by reference: In call by reference the address of the actual parameters are passed as arguments to the called function. The formal parameters are declared as pointers. The changes that are made to the formal parameters affect the actual parameters. #include<stdio.h> main() { int a=10,b=20; clrscr(); printf( The values of a and b before swapping are %d and %d\n ,a,b); swap(&a,&b); printf( The values of a and b after swapping are %d and %d\n ,a,b); getch(); } swap(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } Output: The values of a and b before swapping are 10 and 20. The values of a and b after swapping are 20 and 10.
3. (a) Write a C program using pointer for string comparison. #include<stdio.h> #include<string.h> main() { char str1[20],str2[20]; char *ptr1=str1,*ptr2=str2; int res=0; clrscr(); printf( enter string one\n ); gets(str1); printf( enter string two\n );
Appendix-C.p65
35
7/29/08, 5:22 PM
C.36 Solved Question Papers gets(str2); while(*ptr1!= \0 ) { if(*ptr1==*ptr2) { ptr1++; ptr2++; } else { res=1; break; } } if(res!=0) printf( strings are different\n ); else printf( strings are identical\n ); getch(); }
(b) Write a C program to arrange the given numbers in ascending order using pointers. #include<stdio.h> main() { int a[20],i,n; clrscr(); printf( enter the value of n\n ); scanf( %d ,&n); printf( enter the elements into array\n ); for(i=0;i<n;i++) scanf( %d ,a+i); sort(a,n); printf( elements after sorting are \n ); for(i=0;i<n;i++); printf( %d\t ,*(a+i)); getch(); } sort(x,n) int *x,n; { int i,j,temp; for(i=0;i<n;i++) { for(j=0;j<n;j++) if(*(x+j)>*(x+j+1)) {
Appendix-C.p65
36
7/29/08, 5:22 PM
Solved Question Papers
} }
C.37
temp=*(x+j); *(x+j)=*(x+j+1); *(x+j+1)=temp; }
4. (a) Describe nested structures. Draw diagrams to explain a nested structure. A nested structure means a structure within a structure. This can be done in two ways. 路 A structure itself can be a member of another structure. 路 A structure object can be a member of another structure. The syntax for a nested structure is as follows: struct tagname1 struct tagname1 { { datatype member1; datatype member1; datatype member2; datatype member2; datatype member3; . . . struct tagname2 struct tagname2 x; { }; datatype member4; struct tagname2 datatype member5; { . datatype memberN1; . datatype memberN2; }a; . }b; }; The inner member4 is accessed as b.a.member4. The syntax for accessing the innermost members is tagname1object. tagname name2 object. member.
Diagram for nested structures
Appendix-C.p65
37
7/29/08, 5:22 PM
C.38 Solved Question Papers (b) Write a program to declare pointers as members of a structure and display the contents of the structure. Define a structure object, boy, with three fields: name, age and height. #include<stdio.h> struct boy { char *name; int *age; float *height; }*sp; main() { char name[10]= latha ; int age=18; float height=167.64; sp->name=name; sp->age=&age; sp->height=&height; clrscr(); printf( \n NAME=%s ,sp->name); printf( \nAGE=%d\nHEIGHT=%f ,*sp->age,*sp->height); getch(); }
5.
Appendix-C.p65
(a) What is the task performed by fseek() function? What is its syntax? Explain each parameter in it. The fseek function is used to move the file pointer to the desired position in the file. This means random access to files is made possible by the fseek function. This function has three arguments in its syntax. The syntax of this function is fseek (filepointer, offset, position); · The first argument is the file type pointer which points to the file that we are currently operating with. · The second argument offset specifies the number of bytes the file pointer has to move. The file pointer can move either forward or backward, depending on the value of offset. If the offset value is positive it moves forward and if it is negative it moves backward. The value of the offset is of long type. · The third argument position takes one of the three integer values. These values have specific meaning. Value Meaning 0 Beginning of the file 1 Current position 2 End of file Examples: fseek(fp,m,0) -fp moves m bytes from the starting of the file and points to (m+1)th byte. fseek(fp,-m,1) -from the current position move m bytes in backward direction.
38
7/29/08, 5:22 PM
Solved Question Papers
C.39
fseek(fp,0,0) -go to the beginning of the file. fseek(fp,-m.2) -move backward by m bytes from the end of the file. Offset cannot take negative values when the position value is 0 and the file pointer cannot take positive values when the position value is 2, as the file pointer cannot go beyond the end of file. (b) Write a C program to read the text file containing some paragraph. Use fseek() and read the text after skipping n characters from the beginning of the file. #include<stdio.h> main() { int n; FILE *fptr; char ch; clrscr(); fptr=fopen( input.txt , r ); printf( enter the value of n\n ); scanf( %d ,&n); printf( the contents of the file are:\n); while((ch=getc(fptr))!=EOF) printf( %c ,ch); fseek(fptr,0,0); printf( the contents of the file after skipping n Characters are:\n); fseek(fptr,n,0); while((ch=getc(fptr))!=EOF) printf( %c ,ch); fclose(fptr); getch(); }
6. Write a non-recursive simulation of Towers of Hanoi problem. #include <stdio.h> #include <stdlib.h> #define PR #define PE
(void)printf( (void)fprintf(stderr,
#define ALLO(x) { if((x = (int *)malloc((n+3) * sizeof(int))) == NULL) {\ PE #x allocation failed!\n ); exit(1); }} main(int argc, char *argv[]) /* ========================*/ { int i, *a, *b, *c, *p, *fr, *to, *sp, n, n1, n2;
Appendix-C.p65
39
7/29/08, 5:22 PM
C.40 Solved Question Papers n = atoi(argv[1]); n1 = n+1; n2 = n+2; ALLO(a) ALLO(b) ALLO(c) a[0] = 1; b[0] = c[0] = n1; a[n1] = b[n1] = c[n1] = n1; a[n2] = 1; b[n2] = 2; c[n2] = 3; for(i=1; i<n1; i++) { a[i] = i; b[i] = c[i] = 0; } fr = a; if(n&1) else
}
{ to = c; sp = b; } { to = b; sp = c; }
while(c[0]>1) { PR move disc %d from %d to %d\n , i=fr[fr[0]++], fr[n2], to[n2]); p=sp; if ( ( to[ to[0]] = i)&1) { sp=to; if(fr[fr[0]] > p[p[0]]) { to=fr; fr=p; } else to=p; } else { sp=fr; fr=p; } }
7. Write a routine to reverse elements of a doubly linked list by traversing the list only once. struct DLL { struct DLL *pre; int data; struct DLL *next; }*first,*ptr,*ptr1,*fresh;
Reverse() { if(first==NULL) { printf( the list is empty so no need to reverse\n ); return; } ptr=first; ptr->pre=ptr->next; ptr->next=NULL; ptr1=ptr->pre;
Appendix-C.p65
40
7/29/08, 5:22 PM
Solved Question Papers
}
8.
C.41
while(ptr1->next!=NULL) { ptr1->pre=ptr1->next; ptr1->next=ptr; ptr1=ptr1->pre; ptr=ptr->pre; } ptr1->next=ptr; ptr1->pre=NULL; first=ptr1;
(a) Explain Quick sort with algorithm. This method is also called partition exchange sort. This method is based on divide-andconquer technique, i.e., the entire list is divided into various partitions and sorting is again and again applied on the partitions. In this method, the list is divided into two, based on an element called the pivot element. Usually, the first element is considered to be the pivot element. Now, move the pivot element into its correct position in the list. The elements to the left of the pivot are less than the pivot while the elements to the right of the pivot are greater than the pivot. The process is repeated in each of these partitions. This process proceeds till we get the sorted list of elements. Let us understand this by an example. Consider the list 74, 39, 35, 32, 97, 84. 1. We will take 74 as the pivot and move it to a position so that the new list becomes 39, 35, 32, 74, 97, 84. 2. Now take the partitioned list 39, 35, 32. Let us take 39 as the pivot. Moving it to the correct position gives 35, 32, 39. Reapplying the process to the left partition 35, 32, we get 32, 35. 3. Apply the process to the right partition of 74. Taking 97 as the pivot and positioning it, we get 84, 97. 4. Assembling all the elements from each partition, we get the sorted list.
ALGORITHM FOR QUICK SORT 1. Start. 2. Select the first element of the array as pivot. 3. Position the pivot such that the elements to the left of the pivot are less then the pivot and the elements to the right of the pivot are greater than the pivot. 4. Consider the left and right partition and repeat the steps 2 and 3. 5. Merge all the partitions to get the sorted list of elements. 6. Stop. (b) Analyze the worst case performance of Quick sort and compare with Selection sort. The selection of the pivot plays a vital role in determining the efficiency of the quick sort. The main reasons for this are the pivot may partition the list into two so that one partition is much bigger than the other and the partition may be in an unsorted manner.
Appendix-C.p65
41
7/29/08, 5:22 PM
C.42 Solved Question Papers We will assume that the pivot partitions the list into two so that one of the partitions has no elements while the other has all the other elements. This is the worst case possible. Here, the total number of comparisons at the end of the sort would have been: (n 1)+ (n 2) + (n 3) + +2+1=1/2(n 1)*n = ½(n2) 1/2(n). This is equal to O (n2). Thus the efficiency of the quick sort in its worst case is O (n2). Efficiency of selection sort You can easily understand from the algorithm that the first pass of the program does (n 1) comparisons. The next pass does (n 2) comparisons, and so on. Thus, the total number of comparisons at the end of the sort would be: (n 1)+ (n 2) + (n 3) + +2+1=1/2(n 1)*n = ½(n2) 1/2(n). This is equal to O (n2). Thus the efficiency of the selection sort is O (n2).
Appendix-C.p65
42
7/29/08, 5:22 PM