SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I Year & Sem: I B.E (CSE) & II Date of Exam: 21/02/2015 Subject Code & Name: CS6202 & Programming & Data Structures I Total Marks: 50 Part A 1) Differentiate while & do while statement? While Statement (i) Executes the statements within the while block if only the condition is true. (ii) The condition is checked at the starting of the loop Do While Statement (i) Executes the statements within the while block at least once. (ii) The condition is checked at the end of the loop 2) Define structure. How to initialize a structure? A structure is a user defined data type that groups logically related data items of different data types into a single unit. struct student int rno; int age; char name[20]; ; struct student s1=223,17, john ; 3) What is the value of ptr[0] in the following program? void main() int num[5] = 1,3,6,7,9; int *ptr; ptr = &num[2]; Output: Ptr[0] = 6 4) How to handle function with variable number of arguments in C? Va_start Va_end Va_arg Va_list 5) Define file handling. What are the ways to access files? A file handling is set of records that is available in permanent storage. Ways to access files: Random access Sequential access
Part B 6) a) Write a C program to explain the concept of recursion. (8) #include<stdio.h> int fact(int); int main() int num,f; printf("\nenter a number: "); scanf("%d",&num); f=fact(num); printf("\nfactorial of %d is: %d",num,f); int fact(int n) if(n==1) return 1; return(n*fact(n-1)); 6) b) Define a structure called student that should contain name, regno and marks of 3 subjects and average. Write a C program to read the details of name, regno and marks of 3 subjects for 30 students. Calculate the average and display the name, regno, marks and average of each students. (8) #include<stdio.h> #include<conio.h> struct student int rollno; char name[20]; int m1,m2,m3; float avg; ; struct student s[50]; void main() int i,j,n; clrscr(); printf("\n enter the number of students record:"); scanf("%d",&n); for(i=0;i<n;i++) printf("\n enter the roll no=\n"); scanf("%d",&s[i].rollno); printf("\n enter the name \n"); scanf("%s",s[i].name); printf("\n enter the mark="); scanf("%d",&s[i].m1); printf("\n enter the mark="); scanf("%d",&s[i].m2); printf("\n enter the mark="); scanf("%d",&s[i].m3); s[i].avg=(s[i].m1+s[i].m2+s[i].m3)/3; for(i=0;i<n;i++) printf("\n rollno=%d",s[i].rollno); printf("\n name=%s",s[i].name); printf("\n mark1=%d",s[i].m1); printf("\n mark2=%d",s[i].m2); printf("\n mark3=%d",s[i].m3); printf("\n Average=%f",s[i].avg); getch();
7) a) i) Explain in detail about macro substitution. (8) Assign symbolic constants. #define identifier srting/integer Simple macro #define age 20 #define pi 3.14 Argumented macro #define sq n *n Nested macro #define A 50 #define B + A 7) a) ii) Explain about function pointers. (8) #include <stdio.h> #include<conio.h> void swap(int *x, int *y); int main () int a,b; printf("\n Enter the value of a:"); scanf("%d",&a); printf("\n Enter the value of b:"); scanf("%d",&b); printf("before swap, value of a and b is: %d\t %d\n", a,b ); swap(&a, &b); printf("\nin main, value of a and b is: %d \t %d\n", a,b ); void swap(int *x,int *y) int temp; temp=*x; *x=*y; *y=temp; printf("after swap, value of a and b is: %d \t %d\n", *x,*y ); 7) b) i) Explain about Control statements in detail. (16) if statement: if (condition is true) True statements; If : if(condition) Statement1;... Statement n; Statement1;... Statement n;
Nested if : if(condition 1) if(condition 2) True statement 2; False statement 2; False statement 1; Switch(): switch(expression) case constant-expression : break; /* optional */ case constant-expression : break; /* optional */. default : /* Optional */ While: while(condition) Continue: Continue; Break: Do: For: Break; do while( condition ); for ( init; condition; increment ) 8) a) i) Write a C program to perform matrix Multiplication. (8) #include<stdio.h> #include<conio.h> void main() int a[25][25],b[25][25],c[25][25],i,j,k,r,s; int m,n; clrscr(); printf(" Enter the row and columns of A matrix..."); scanf("%d%d",&m,&n); printf("enter the row and columns of B matrix,,,,,,,,,,"); scanf("%d%d",&r,&s); if(m!=r) printf("\n The matrix cannot multipled"); printf("\n Enter the elements of A matrix"); scanf("\t%d",&a[i][j]); printf("\n Enter the elements of B matrix");
matrix"); matrix"); scanf("\t%d",&b[i][j]); printf("\n the elements of A printf("\t%d",a[i][j]); printf("\n the elements of B printf("\t%d",b[i][j]); c[i][j]=0; for(k=0;k<m;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("the multiplication of two matrixes"); printf("\t%d",c[i][j]); getch(); 8) a) ii) Explain about union with an example. (8) Union is also a derived type as structure. Union can be defined in same manner as structures just the keyword used in defining union in union where keyword used in defining structure was struct. union car char name[50]; int price; ; union car c1, c2, *c3; Program: #include <stdio.h> union job char name[32]; float salary; int worker_no; u; int main() printf("enter name:\n"); scanf("%s",&u.name); printf("enter salary: \n"); scanf("%f",&u.salary); printf("displaying\nname :%s\n",u.name); printf("salary: %.1f",u.salary); 8) b) Explain in detail about file manipulation. (16) A file is a collection of related data that a computers treats as a single unit. Steps in Processing a File 1. Create the stream via a pointer variable using the FILE structure: FILE *p;
2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file. Opening a file FILE *fopen(filename, mode); Example: FILE *p1, *p2; p1 = fopen( INPUT.txt, r ); p2 =fopen( OUTPUT.txt, w );.... fclose(p1); fclose(p2); Closing a file fclose(file_pointer); fscanf() fscanf (fp,"string",identifiers); fprintf() fprintf (fp,"string",variables); fseek() Example: #include <stdio.h> int main() FILE * f; f = fopen("myfile.txt", "w"); fputs("hello World", f); fseek(f, 6, SEEK_SET); fputs(" India", f); fclose(f);