330 likes | 463 Views
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
E N D
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
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
Assignment ecs30 winter 2012 Lecture #04
Arithmetic expressions • +, -, *, /, % radius ecs30 winter 2012 Lecture #04
Arithmetic expressions • +, -, *, /, % ecs30 winter 2012 Lecture #04
Arithmetic expressions V = p2-p1/t2-t1; • +, -, *, /, % ecs30 winter 2012 Lecture #04
Mixed-Type Assignments int m, n; double p, x, y; ecs30 winter 2012 Lecture #04
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
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
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
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
Readability • x/y*z/w+a/b--c*-*d ecs30 winter 2012 Lecture #04
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
Arithmetic expressions • +, -, *, /, % radius ecs30 winter 2012 Lecture #04
{ … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30 winter 2012 Lecture #04
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
Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 winter 2012 Lecture #04
Abstraction ecs30 winter 2012 Lecture #04
double Find_area(doubler) { double area; area = r * r * PI; return area; } ecs30 winter 2012 Lecture #04
Inputs and Outputs (parameters and return value) ecs30 winter 2012 Lecture #04
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
Reusability/Sharing, Readability/Debugging, Modularity Abstraction Function vs. Macro ecs30 winter 2012 Lecture #04
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
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
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
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
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