1 / 33

ecs30 Winter 2012: Programming and Problem Solving # 04: Chapters 2 ~ 4

ecs30 Winter 2012: Programming and Problem Solving # 04: Chapters 2 ~ 4. 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

hao
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 04: Chapters 2 ~ 4

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#04: Chapters 2~4 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 #04

  2. ecs30 winter 2012 Lecture #04

  3. ecs30 winter 2012 Lecture #04

  4. 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 #04

  5. Assignment ecs30 winter 2012 Lecture #04

  6. Arithmetic expressions • +, -, *, /, % radius ecs30 winter 2012 Lecture #04

  7. Arithmetic expressions • +, -, *, /, % ecs30 winter 2012 Lecture #04

  8. Arithmetic expressions V = p2-p1/t2-t1; • +, -, *, /, % ecs30 winter 2012 Lecture #04

  9. Mixed-Type Assignments int m, n; double p, x, y; ecs30 winter 2012 Lecture #04

  10. Mixed-Type Assignments int m, n; double p, x, y; m = 3; n = 2; p = 2.0; x = m / p; y = m / n; printf(“x = %lf, y = %lf\n”, x,y); What is the root cause? ecs30 winter 2012 Lecture #04

  11. Mixed-Type Assignments int m, n; double p, x, y; m = 3; n = 2; p = 2.0; x = m / p; y = m / n; printf(“x = %lf, y = %lf\n”, x,y); What is the root cause? The expression was evaluated BEFORE the assignment!! ecs30 winter 2012 Lecture #04

  12. Use “Casting” int m, n; double p, x, y; m = 3; n = 2; p = 2.0; x = m / p; y = (double) m / (double) n; printf(“x = %lf, y = %lf\n”, x,y); ecs30 winter 2012 Lecture #04

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

  14. Readability • x/y*z/w+a/b--c*-*d ecs30 winter 2012 Lecture #04

  15. 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 #04

  16. ecs30 winter 2012 Lecture #04

  17. Arithmetic expressions • +, -, *, /, % radius ecs30 winter 2012 Lecture #04

  18. { … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30 winter 2012 Lecture #04

  19. ecs30 winter 2012 Lecture #04

  20. Figure 3-4: #definePI3.1415926 #define Area 3.14 * R_big * R_big /* get the radius of bigger circle R_big */ /* get the radius of bigger circle R_inner*/ A = pi * r * r; A = 3.1415926 * r * r; ecs30 winter 2012 Lecture #04

  21. ecs30 winter 2012 Lecture #04

  22. Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 winter 2012 Lecture #04

  23. ecs30 winter 2012 Lecture #04

  24. Abstraction ecs30 winter 2012 Lecture #04

  25. double Find_area(doubler) { double area; area = r * r * PI; return area; } ecs30 winter 2012 Lecture #04

  26. Inputs and Outputs (parameters and return value) ecs30 winter 2012 Lecture #04

  27. 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 #04

  28. Reusability/Sharing, Readability/Debugging, Modularity Abstraction Function vs. Macro ecs30 winter 2012 Lecture #04

  29. 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 #04

  30. 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 #04

  31. 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 #04

  32. trouble.c (hw#2)(try to control the input) #include <stdio.h> int main(void) { int x; printf("%x\n", &x); printf("%d\n", (int) &x); scanf("%d", &x); printf("[04] %d\n", (int) *((int *) x)); } What would be the valid input values for x such that the program won’t crash? ecs30 winter 2012 Lecture #04

  33. Memory Faults(a logical/run-time error) • Not all memory addresses can be accessed by “pointers” (or “addresses”) in C • In fact, more often, a simple logical error will trigger unexpectedly such violations: • bus error • segmentation fault • Advanced topic: what is the difference? • How to debug? (e.g., gdb) ecs30 winter 2012 Lecture #04

More Related