1 / 39

ecs30 Winter 2012: Programming and Problem Solving #11: Chapter 5~8 – from Loops to Arrays

ecs30 Winter 2012: Programming and Problem Solving #11: Chapter 5~8 – from Loops to Arrays. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. “sorting”. Input: three non-negative integers Output:

latif
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving #11: Chapter 5~8 – from Loops to Arrays

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#11: Chapter 5~8 – from Loops to Arrays Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #11

  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 #11

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

  4. Sorting 300 numbers X0 X1 X299 ecs30 Winter 2012 Lecture #11

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

  6. while (<condition>) <statement> <statement>:= single statement; or { statements } ecs30 Winter 2012 Lecture #11

  7. for (<initialization>; <condition>; <update>) <statement> <statement> := single statement; or { statements } Initialization Condition Update ecs30 Winter 2012 Lecture #11

  8. Initialization Condition Check for (<initialization>; <condition>; <update>) <statement> Loop body Condition Update ecs30 Winter 2012 Lecture #11

  9. What is the difference between i++ and ++i? i++; ++I; I = I + 1; ecs30 Winter 2012 Lecture #11

  10. #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 #11

  11. for (i = n; i > 1; --i) product *= i; for (i = 2; i <= n; ++i) product *= i; ecs30 Winter 2012 Lecture #11

  12. Debugging Logic Errors for (i = 1; i < n; ++i) product *= i; Section 5.10 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 #11

  13. 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 #11

  14. The C “TrIO” • printf, scanf(stdout, stdin) • [printf/scanf](<format string>) • fprintf, fscanf(file pointer) • Chapter 12 • [fprintf/fscanf](<FILE *>, <format string>) • fopen and fclose • sprintf, sscanf(string) • Chapter 9 • [sprintf/sscanf](<char *>, <format string>) ecs30 Winter 2012 Lecture #11

  15. File I/O stdout stdin Program stderr foo scanf(“%d”, &x); fscanf(f, “%d”, &x); sscanf(s, “%d”, &x); bar ecs30 Winter 2012 Lecture #11

  16. Standard File I/O stdout stdin Program stderr foo bar ecs30 Winter 2012 Lecture #11

  17. 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 #11

  18. for (<initialization>;<condition>;<update>) { <statements>; if (<condition>) break; <statements>; } HERE!!! ecs30 Winter 2012 Lecture #11

  19. for (<initialization>;<condition>;<update>) { <statements>; if (<condition>) continue; <statements>; HERE!!! } ecs30 Winter 2012 Lecture #11

  20. Chapter 6: Modular Programming Chapter 7: Simple Data Types Chapter 8: Arrays ecs30 Winter 2012 Lecture #11

  21. Homework #5! ecs30 Winter 2012 Lecture #11

  22. Call by References(Chapter 6) • How to let “a called function” change the values of variables from its “caller”? • Global variables • Call by references (call by pointers) ecs30 Winter 2012 Lecture #11

  23. 5.0 x = x * 2; Caller No, they don’t share. Calling means “copying the values!” Callee ecs30 Winter 2012 Lecture #11

  24. num_1 X num_2 n Functions in C are“call by values”for input parameters! I.e., we copy the values fromcallertocalleewhencalling a function! (and, NOT copying them back when returns!) ecs30 Winter 2012 Lecture #11

  25. An interesting difference • Input/output for functions • A design concept in problem solving • What should be input to a function/module? • Call by Value/Referene/Name • Parameters and Return values for functions • An implementation concept in C (or a programming language feature of C) • We can use this feature to implement different ways of inputs and outputs! • C is call by value!(but…) Advanced ecs30 Winter 2012 Lecture #11

  26. num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11

  27. num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11

  28. Call by References &num_1 &x &num_2 &n num_1 scale(&num_1, &num_2); double scale(double *xp, int *np) {… (*xp) = (*xp) * (*np); } num_2 ecs30 Winter 2012 Lecture #11

  29. Input/Output of Functions -1.651  -, 1, 651 ecs30 Winter 2012 Lecture #11

  30. char s; int w; double f; separate(num, &s, &w, &f); ecs30 Winter 2012 Lecture #11

  31. ecs30 Winter 2012 Lecture #11

  32. ecs30 Winter 2012 Lecture #11

  33. int main(void) { int num1, num2, num3; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d%d%d",&num1,&num2,&num3); // Ordering order(&num2,&num3); order(&num1,&num2); order(&num2,&num3); // Results fprintf(stdout,”%d\n%d\n%d\n", num1, num2, num3); return 0; } ecs30 Winter 2012 Lecture #11

  34. ecs30 Winter 2012 Lecture #11

  35. ecs30 Winter 2012 Lecture #11

  36. Call by References &num_1 &x &num_2 &n num_1 scale(&num_1, &num_2); double dcale(double *xp, double *n) {… (*xp) = (*xp) * 2; } num_2 ecs30 Winter 2012 Lecture #11

  37. int main(void) { int num[3]; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d%d%d",&(num[0]),&(num[1]),&(num[2]); // Ordering order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Results fprintf(stdout,”%d\n%d\n%d\n",num[0],num[1],num[2]); return 0; } ecs30 Winter 2012 Lecture #11

  38. We skip Chapter 7 for now… Chapter 8, Array! ecs30 Winter 2012 Lecture #11

  39. Declare the array Access the array Memory Cell models for the array ecs30 Winter 2012 Lecture #11

More Related