BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER 2015 - 2016 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial# 16 (SOLUTIONS) DATE : 15-DEC-2015 Answer 1 #include<stdio.h> struct course { int comp_cd; char course_name[50]; char course_no[10]; int unit; }c[5]; main() { int i; for(i=0;i<5;i++) { printf("\n%d. Enter comp code:",i+1); scanf("%d",&c[i].comp_cd); printf("\n%d. Enter course name:",i+1); scanf("%s",&c[i].course_name); printf("\n%d. Enter course number:"i+1); scanf("%s",&c[i].course_no); printf("\n%d. Enter course units:"i+1); scanf("%d",&c[i].unit); } printf("\n\nThe following information you provided\n"); printf("\nSl#\tComp Code\tCourse Name\tCourse Number\tUnits\n"); printf("---------------------------------------------------\n"); for(i=0;i<5;i++) { printf("%d\t%d\t",i+1,c[i].comp_cd); printf("%s\t",c[i].course_name); printf("%s\t",c[i].course_no); printf("%d\n",c[i].unit); } } Answer 2 #include<stdio.h> void count_NZ(int a[1000]) CS F111 COMPUTER PROGRAMMING Page 1 of 3 { int i,count=0; for(i=0;i<1000;i++) { if(a[i]>0) count++; } int *pm=malloc(count*sizeof(int)); int *pc=calloc(1,count*sizeof(int)); } printf("\nmalloc= %u\nCalloc= %u\n\n",pm,pc); Answer 3 #include<stdio.h> main() { char str[500], *st; int i_length; printf("Enter the string end with $\n"); scanf("%[^$]s",str); i_length=IDEAL_LENGTH(str); printf("\nThe ideal length is %d.\n\n",i_length); st=convert(str,i_length); printf("\nThe base pointer of the converted string is: %u\n",st); } printf("\nThe converted string is\n\n"); printf("%s\n\n",st); void* convert(char s[500], int len) { int i,j=0; char *s1; s1=(char*)malloc(len*sizeof(char)); for(i=0;s[i]!='\0';i++) { if(i==0||s[i]!=' '||(s[i]==' ' && s[i-1]!=' ')) { *(s1+j)=s[i]; j++; } } *(s1+j)='\0'; } return s1; CS F111 COMPUTER PROGRAMMING Page 2 of 3 int IDEAL_LENGTH(char s[500]) { int i, count=0; for(i=0;s[i]!='\0';i++) { if(i==0||s[i]!=' '||(s[i]==' ' && s[i-1]!=' ')) count++; } return count; } Practice Questions i) c, because both malloc() and calloc() functions obtains the memory block dynamically and returns the base pointer ii) d, malloc() function return the base address of the memory block allocated dynamically. But if the memory block is not allocated than it returns a NULL pointer. iii) d, memory allocated by malloc()/calloc() function should be made free explicitly. Therefore the base pointer returned by malloc()/calloc() function should be passed to free() function to make that memory block pointer free. iv) b, (int*)malloc(10*sizeof(int). The (int*) is the type casting. It tells the compiler that the memory block allocated dynamically will be used for storing integer values. v) a, typedef does not create a new data type. It just create a data type with the combination of existing data types. vi) a) 10 0, In the function func() the value of a is 10. In a main function the value of a is not accessible because it was assigned in a local scope so 0 will be printed. b) PILANI, If we are accessing a 2-D character array, the single dimension of it will work like a string. c) 82, the ASCII value of character ‘R’. In union the last assigned value only will remain in the memory. ***END*** CS F111 COMPUTER PROGRAMMING Page 3 of 3