1 / 32

ecs30 Winter 2012: Programming and Problem Solving # 05: Chapters 2 ~ 5

ecs30 Winter 2012: Programming and Problem Solving # 05: Chapters 2 ~ 5. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. kms = 1.609 * miles ; . “ = ” : assignment from right to left

ronny
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 05: Chapters 2 ~ 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#05: Chapters 2~5 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 WInter 2012 Lecture #05

  2. kms = 1.609 * miles; • “ = ” : assignment from right to left • the LEFT side is always a declared variable! • The RIGHT side can be: • variables (including [&, *]), constants, logical/arithmetic expressions kms = 1.609 * miles; ecs30 WInter 2012 Lecture #05

  3. Assignment ecs30 WInter 2012 Lecture #05

  4. Arithmetic expressions V = p2-p1/t2-t1; • +, -, *, /, % ecs30 WInter 2012 Lecture #05

  5. Evaluation Expressions(section 2.5 and 4.2) • Parenthesis rule • Operator precedence rule • function calls, unary {+,-,&,*,!}, binary {*,/,%}, binary {+,-}, logic {<,<=,>=,>},… assignment {=} • Associativity rule • Unary: right to left (e.g.,(-*x)) • Binary: left to right (e.g., (x / y * z)) ecs30 WInter 2012 Lecture #05

  6. Readability • x/y*z/w+a/b--c*-*d • First of all, you shouldn’t write such a long expression! • (((x/y)*z)/w)+(a/b)-((-c)*(-(*d))) ecs30 WInter 2012 Lecture #05

  7. Reusability/Sharing, Readability/Debugging, Modularity Abstraction Function vs. Macro ecs30 WInter 2012 Lecture #05

  8. Function • intmain (void) { … } • float myfunc (intx) { …; return f;} <return type> <function name> (input parameters) {<body>} • { <declarations> <statements> } ecs30 WInter 2012 Lecture #05

  9. #define PI 3.14159 double find_area (double r) { double area; area = r * r * PI; return area; } ecs30 WInter 2012 Lecture #05

  10. Inputs and Outputs (parameters and return value) ecs30 WInter 2012 Lecture #05

  11. Homework Assignment #2 % gcc -E ecs30b_hw2_2.c % gcc -c ecs30b_hw2_2.c % gcc ecs30b_hw2_2.o -o ecs30b_hw2_2 -lm Chapter 3: mathematical library functions ecs30 WInter 2012 Lecture #05

  12. Call by Value, Reference, Name Pointer/Address of Memory/Variable Pointer to Function ~ Control of “where is the function code”! ecs30 WInter 2012 Lecture #05

  13. Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 WInter 2012 Lecture #05

  14. double Find_area(doubler) { double area; area = r * r * PI; return area; } ecs30 WInter 2012 Lecture #05

  15. Call_by_Value ---- (Call_by_Reference) double r_out, r_in, the_area; the_area = r_out * r_out * PI – r_in * r_in * PI; the_area = find_area(r_out) – find_area(r_in); ecs30 WInter 2012 Lecture #05

  16. Section 13.8 #define AREA(r) (r * r * PI) double r_out, r_in, the_area; the_area = r_out * r_out * PI – r_in * r_in * PI; the_area = find_area(r_out) – find_area(r_in); the_area = AREA(r_out) – AREA(r_in); ecs30 WInter 2012 Lecture #05

  17. ecs30 WInter 2012 Lecture #05

  18. double r_in, r_out, the_area; get_input_paramenters(); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(void) { double r_in, r_out; scanf(“%lf”, &r_in); scanf(“%lf”, &r_out); return 0; } ecs30 WInter 2012 Lecture #05

  19. Scope { … } • { … } • {… { … {…}…} … {…}…} • { <declarations> <statements> } ecs30 WInter 2012 Lecture #05

  20. double r_in, r_out, the_area; get_input_paramenters(r_in, r_out); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(double in, out) { scanf(“%lf”, &in); scanf(“%lf”, &out); return 0; } ecs30 WInter 2012 Lecture #05

  21. double r_in, r_out, the_area; get_input_paramenters(&r_in, &r_out); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(double *in_ptr, *out_ptr) { scanf(“%lf”,in_ptr); scanf(“%lf”,out_ptr); return 0; } ecs30 WInter 2012 Lecture #05

  22. Reusability/Sharing, Readability/Debugging, Modularity Abstraction Single return value for now; We will address other ways later (in Chapter 6). Function vs. Macro ecs30 WInter 2012 Lecture #05

  23. Inputs and Outputs (parameters and return value) They are actually different!! ecs30 WInter 2012 Lecture #05

  24. 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 #05

  25. Execution Sequence/Control Flow 3 1 2 ecs30 WInter 2012 Lecture #05

  26. X n ecs30 WInter 2012 Lecture #05

  27. X n >>> x = x * 2; ecs30 WInter 2012 Lecture #05

  28. How about num_1 and num_2? num_1 X num_2 n Caller Callee When you make a function call, do they “SHARE” the memory boxes? In other words, 12 or 24 bytes allocated for these four variables? ecs30 WInter 2012 Lecture #05

  29. No, they don’t share. Calling means “copying the values!” ecs30 WInter 2012 Lecture #05

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

  31. num_1 X num_2 n ecs30 WInter 2012 Lecture #05

  32. 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 #05

More Related