1 / 43

ecs30 Winter 2012: Programming and Problem Solving # 09~10: Chapter 5

ecs30 Winter 2012: Programming and Problem Solving # 09~10: Chapter 5. Future Prof . (+ superstar) Roozbeh Nia Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. “sorting”. Input: three non-negative integers Output:

carol
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 09~10: Chapter 5

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. ecs30 Winter 2012:Programming and Problem Solving#09~10: Chapter 5 Future Prof .(+ superstar)RoozbehNia Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  2. “sorting” Input: three non-negative integers Output: (1). [Input Validation]: If any of the inputs is negative, do nothing and return 1 in the main function. (2). [Sorting the Inputs after the Validation]: If the inputs are all valid (i.e., non-negative), your program will print the largest integer (among those three) first, then the middle number, then finally the smallest number, in three different lines. (BTW, this procedure is called SORTING.) After these outputs, return 0 in the main function. Please note that you are NOT allowed to use goto, loop or array to solve this problem, even if you know how. ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  3. Problem Solving X Y Z Sorting: place the largest number in X, then Y, then finally Z ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  4. Problem Solving X Y Z Input: 3 non-negative integer, unknown order Procedure: Let’s compare Y and Z first and place the larger in Y Output: Y will be larger than Z, and we don’t know the rank of X yet comparing to both Y and Z. ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  5. Problem Solving X Y Z Input: 3 non-negative integer, Y>Z, unknown about X Procedure: Let’s compare X and Y first and place the larger in X Output: X will be the largest, and we don’t know the rank between Y and Z, but they both are smaller than X. ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  6. Problem Solving X Y Z Input: 3 non-negative integer, X Largest, unknown Y, Z, Procedure: Let’s compare Z and Y first and place the larger in Y Output: done ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  7. Felix finished here! ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  8. C Implementation intx, y, z, tmp; if (y < z) { tmp = y; y = z; z = tmp; } if (x < y) { tmp = x; x = y; y = tmp; } if (y < z) { tmp = y; y = z; z = tmp; } printf(“%d\n%d\n%d\n”, x, y, z); ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  9. Sorting 300 numbers X0 X1 X299 ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  10. Sorting 300 numbers X0 X1 X299 How to store 300 or even a variable number of items? (“array, string, linked list, struct”) How to represent the repeating actions (but may be slightly different in each iteration)? Looping! ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  11. Initialization Condition Check Loop body Condition Update ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  12. Assuming we have 7 employees… ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  13. ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  14. while (<condition>) <statement> <statement>:= single statement; or { statements } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  15. From HW#2_2: example of input checking using while intm,n; m = n = 0; while (!(m > n)) { printf(“\nm = ? “); scanf(“%d”, &m); printf(“\nn = ? “); scanf(“%d”, &n); } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  16. for(count_emp=0;count_emp<number_emp;count_emp+=1){…} ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  17. for (<initialization>; <condition>; <update>) <statement> <statement> := single statement; or { statements } Initialization Condition Update ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  18. Initialization Condition Check for (<initialization>; <condition>; <update>) <statement> Loop body Condition Update ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  19. count_emp = 0; Condition Check count_emp< number_emp Loop body count_emp = count_emp + 1; count_emp += 1; count_emp++; Condition Update ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  20. What is the difference between i++ and ++i? ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  21. i++ versus ++i ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  22. #include <stdio.h> int main(void) { int i,j; i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =i++;//(1) j = i;(2) i = i+1; printf("i = %d, j = %d\n", i,j); i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =++i;//(1) i = i+1;(2) j =i; printf("i = %d, j = %d\n", i,j); } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  23. for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) Print 7 times versus 6 times! ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  24. for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  25. for (i = n; i > 1; --i) product *= i; for (i = 2; i <= n; ++i) product *= i; ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  26. How many times? printf(“I will not tag ecs30 students on Facebook.\n”); ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  27. ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  28. Debugging Logic Errors for (i = 1; i < n; ++i) product *= i; for (i = 1; i < n; ++i) { product *= i; printf(“product=%d, i=%d\n”, product, i); } printf(“OUT:product=%d, I =%d\n”, product, i); ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  29. Preprocessor #define FACT_DEBUG … … for (i = 1; i < n; ++i) { product *= i; #ifdef FACT_DEBUG printf(“product=%d, i=%d\n”, product, i); #endif/* FACT_DEBUG */ } #ifdef FACT_DEBUG printf(“OUT:product=%d, I =%d\n”, product, i); #endif /* FACT_DEBUG */ % gcc -c fact.c -DFACT_DEBUG % gcc -c fact.c -DFACT_DEBUG=XYZ ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  30. // Figure 5.11, pp. 243 #include <stdio.h> int main(void) { FILE *inp = NULL; int sum = 0; int score, input_status; if ((inp = fopen("scores.dat", "r")) == NULL) return -1; printf("Scores\n"); input_status = fscanf(inp, "%d", &score); while (input_status != EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } printf(”\nSum of exam scores is %d\n", sum); fclose(inp); return0; } scores.dat ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  31. scanf(“%d”, &x); • fscanf(stdin, “%d”, &x); • sscanf(string, “%d”, &x); /* chapter 9 */ ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  32. input_status = fscanf(inp, "%d", &score); while (input_status != EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  33. The C “TrIO” • printf, scanf (stdout, stdin) • [printf/scanf](<format string>) • fprintf, fscanf (file pointer) • pp 78~80, 242~244 • [fprintf/fscanf](<FILE *>, <format string>) • fopen and fclose • sprintf, sscanf (string) • Chapter 9 • [sprintf/sscanf](<char *>, <format string>) ecs30b Fall 2008 Lecture #13

  34. File I/O stdout stdin Program stderr foo scanf(“%d”, &x); fscanf(f, “%d”, &x); sscanf(s, “%d”, &x); bar ecs30b Fall 2008 Lecture #13

  35. Standard File I/O stdout stdin Program stderr foo bar ecs30b Fall 2008 Lecture #13

  36. // Figure 5.11, pp. 243 #include <stdio.h> int main(void) { FILE *inp = NULL; int sum = 0; int score, input_status; if ((inp = fopen("scores.dat", "r")) == NULL) return -1; printf("Scores\n"); input_status = fscanf(inp, "%d", &score); while (input_status != EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } printf(”\nSum of exam scores is %d\n", sum); fclose(inp); return0; } scores.dat ecs30b Fall 2008 Lecture #13

  37. input_status = fscanf(inp, "%d", &score); while (input_status != EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } F1: 20 30 40 10 F2: 20 30 40 10 F3: 20304010 HW… F1’: 20\n30\n40\n10 ecs30b Fall 2008 Lecture #13

  38. Initialization Loop body Condition Update Condition Check ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  39. break and continue ecs30b Fall 2008 Lecture #13

  40. Initialization Condition Check break; Loop body (out of the innermost switch/for/while/do..while statement) (directly to the end of the innermost for/while/do..while bracket!) Condition Update ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  41. for (<initialization>;<condition>;<update>) { <statements>; if (<condition>) break; <statements>; } HERE!!! ecs30b Fall 2008 Lecture #13

  42. for (<initialization>;<condition>;<update>) { <statements>; if (<condition>) continue; <statements>; HERE!!! } ecs30b Fall 2008 Lecture #13

  43. input_status = fscanf(inp, "%d", &score); while (input_status != EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } while (1) { if (fscanf(inp, "%d", &score) == EOF) break; printf("%5d\n", score); sum += score; } ecs30b Fall 2008 Lecture #13

More Related