1 / 9

CS1010 Discussion Group C03

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(&amp;x, y); printf(&quot;%d %d %d<br>&quot;, sum, x, y); return 0; } int f(int *x, int y) { int sum = 0; while (*x &gt; 0) { sum += g(&amp;y); *x -= 1; } return sum; }

Download Presentation

CS1010 Discussion Group C03

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010 Discussion Group C03 Week 9 Aaron Tan

  2. 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

  3. 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.

  4. 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.)

  5. 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

  6. 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.

  7. 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.

  8. 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.

  9. End of file

More Related