1 / 63

ecs30 Summer 2014: Programming and Problem Solving # 03: Chapters 3-7

ecs30 Summer 2014: Programming and Problem Solving # 03: Chapters 3-7. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Review. Programs: kms and hello Program, Programming Languag , and Compiler

mort
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving # 03: Chapters 3-7

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 Summer 2014:Programming and Problem Solving#03: Chapters 3-7 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 #01

  2. Review • Programs: kms and hello • Program, Programming Languag, and Compiler • X, &X, *X in C • Binary representation • Arithmetic Expressions • Mix-types Assignments • Function Arguments and Return Type/Value ecs30 Winter 2012 Lecture #01

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

  4. 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) (*-x)) • Binary: left to right (e.g., (x / y * z)) ecs30 winter 2012 Lecture #04

  5. Abstraction ecs30 winter 2012 Lecture #04

  6. Call by value Call by reference Call by name Figure 3-4: #define PI 3.1415926 #define Area 3.14 * R_big * R_big // gcc -E /* 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

  7. 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

  8. 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)); } 32 bits versus 64 bits sizeof uname -m What would be the valid input values for x such that the program won’t crash? ecs30 winter 2012 Lecture #04

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

  10. ecs30 winter 2012 Lecture #04

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

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

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

  14. Problem Solving ecs30 Winter 2012 Lecture #01

  15. Problem Solving ecs30 Winter 2012 Lecture #01

  16. Problem Solving Thinking Digitally!!! Variables Rules Inputs and Outputs ecs30 Winter 2012 Lecture #01

  17. Variables: 16 tiles T00, T01, T02, T03 T10, T11, T12, T13 T20, T21, T22, T23 T30, T31, T32, T33 ecs30 Winter 2012 Lecture #01

  18. Rules The rule of Shifting Up, Down, Left, Right Shift until no more empty tiles and no collapse The rule of Collapse If a tile hits another tile with equal value during a shift action The rule of “Game Over” ecs30 Winter 2012 Lecture #01

  19. How about Inputs? ecs30 Winter 2012 Lecture #01

  20. How about Inputs? New Tile Generation User Input Action ecs30 Winter 2012 Lecture #01

  21. Problem Solving Thinking Digitally!!! Variables Rules Inputs and Outputs ecs30 Winter 2012 Lecture #01

  22. void printRightShift(int x0, int x1, int x2, int x3); ecs30 Winter 2012 Lecture #01

  23. void printRightShift(int x0, int x1, int x2, int x3); 4 8 4 8 ecs30 Winter 2012 Lecture #01

  24. void printRightShift(int x0, int x1, int x2, int x3); 8 8 16 ecs30 Winter 2012 Lecture #01

  25. void printRightShift(int x0, int x1, int x2, int x3) { … printf(…); return; } ecs30 Winter 2012 Lecture #01

  26. 4 8 ecs30 Winter 2012 Lecture #01

  27. 4 8 ecs30 Winter 2012 Lecture #01

  28. 4 8 ecs30 Winter 2012 Lecture #01

  29. 4 8 ecs30 Winter 2012 Lecture #01

  30. 4 8 ecs30 Winter 2012 Lecture #01

  31. intCompareShiftCollapse(int x, int y); ecs30 Winter 2012 Lecture #01

  32. void printRightShift(int x0, int x1, int x2, int x3) { … CompareShiftCollapse(x0, x1); CompareShiftCollapse(x1, x2); CompareShiftCollapse(x2, x3); printf(…); return; } ecs30 Winter 2012 Lecture #01

  33. 4 4 4 4 8 8 4 ecs30 Winter 2012 Lecture #01

  34. ecs30 Winter 2012 Lecture #01

  35. Problem Solving 210 29 28 27 23 24 25 26 22 24 22 21 ecs30 Winter 2012 Lecture #01 ecs30 Winter 2012 Lecture #01 35

  36. #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

  37. decision F T if (condition) { <statement> <statement> (optional) } else { <statement> <statement> (optional) } <other statements> ecs30 WInter 2012 Lecture #07

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

  39. 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

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

  41. void Compare_and_swap(double x, y) { double temp; if (x > y) { temp = y; y = x; x = temp; } } ecs30 WInter 2012 Lecture #06

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

  43. 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

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

  45. X n ecs30 WInter 2012 Lecture #05

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

  47. 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

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

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

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

More Related