Solved Question Paper 3

Page 1

C Programming and Data Structures May/June 2007 SET- 3 1.

Appendix-C.p65

(a) Explain the following and illustrate it with an example each. (i) Increment and Decrement operator The increment operator is represented by ++ and decrement operator by . Increment operator is to add 1 to the value of the variable. For example, ++a is equivalent to a=a+1. Increment operator is of two types. They are Pre increment ++a Post increment a++. There is no difference between the two when they are used individually. But when they are used in an expression they have different meanings. Consider the following example. Let the value of A be 5. A=5; B=++A; in this statement both the values of A and B are 6. B=A++; in this statement the value of B is 5 and A is 6. In pre-increment the value of the variable is incremented and then it is assigned to B. In post-increment the value of A is assigned to B and then the value of A is incremented. Decrement operator is to subtract 1 from the value of the variable. For example, a is equivalent to a=a 1. Decrement operator is of two types. They are Pre-decrement a Post-decrement a . There is no difference between the two when they are used individually. But when they are used in an expression they have different meanings. Consider the following example. Let the value of A be 5. A=5; B= A; in this statement both the values of A and B are 4. B=A ; in this statement the value of B is 5 and A is 4. In pre-decrement the value of the variable is decremented and then it is assigned to B. In post-decrement the value of A is assigned to B and then the value of A is decremented. (ii) Conditional operator A ternary operator pair ?: is available in C to construct conditional expressions of the form: exp1?exp2:exp3; Here, exp1 is evaluated first. If it is nonzero (or true), then exp2 is evaluated and this becomes the result of the expression. However, if exp1 is false, exp3 is evaluated and it becomes the value of the expression. Consider the given example:

22

7/29/08, 5:22 PM


Solved Question Papers

C.23

A=10; B=15; C=(A>B)?A:B; In this, A>B is evaluated. If it is true, C is assigned the value of A, otherwise it is assigned the value of B. (iii) Bitwise operator These are special types of operators used for manipulation of data at bit level. These operators are used for testing the bits, or shifting them left or right. They can be applied only on integers. The various bitwise operators are: Operator

Meaning

& | ! ^ << >> ~

Bitwise AND Bitwise OR Bitwise NOT Exclusive OR Left shift Right shift OneÂ’s complement

(iv) Assignment operator They are used to assign the result of an expression to a variable. The assignment operator used is “=”. The statement a=20, means the value 20 is stored in a. the syntax for this operator is Variable=expression; Another form of assignment operator is Variable op= expression; op stands for operator. Example: A+=1; is same as A= A+1; In the same way we have /=,*=,-=,%=. (b) State the rules that are applied while evaluating an expression in automatic type conversion. (I) If one of the operands is long double, the other will be converted to long double and the result will be long double (II) else if one of the operands is double, the other will be converted to double and the result will be double (III) else if one of the operands is float, the other will be converted to float and the result will be float (IV) else if one of the operands is unsigned long int, the other will be converted to unsigned long int and the result will be unsigned long int (V) else if one of the operands is long int, the other will be converted to long int and the result will be long int (VI) else if one of the operands is unsigned int, the other will be converted to unsigned int and the result will be unsigned int

Appendix-C.p65

23

7/29/08, 5:22 PM


C.24 Solved Question Papers 2.

(VII) All short and char are automatically converted to int. (a) What do you mean by functions? Give the structure of the functions and explain about the arguments and their return values. A C program has always a main module (called main) where the execution of the program begins. If a program is very big and if we write all the statements of that program in the main itself then it may become complex and large. As a result, the task of debugging and maintaining may become difficult. On the other hand, if we split the program into several functional portions, these are easier. These subprograms are called functions. The structure of the function is function_name(argument list) declaration of argument list; { local variable declaration; executable statements; return value; }

Argument list The arguments are valid variable names separated by commas. The argument variables receive values from a calling function. Thus, they provide us the link between the main program and the function. Return values A function may or may not send a value back to the calling function. This value which is sent to the calling function is the return value of the function. It is achieved through the return keyword. There can be only one value in a return statement. (b) Write a C program that uses function to sort an array of integers. #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++) {

Appendix-C.p65

24

7/29/08, 5:22 PM


Solved Question Papers

} }

3.

C.25

for(j=0;j<n;j++) if(*(x+j)>*(x+j+1)) { temp=*(x+j); *(x+j)=*(x+j+1); *(x+j+1)=temp; }

(a) Explain the process of accessing a variable through its pointer. Give an example. The variable can be accessed by a pointer by using the operators & and *. & gives the address of the variable where it is stored and * gives the value present at that address. & is called address of operator. * is called value at that address operator. Every variable will have three components. int a=10,b; a Name 10 Value 100 Address The address of the variable a can be assigned to any pointer as shown below. int *ptr; ptr=&a; /* this statement assigns the address of a to ptr */ b=*ptr;/* this statement assigns the value at the address contained in ptr to b */ Example program: #include<stdio.h> main() { int x,y,*p; x=20; p=&x; y=*p; printf( %d is the value stored at address %u\n ,x,&x); printf( %d is the value stored at address %u\n ,*p,p); printf( %d is the value stored at address %u\n ,y,&y); getch(); }

(b) Write a C program using pointers to read in an array of integers and elements in reverse order. #include<stdio.h> main() { int a[20,n,I; clrscr(); printf( enter the value of n\n );

Appendix-C.p65

25

7/29/08, 5:22 PM

print its


C.26 Solved Question Papers scanf( %d ,&n); /* reading values into the array */ printf( enter the elements into array\n ); for(i=0;i<n;i++) scanf( %d ,a+i); /* printing the elements in reverse order */ printf( elements in reverse order are\n ); for(i=n 1;i>=0;i ) printf( %d\t ,*(a+i)); getch(); }

4. (a) Write a C program to illustrate the comparison of structure variables. #include<stdio.h> struct student { int number; char name[20]; int marks; } s1= {111, Rao , 725};

main () { struct student s2= {222, Reddy , 670}; clrscr (); /* comparison of structures */ if (s1==s2) printf ( s1 and s2 are same student records\n ); else printf ( s1 and s2 are records of two different students\n ); getch (); }

(b) What is the use of a structure? Given an example for a structure with initialized values. A structure is a collection of items of similar or dissimilar data types, which share a common name. Uses of structures · A structure helps us to organize complex data in a meaningful manner. · The concept of derived data types is realized using structures. · We can create variables of structure type. · Structures are used to implement linked lists, binary trees and other data structures. Structure with initialized values: #include<stdio.h> struct record { int day;

Appendix-C.p65

26

7/29/08, 5:22 PM


Solved Question Papers

C.27

char month[20]; int year; }a={21, May ,2007}; main() { struct record b; clrscr(); b.day=28; strcpy(b.month, May ); b.year=2007; printf( details of exam held\n ); printf( DATE-%d\tMonth-%s\tYear%d\n ,a.day,a.month,a.year); printf( DATE-%d\tMonth-%s\tYear%d\n ,b.day,b.month,b.year); getch(); }

5.

(a) Distinguish between text mode and binary mode operation of a file. Text Mode

Binary Mode

The different modes of operations used in text mode are a, r, w a- append, r- read, w- write a+, r+, w+ are the additional modes used in text mode. a+ - read and write and the contents are safe when the file is opened. r+ - read and write and the contents are safe when the file is opened. w+ - read and write, a file will be created if it does not exist

The different modes of operations used in binary mode are ab, rb, wb ab- append, rb- read, wb- write a+b, r+b, w+b are additional modes used in binary mode. a+b - read and write and the contents are safe when the file is opened. r+b - read and write and the contents are safe when the file is opened. w+b - read and write , a file will be created if it does not exist

(b) Write a C program to open a pre-existing file and add information at the end of the file. Display the contents of the file before and after appending. #include<stdio.h> main() { FILE *fptr; char ch; clrscr(); fptr=fopen( input.txt , r+ ); printf( the contents of the file before appending data\n ); while((ch=getc(fptr))!=EOF) printf( %c ,ch); printf( enter the data to be appended\n ); while((ch=getchar())!=EOF)

Appendix-C.p65

27

7/29/08, 5:22 PM


C.28 Solved Question Papers putc(ch,fptr); printf( the contents of the file after appending data:\n ); while((ch=getc(fptr))!=EOF) printf( %c ,ch); getch(); }

6. Write a program to convert a given prefix expression to postfix expression using stacks. #include<stdio.h> #include<conio.h> #include<ctype.h> char stack[30],str[30]; int tos=0; void push(char c) { if(tos>=29) { printf( stack is full\n ); return; } else stack[tos++]=c; } char pop() { tos ; if(tos<0) { printf( stack is empty\n ); return 0; } else return(stack[tos]); } main() { int i,check_stack=0; char x; clrscr(); printf( enter the prefix expression\n ); gets(str); for(i=0;str[i]!= \0 ;i++) { if(!isalpha(str[i]))

Appendix-C.p65

28

7/29/08, 5:22 PM


Solved Question Papers { if(check_stack>1) printf( %c ,pop()); push(str[i]); check_stack=0; } else { printf( %c ,str[i]); check_stack++; if(check_stack==2) { printf( %c ,pop()); check_stack=0; } } } while(tos!=0) printf( %c ,pop()); getch(); }

7.

(a) Write a C program to implement binary tree traversals. Binary tree traversals are of three types. They are (1) Inorder traversals (2) Preorder traversals (3) Post order traversals

/* IN ORDER TRAVERSAL */ inorder(node * root) { if(root!=NULL) { inorder (root->left); printf( %d , root->data); inorder(root->right); } else return; } /*

PRE ORDER TRAVERSAL

preorder(node * root) { if(root!=NULL) { preorder (root->left);

Appendix-C.p65

29

7/29/08, 5:22 PM

*/

C.29


C.30 Solved Question Papers

else return; } /*

printf( %d , root->data); preorder(root->right); }

POST ORDER TRAVERSAL

*/

postorder(node * root) { if(root!=NULL) { postorder (root->left); printf( %d , root->data); postorder(root->right); } else return; }

(b) Write an algorithm to count the number of leaf nodes in a binary tree. What is its computing time?

8.

tree( struct node * root) { static int count=0; if(root!=NULL) { if(root->left!=NULL||root->right!=NULL) { tree(root->left); count++; tree(root->right); } else return; } else return; }

(a) Write a C program to sort the elements of an array using Quick sort with a suitable example. /* main function */ #include<stdio.h> #include<conio.h> main() {

Appendix-C.p65

30

7/29/08, 5:22 PM


Solved Question Papers int i,n,a[20]; clrscr(); printf( enter the size of array\n ); scanf( %d ,&n); printf( enter the elements into array\n ); for(i=0;i<n;i++) scanf( %d ,a+i); quicksort(a,0,n 1); prints( elements after sorting are\n ); for(i=0;i<n;i++) printf( %d\t ,*(a+i)); getch(); } /* QUICK SORT FUNCTION quicksort(int *k,int lb,in tub) { int pivot,i,j; if(lb<ub) { i=lb; j=ub; pivot=i; while(i<j) { while((*(k+i)<=*(k+pivot))&&(i<ub)) i++; while(*(k+j)>*(k+pivot)) j ; if(i<j) swap(k+i,k+j); } swap(k+j,k+pivot); quicksort(k,lb,j 1); quicksort(k,j+1,ub); } return; } /* Function for swapping swap(int *a, int *b) { int temp; temp=*a; *a=*b; *b=temp; }

Appendix-C.p65

31

*/

7/29/08, 5:22 PM

*/

C.31


C.32 Solved Question Papers Example : Consider the elements to be sorted 42 23 74 11 65 58 Pass 1: 11 23 36 42 65 58 Pass 2: 11 23 36 42 65 58 Pass 3: 11 23 36 42 58 65 Pass 4: 11 23 36 42 58 65 Pass 5: 11 23 36 42 58 65 The elements after sorting are 11 23 36 42 58 65 74

94 94 94 94 87 74

36 74 74 74 74 87

99 99 99 99 94 94

87

94

99

87 87 87 87 99 99

(b) What is the worst case and best case time complexity of the above program? The time complexity of the above program in the worst and best case is O(n2) and O(nlogn) respectively.

Appendix-C.p65

32

7/29/08, 5:22 PM


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.