<<

Programming and Data Structures May/June 2007

SET-2

1. (a) What is a string constant? How do string constants differ from character constants? Do string constants represent numerical values? A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank spaces. Example: “hai” , “1982”, etc; A character constant is not equivalent to the single character constant. A single character string constant does not have an equivalent integer whereas a character constant has an integer value (ASCII value). String constants do not represent equivalent numerical values. (b) Summarize the standard escape sequences in C. Describe them. Black slash characters are also called escape sequences. Constant Meaning ‘\a’ Audible alert(bell) ‘\b’ Back space ‘\f’ Form feed ‘\n’ New line ‘\r’ Carriage return ‘\t’ Tab space ‘\v’ Vertical tab ‘\” Single quote ‘\”’ Double quote ‘\?’ ‘\\’ Back slash ‘\0’ Null (c) What is a variable? How can variables be characterized? Give the rules for variable declaration. A variable is a data name that may be used to store a data value. A variable may take different values at different times during execution. Variables are characterized based on the value they hold. Depending on that they are classified into int, float, char, double, and so on. Rules for variable declaration: · They must begin with a letter. Some compilers permit underscore as the first character. · ANSI standard recognizes a length of 31 characters. However, the length should not be normally more than eight characters.

Appendix-C.p65 12 7/29/08, 5:21 PM Solved Question Papers C.13

· Uppercase and lowercase are significant. That is, variable SUM is not the same as sum or Sum. · The variable name should not be a keyword. · A blank space is not allowed. (d) What is the purpose of type declarations? What are the components of type declara- tion? Type declarations are used to declare variables, which store the value of any data type. The syntax for declaring a variable is data-type v1, v2, v3, vn; The components of type declaration are data type and variable list. Data types specify the type of data that a variable will hold. Example: int a; float b; char ch; a is a variable that holds an integer-type value. Similarly, b and ch are variables of float and character data type respectively. 2. (a) Write a program to demonstrate passing an array argument to a function. Con- sider the problem of finding the largest of N numbers defined in an array. #include main() { int a[20],n,i; clrscr(); printf(“enter the size of an array\n”); scanf(“%d”,&n); printf(“enter elements into array\n”); for(i=0;i

largest(x,p) int x[],p; { int large,i; large=x[0]; for(i=0;i

Appendix-C.p65 13 7/29/08, 5:21 PM C.14 Solved Question Papers

(b) Write a recursive function power ( base , exponent) that when invoked returns a base exponent. #include main() { int res=0,base,exponent; clrscr(); printf(“enter the values of base and exponent\n”); scanf(“%d%d”,&base,&exponent); res=power(base,exponent); printf(“the result is %d\n”,res); getch(); }

int power(b,e) int b,e; { if(e==0) return 1; return(b*power(b,e–1)); } 3. (a) What is a pointer? How is a pointer initiated? Give an example. A pointer is a variable which stores the address of another variable. The syntax for declaring a pointer is data type * pointer_name; Example: int *ptr; char *cptr; float *p; Once a pointer variable is declared, it can be assigned the address of a variable using assignment statement. Example: int a, *ptr; ptr=&a; ‘&’ is called address of operator. ptr is assigned the address of a. The pointer and variable must be of the same data type. (b) State whether each of the following statements is true or false. Give reasons. (i) An integer can be added to a pointer. True. Example: ptr=ptr+1; here, the pointer is incremented depending on its data type. If it is an integer then the scale factor is 2 bytes, character 1 byte, and so on. (ii) A pointer can never be subtracted from another pointer. False. Because C supports pointer arithmetic, by which a pointer can be subtracted or added to another pointer. (iii) When an array is passed as an argument to a function, a pointer is passed. True. Because the array name is itself a constant pointer to that array. (iv) Pointers cannot be used as formal parameters in headers to function defini- tions.

Appendix-C.p65 14 7/29/08, 5:22 PM Solved Question Papers C.15

False. C supports call by reference in which the formal parameters are pointers. (c) If m and n have been declared as integers and p1 and p2 as pointers to inte- gers, then find out the errors, if any, in the following statements. (i) p1=&m; The given statement is correct. (ii) p2 = n; The given statement is an error as the compiler cannot convert int to int *. (iii) m=p2-p1; The given statement is correct as C supports pointer arithmetic. (iv) *p1=&n; The given statement is not correct as the compiler cannot convert int* to int. 4. Write a C program to compute the monthly pay of 100 employees using each employee’s name and basic pay. The DA is computed as 52% of the basic pay. Gross-salary (Basic- pay+DA).Print the employee’s name and gross salary. #include struct employee { char name[20]; float basic_pay; float DA; float Gross_salary; }E[100]; main() { int i; clrscr(); for(i=0;i<100;i++) { printf(“enter the name and basic pay for %d employee\n”,i+1); scanf(“%s%f”,E[i].name,&E[i].basic_pay); } for(i=0;i<100;i++) { E[i].DA=52*E[i].basic_pay/100; E[i].Gross_salary=E[i].DA+E[i].basic_pay; printf(“employee name is %s, gross salary=%f\n”,E[i].name,E[i].Gross_salary); } getch(); }

5. (a) What are the file I/O functions in C? Give a brief note about the task performed by each function. Once a file is opened, reading out of or writing to it is accomplished using the standard I/O routines.

Appendix-C.p65 15 7/29/08, 5:22 PM C.16 Solved Question Papers

The getc and putc functions The simplest file I/O functions are getc and putc. These are analogous to getchar and putchar functions and handle one character at a time. Assume that a file is opened with mode w and file pointer fp. Then, the statement putc(c,fp) ; writes the character contained in the character variable c to the file associated with FILE pointer fp. Similarly, getc is used to read a character from a file that has been opened in read mode. For example, the statement c=get(fp1); would read a character from the file whose file pointer is fp1. The file pointer moves by one character position for every operation of getc or putc. The getc will return an end-of-file marker EOF, when or end of the file has been reached. The fprintf and fscanf functions These functions are analogous to printf and scanf. The syntax for fprintf is fprintf(file pointer, ”control string”, variable list); Example: fprintf(fptr, “ %d%s”,a, str); fprintf is used to write multiple data types to the file at a time. The syntax for fscanf is fscanf(file pointer, ”control string”, variable list); Example: fscanf(fptr, “ %d%s”,a, str); fscanf is used to read multiple data types from the file. (b) Write a program to read an input file and count the number of characters in the input file. #include #include main() { FILE *fptr; int nc=0; char ch; clrscr(); fptr=fopen(“input.c”,”r”); if(fptr!=NULL) { while((ch=getc(fptr))!=EOF) { nc++; } fclose(fptr); printf(”number of characters =%d\n”,nc); } else printf(“the input file does not exist\n”); getch(); }

Appendix-C.p65 16 7/29/08, 5:22 PM Solved Question Papers C.17

6. Write a C program for implementation of various operations on circular queue. #include #define size 20 int cq[size],F=0,R=0,count=0; main() { int a,ch; clrscr(); while(1) { printf(“main menu\n”); printf(“1.insertion\t2.deletion\t3.display\t4.exit\n”); printf(“enter your choice\n”); scanf(“%d”,&ch); switch(ch) { case 1: printf(“enter the value to be inserted\n”); scanf(“%d”,&a); insert(a); break; case 2: delete(); break; case 3: display(); break; case 4: exit(0); default: printf(“wrong choice\n”); } getch(); }

/* insertion function */ insert(int x) { if(F==R&&count==size-1) { printf(“circular queue is full\n”); return; } cq[R]=x; R=(R+1)%size; }

Appendix-C.p65 17 7/29/08, 5:22 PM C.18 Solved Question Papers

/* deletion function */ delete() { if(F==R&&count==0) { printf(“circular queue is empty\n”); return; } printf(“the deleted element is %d\n”,cq[F]); F=(F+1)%size; }

/* display function */ display() { int i; if(F==R&&count==0) { printf(“circular queue is empty\n”); return; } for(i=F;i!=R;i=(i+1)%size) printf(“%d”,cq[i]); } 7. How can a polynomial in three variables (x ,y and z) be represented by a singly linked list? Each node should represent a term and should contain the powers of x, y and z as well as the coefficient of that term. Write a routine to evaluate this polynomial for given values of x, y and z. A polynomial can be represented with a linked list in the same way as we represent a polyno- mial with a single variable. COEFFICIENT X- POWER Y- POWER Z- POWER NEXT Addr Consider a simple polynomial P(x,y,z)=5x2y3z4+10x3y2z5. This can be represented as 5234 ¾¾¾¾¾¾¾¾ 10 3 2 5 NULL

Routine to evaluate the given polynomial struct poly { int coef, xpow, ypow, zpow; }; evaluate() { int x, y, z; long int sum=0; if(first==NULL) {

Appendix-C.p65 18 7/29/08, 5:22 PM Solved Question Papers C.19

printf(“list is empty\n”); return; } printf(“enter the values of x, y, Z”); scanf(“%d%d%d”,&x,&y,&z); ptr=first; while(ptr->next!=NULL) { sum+=pow(x,ptr->xpow)*pow(y, ptr->ypow)*pow(z, ptr- >zpow)*ptr->coef; ptr=ptr->next; } printf(“SUM=%ld\n”,sum); } 8. (a) Write a C program to search for a given element in the integer array using binary search.

#include main() { int a[10],n,i,pos=0,key; clrscr(); printf(“enter the size of array\n”); scanf(“%d”,&n); printf(“enter the elements into array\n”); for(i=0;i

/* Binary search function */ bin_search(b,n,x) int *b,n,x; { int lb,ub,mid; lb=0; ub=n–1; while(lb<=ub) { mid=(lb+ub)/2;

Appendix-C.p65 19 7/29/08, 5:22 PM C.20 Solved Question Papers

if(x>*(b+mid)) lb=mid+1; else if(x<*(b+mid)) ub=mid–1; else return mid+1; } } (b) Write a C program to sort the elements of an array using tree sort method with a suitable example. #include #include define MAXSIZE 50 void heapsort(int elements[ ],int maxsize); void heap(int elements[ ], int root, int leaf); int elements[MAXSIZE],maxsize; main() { int i; printf(“\n enter no of elements:”); scanf(“%d”,&maxsize); printf(“enter the elements to be sorted\n”); for(i=0;i0;i–) { temp=elements[0]; elements[0]=elements[i]; elements[i]=temp; heap(elements,0,i–1); } } void heap(int elements[],int root, int leaf) { int flag, max, temp;

Appendix-C.p65 20 7/29/08, 5:22 PM Solved Question Papers C.21

flag=0; while((root*2<=leaf)&&(!flag)) { if(root*2==leaf) max=root*2; else if(elements[root*2]>elements[(root*2)+1]) max=root*2+1; if(element[root]

Appendix-C.p65 21 7/29/08, 5:22 PM