1 / 33

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

ecs30 Summer 2014: Programming and Problem Solving # 04: Chapters 3-7. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Abstraction. Problem Solving. Thinking Digitally!!!. Variables Rules Inputs and Outputs.

davis
Télécharger la présentation

ecs30 Summer 2014: Programming and Problem Solving # 04: 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#04: 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. Abstraction ecs30 winter 2012 Lecture #04

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

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

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

  6. 4 8 ecs30 Winter 2012 Lecture #01

  7. 4 8 ecs30 Winter 2012 Lecture #01

  8. 4 8 ecs30 Winter 2012 Lecture #01

  9. 4 8 ecs30 Winter 2012 Lecture #01

  10. 4 8 ecs30 Winter 2012 Lecture #01

  11. X n ecs30 WInter 2012 Lecture #05

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

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

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

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

  16. num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11

  17. num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11

  18. Call by References &num_1 &x &num_2 &n num_1 scale(&num_1, &num_2); double scale(double *xp, int *np) {… (*xp) = (*xp) * (*np); } num_2 ecs30 Winter 2012 Lecture #11

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

  20. 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 #08

  21. #include <stdio.h> #include <stdlib.h> int main(void) { intx = 0; fprintf(stderr, "%d\n", (int) (&x)); printf("enterx\n"); scanf("%d", &x); printf("[04] %d\n", (int) (*((int *) x))); return 0; } ecs30 WInter 2012 Lecture #08

  22. #include <stdio.h> #include <stdlib.h> int main(void) { intx = 0; fprintf(stderr, "%d\n", (int) (&x)); printf("enterx\n"); scanf("%d", &x); printf("[04] %d\n", (int) (*((int *) x))); return 0; } Xp == &x *xp == x ecs30 WInter 2012 Lecture #08

  23. 0x00000222 ???? (X == 0x00000222) 0x00000222 0xbfcd1340 ecs30 WInter 2012 Lecture #08

  24. 0x00000222 ???? (X == 0xbfcd1340) 0xbfcd1340 0xbfcd1340 ecs30 WInter 2012 Lecture #08

  25. 0x00000222 ???? (X == 0xbfcd1340) 0xbfcd0000 0xbfcd1340 ecs30 WInter 2012 Lecture #08

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

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

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

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

  30. #include <stdio.h> int main(void) { int i,j; i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =i++;//(1) j = i;(2) i = i+1; printf("i = %d, j = %d\n", i,j); i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =++i;//(1) i = i+1;(2) j =i; printf("i = %d, j = %d\n", i,j); } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  31. for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) Print 7 times versus 6 times! ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  32. for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

  33. for (i = n; i > 1; --i) product *= i; for (i = 2; i <= n; ++i) product *= i; ecs30 WInter 2012 Lecture #09~10 (Roozbeh)

More Related