90 likes | 160 Views
CS1010 Discussion Group C03. Week 9 Aaron Tan. Output: 310 0 5. Q1. 310. int main(void) { int x = 5, y = 5; int sum = f(&x, y); printf("%d %d %d<br>", sum, x, y); return 0; } int f(int *x, int y) { int sum = 0; while (*x > 0) { sum += g(&y); *x -= 1; } return sum; }
E N D
CS1010 Discussion Group C03 Week 9 Aaron Tan
Output: 310 0 5 Q1 310 int main(void) { int x = 5, y = 5; int sum = f(&x, y); printf("%d %d %d\n", sum, x, y); return 0; }int f(int *x, int y) { int sum = 0; while (*x > 0) { sum += g(&y); *x -= 1; } return sum; } int g(int *y) { *y *= 2; return *y; } 2 3 4 sum sum y y x 5 5 What is the output? 0 1 x y 5 10 20 160 40 80 30 70 10 0 150 310
Q4 int brusco_gcd(int a, int b) { ... return a; } Pointer parameter or no pointer parameter? void brusco_gcd(int a, int b, int *ans) { ... *ans = a; } Given a choice between a function that returns a value and a function that takes in a pointer parameter, choose the former.
Q5 void brusco_gcd(int a, int b) { ... printf("The GCD is %d\n", a); } To print or not to print? In general,do not mix computation task and input/output task in a single function. (There might be exception.)
Q6 • Separate functions: • int compute_surface_area(int length, int width, int height) • double compute_diagonal(int length, int width, int height) • Single function with pointer parameters • void compute_surface_area_and_diagonal(int length, int width, int height, int *areaPtr, double *diagonalPtr) Which is better? • In general,each function to do one task is preferred (cohesion). • Disadvantage of option (2): caller needs to declare two variables and passes their addresses to function. • Exceptions when option (2) is used: • When the values to pass back are so intimately related that they are considered as one group of date (eg: x- and y-coordinates of a point). [However, there is alternative, using structure – to be covered.] • When efficiency is more important than cohesion
Q7 int main(void) { FILE *infile; int num; if ((infile = fopen("feof.in", "r")) == NULL) { printf("Cannot open file \"feof.in\"\n"); exit(1); } while (!feof(infile)) { fscanf(infile, "%d", &num); printf("Value read: %d\n", num); } fclose(infile); return 0; } File feof.in: feof issue (1/3) 10 20 30 Output: Value read: 10 Value read: 20 Value read: 30 Value read: 30 Reason: feof() is TRUE only after the end of file (EOF) is read, not when EOF is reached. What has happened is the last value of feof.in (30) was read, but not the EOF. When the loop returns, the fscanf() attempts to read one more value and failed. Since no error checking was done, whatever was left in the buffer is still there and the loop continues.
Q7 int main(void) { FILE *infile; int num; if ((infile = fopen("feof.in", "r")) == NULL) { printf("Cannot open file \"feof.in\"\n"); exit(1); } while (!feof(infile)) { if (fscanf(infile, "%d", &num) != EOF) printf("Value read: %d\n", num); } fclose(infile); return 0; } feof issue: Solution 1 (2/3) Quick fix use EOF. However, there are now 2 tests in a loop.
Q7 int main(void) { FILE *infile; int num; if ((infile = fopen("feof.in", "r")) == NULL) { printf("Cannot open file \"feof.in\"\n"); exit(1); } while (fscanf(infile, "%d", &num) != EOF) { printf("Value read: %d\n", num); } fclose(infile); return 0; } feof issue: Solution 2 (3/3) A better solution.