1 / 41

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

ecs30 Winter 2012: Programming and Problem Solving # 06: Chapters 2~ 5. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Reusability/Sharing, Readability/Debugging, Modularity. Abstraction. Function vs. Macro.

malini
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 06: 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#06: 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 #06

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

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

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

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

  6. 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 #06

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

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

  9. 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 #06

  10. intx; (int *) xp = &x; x = 222; x, &x, *xp; ecs30 WInter 2012 Lecture #06

  11. intx; int * xp = &x; x = 222; x, &x, *xp; /*???*/ ecs30 WInter 2012 Lecture #06

  12. int* xp; <type> * <variable name>; I declare a variable xpsuch that xpis an address containing a integer-type object. address xp &xp xp T(int) = 222 x (*xp) == 222; ecs30 WInter 2012 Lecture #06

  13. int* xp; <type> * <variable name>; I declare a variable xpsuch that xpis an address containing a integer-type object. 34ef5f12 xp &xp xp or &x == 0x34ef5f12 T(int) = 222 x x == 222 *xp == 222 ecs30 WInter 2012 Lecture #06

  14. T = (int *) * T* x; <type> * <variable name>; I declare a variable x such that x is an address containing a T-type object. xp = &x; 34ef5f12 xp &xp &x == 0x34ef5f12 T(int) = 222 x x == 222 *xp == 222 ecs30 WInter 2012 Lecture #06

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

  16. #include <stdio.h> #include <stdlib.h> #define PI 3.1415926 intmain(void) { double inner, outer, area; /* inputs of radius */ printf("pleaseinout the inner:"); scanf("%lf", &inner); printf("pleaseinout the outer:"); scanf("%lf", &outer); Compare_and_swap(&inner, &outer); /* calculate the area */ area = outer * outer * PI - inner * inner * PI; /* print the result */ printf("area = %f\n", area); return 0; } ecs30 WInter 2012 Lecture #06

  17. What is the “condition”? ecs30 WInter 2012 Lecture #06

  18. OR, AND, XOR X Y (X || Y) (X && Y) (X Y) T T T T F T F T F T F T T F T F F F F F ecs30 WInter 2012 Lecture #06

  19. Brainstorming… What is this program segment doing? Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06

  20. void Compare_and_swap(double *xp, *yp) { double temp; if (*xp > *yp) { temp = *yp; *yp = *xp; *xp = temp; } } ecs30 WInter 2012 Lecture #06

  21. ecs30 WInter 2012 Lecture #06

  22. if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06

  23. printf and fprintf printf("Name: %s\n", yourname); fprintf(stdout, "Name: %s\n", yourname); %./a.out > out.xyz fprintf(stderr, "Name: %s\n", yourname); ecs30 WInter 2012 Lecture #06

  24. “conditional” flow control ecs30 WInter 2012 Lecture #06

  25. ecs30 WInter 2012 Lecture #06

  26. ecs30 WInter 2012 Lecture #06

  27. Conditional Swapping Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06

  28. if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06

  29. T/F if (x == 0) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) ecs30 WInter 2012 Lecture #06

  30. T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) • ONLY (x == 1) (0 for 32 bits) 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06

  31. T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? if (x) {// TRUE} else {//FALSE} 00000010 00010000 00000000 00000000 !(00000000 00000000 00000000 00000000) if (x = 0) {// TRUE} else {//FALSE} (00000000 00000000 00000000 00000000) Always false! ecs30 WInter 2012 Lecture #06

  32. Logical Negation if (!x) {// TRUE} else {//FALSE} • What should be the value of x? if (x == 0) {// TRUE} else {//FALSE} ecs30 WInter 2012 Lecture #06

  33. Logical/Bitwise • Logical: (if statements) • X = {false or true} {zero or otherwise} • Bitwise: we operate on every bit! 00000000 00000000 00000000 00000001 & 10101010 00000000 00000000 10101011 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06

  34. Special Symbols(Appendix C) Logical • ||, &&, >, <, >=, <=, ==, != • ! Bitwise • |, &, ^, <<, >>, ~ ecs30 WInter 2012 Lecture #06

  35. if and ? printf(“%d\n”, (x>y) ? x : y); if (x > y) { printf(“%d\n”, x); } else { printf(“%d\n”, y); } ecs30 WInter 2012 Lecture #06

  36. Brainstorming… ecs30 WInter 2012 Lecture #06

  37. Brainstorming… if ((x>= min) && (x <= max)) { // TRUE } ecs30 WInter 2012 Lecture #06

  38. Brainstorming… ecs30 WInter 2012 Lecture #06

  39. Brainstorming… if ((x < z) || (x > y)) { // TRUE } ecs30 WInter 2012 Lecture #06

  40. char class; switch(class) { case ‘B’: case ‘b’: printf(“Battleship\n”); break; case ‘C’: case ‘C’: printf(“Cruiser\n”); break; default: printf(“Unknown class %c\n”, class); } ecs30 WInter 2012 Lecture #06

  41. int class; switch(class) { case 0: case 1: printf(“Battleship\n”); break; case 2: printf(“Cruiser\n”); break; default: printf(“Unknown class %d\n”, class); } ecs30 WInter 2012 Lecture #06

More Related