170 likes | 254 Views
System Programming in C. Lecture 16. Examination Schedule. Exam: 14-jan-2002 9:00 A groups – class 211 D groups – class 214 Consultation: 11-jan-2002 13:00 Class 211. Examination. Gives 80% of the final mark (20% - home works) Written Can use all available materials EXCEPT:
E N D
System Programming in C Lecture 16
Examination Schedule • Exam: • 14-jan-2002 9:00 • A groups – class 211 • D groups – class 214 • Consultation: • 11-jan-2002 13:00 • Class 211
Examination • Gives 80% of the final mark (20% - home works) • Written • Can use all available materials EXCEPT: • Others’ help • Computers • Tasks will be given in English – If not sure in your English skills recommend bringing in dictionaries • Time: 1.5 hours
Tasks • 7-10 questions, weighted. • Types of questions: • Determine which of code pieces does what is stated • Write a short program according to the specification • Find the errors in code • Given the piece of code, describe what it does • Pointer/array arithmetic – calculate several array expressions • Questions on program building process • Short questions on the language features
Compare the Code • The task is to print user's input (string terminated with newline character) in reverse order. Which of the functions func1 and func2 accomplishes that? Why the other one does not?
Compare the Code void func1(void) { char c; c = getchar(); putchar(c); if ( c != '\n' ) func1(); } void func2(void) { char c; c = getchar(); if ( c != '\n' ) func2(); putchar(c); }
What’s Wrong Here? void insertfront(List* ilist, int val) { ListItem *newitem; newitem.next = ilist.head; newitem.data = val; ilist.head = newitem; }
Are These Versions Ok? void insertfront(List *ilist, int val) { Listitem *newitem; newitem = (Listitem*)malloc(sizeof(Listitem)); (*newitem).next = (*ilist).head; (*newitem).data = val; (*ilist).head = newitem; } void insertfront(List *ilist, int val) { Listitem *newitem; newitem = (Listitem*)malloc(sizeof(Listitem)); newitem->next = ilist->head; newitem->data = val; ilist->head = newitem; }
What is Wrong Here? void Init(int **arr, int sz_n, int sz_m) { int j,k; arr = (int **)malloc(sz_n*sizeof(int*)); for (j=0;j<sz_n;j++) { arr[j] = (int*)malloc(sz_m*sizeof(int)); for (k=0;k<sz_m;k++) arr[j][k]=0; } } main() { int **a; Init (a,5,10); printf("entry[3][3] is %d\n", a[3][3]); }
More Errors float divide(int number, int denom) { if (denom==0) { fprintf(stsderr, "divide by 0\n"); exit(1); } return (number/denom); }
What does this function do? void func1(char *s) { char *t; int c; for (t=s+(strlen(s)-1);s<t;s++,t--) { c=*s; *s=*t; *t=c; } } ... func1(s); printf("%s\n",s);
Write a Program In certain sports, eight judges are used and a competitor's score is hte average of all their marks, except the largest and the smallest. Write a C program which read eight floating point numbers into an array and then computes the average of them, except one instance of the biggest and one instance of the smallest. For example, for the input: 5.8 5.7 5.4 5.9 5.6 5.1 5.9 5.7 your program should eliminate the two most extreme values: 5.1 5.4 5.6 5.7 5.7 5.8 5.9 5.9 and print out: 5.683333 as an average of remaining values.
Building the program… What is the function of linker: - Expand macros. - Translate source code into object modules - Assemble object modules into an executable
Generic questions Given the declarations float x = 3.5; float y = 0.6; what will be the type and value of the expression: ( x > y && x >=y) Type: Value:
Review questions • What is the difference between internal and external (global) variables ? • What is the difference between static and automatic variables ? • If a variable is declared outside a function block with a modifier static , does it have internal linkage (not accessible from other files) or external linkage (can be accessed by other files) ? • What is the purpose of each field between the parenthesis in a for loop ? for(a;b;c) • What is the difference between • while (j>0)do {...} and • do {…} while (j>0);
Arrays… Assume: - fixed is a 2D arrya of integers - the actual address of fixed s "1000" - integers are 4 bytes long For each line, answer: 1) is it legal (compiles)? 2) Can it cause a run-time error? 3) If it runs, what is the outcome? … int fixed[30][40]; printf("%d ", &(fixed[0][0])); /*line 1*/ printf("%d ", &(fixed[0][30])); /*line 2*/ printf("%d ", &(fixed[0][45])); /*line 3*/ printf("%d ", &(fixed[1][5])); /*line 4*/ printf("%d ", &(fixed[30][10])); /*line 5*/ fixed[0][10] = 10; /* line 6 */ fixed[0][10] = 'a'; /* line 7 */ fixed[30][0] = 10; /* line 8 */
Arrays int a[7] = {1,2,3,4,5,6,7}; int *pa = &a[3]; Variable Type Value pa pa[1] pa[-2] a[3] a+5 a *(a+3) *(pa-2) *a *pa