1 / 65

ecs30 Summer 2014: Programming and Problem Solving # 06: Chapters 3-8

ecs30 Summer 2014: Programming and Problem Solving # 06: Chapters 3-8. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. “sorting”. Input: three non-negative integers Output:

march
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving # 06: Chapters 3-8

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#06: Chapters 3-8 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. “sorting” Input: three non-negative integers Output: (1). [Input Validation]: If any of the inputs is negative, do nothing and return 1 in the main function. (2). [Sorting the Inputs after the Validation]: If the inputs are all valid (i.e., non-negative), your program will print the largest integer (among those three) first, then the middle number, then finally the smallest number, in three different lines. (BTW, this procedure is called SORTING.) After these outputs, return 0 in the main function. Please note that you are NOT allowed to use goto, loop or array to solve this problem, even if you know how. ecs30b Fall 2008 Lecture #10

  3. Declare the array Access the array Memory Cell models for the array ecs30 Winter 2012 Lecture #11

  4. Object: {0,1}* 8 bytes, such as Double Object: {0,1}* 4 bytes, such as Integer Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* ecs30 Winter 2012 Lecture #12

  5. Declare the array Access the array Memory Cell models for the array ecs30 Winter 2012 Lecture #12

  6. int num[8] num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] num[8] 20 123 -39 53 111 0 -20 202 291 ecs30 Winter 2012 Lecture #12

  7. int num[8] num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] num[8] 20 123 -39 53 111 0 -20 202 291 ecs30 Winter 2012 Lecture #12

  8. int num[8] num[-1] num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] num[8] 20 20 123 -39 53 111 0 -20 202 291 ecs30 Winter 2012 Lecture #12

  9. int main(void) { int num[3],i; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d%d%d",&(num[0]),&(num[1]),&(num[2]); // Ordering order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Results for (i=0; i<3; i++){fprintf(stdout,”%d\n",num[i]);} return 0; } ecs30 Winter 2012 Lecture #12

  10. int main(void) { int num[3],i; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d%d%d",&(num[0]),&(num[1]),&(num[2]); // Ordering order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Results for (i=0; i<3; i--){fprintf(stdout,”%d\n",num[i]);} return 0; } ecs30 Winter 2012 Lecture #12

  11. int num[8] num[-1] num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] num[8] 20 20 123 -39 53 111 0 -20 202 291 ecs30 Winter 2012 Lecture #12

  12. int num2D[8][6]; 20 13 -39 53 111 0 -20 22 120 123 39 3 121 0 -20 202 2 23 31 153 11 100 -20 212 -20 723 -3 -53 181 0 -20 202 2 13 91 53 1 -99 -20 22 20 103 -9 253 19 0 -20 202 ecs30 Winter 2012 Lecture #12

  13. int num3D[8][6][4]; structchess_board piece[8][8]; ecs30 Winter 2012 Lecture #12

  14. Declare the array <type> <array_name>[size]([]…[]); Access the array <array_name>[index] ([]…[]) Memory Cell models for the array Lower level implementation *(num+i) == num[i] m[x][y] ~ *(m + x*columns + y) ecs30 Winter 2012 Lecture #12

  15. Original int main(void) { int num[3],i; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d%d%d",&(num[0]),&(num[1]),&(num[2]); // Ordering order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Results for (i=0; i<3; i++){fprintf(stdout,”%d\n",num[i]);} return 0; } How to make it “loop”? ecs30 Winter 2012 Lecture #12

  16. int main(void) { int num[3],i,j; fprintf(stdout, "Enter the three numbers you want sorted:"); fscanf(stdin,"%d %d %d", &(num[0]),&(num[1]),&(num[2]); // Ordering for (i = 0; i < (3-1); i++) for (j = (3-1); j > i; j--) order(&(num[j]),&(num[j-1])); // Results for (i=0; i<3; i++){fprintf(stdout,”%d\n",num[i]);} return 0; } ecs30 Winter 2012 Lecture #12

  17. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #12

  18. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #12

  19. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #12

  20. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Ordering for (i = 0; i < 2; i++) for (j = 2; j > i; j--) order(&(num[j-1]),&(num[j])); (i == 0) && (j == 2) ecs30 Winter 2012 Lecture #12

  21. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Ordering for (i = 0; i < 2; i++) for (j = 2; j > i; j--) order(&(num[j-1]),&(num[j])); (i == 0) && (j == 1) ecs30 Winter 2012 Lecture #12

  22. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Ordering for (i = 0; i < 2; i++) for (j = 2; j > i; j--) order(&(num[j-1]),&(num[j])); (i == 0) && (j == 0) (i == 1) && (j == 2) ecs30 Winter 2012 Lecture #12

  23. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Ordering for (i = 0; i < 2; i++) for (j = 2; j > i; j--) order(&(num[j]),&(num[j-1])); (i == 0) && (j == 0) (i == 1) && (j == 2) (i == 1) && (j == 1) ecs30 Winter 2012 Lecture #12

  24. num[0] num[1] num[2] order(&(num[1]),&(num[2])); order(&(num[0]),&(num[1])); order(&(num[1]),&(num[2])); // Ordering for (i = 0; i < 2; i++) for (j = 2; j > i; j--) order(&(num[j]),&(num[j-1])); (i == 0) && (j == 0) (i == 1) && (j == 2) (i == 2) && (j == 1) ecs30 Winter 2012 Lecture #12

  25. num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] 20 123 -39 53 111 0 -20 202 ecs30 Winter 2012 Lecture #12

  26. 20 123 -39 53 111 0 -20 202 ecs30 Winter 2012 Lecture #12

  27. 20 20 20 123 123 123 -39 -39 -39 53 53 53 111 111 111 0 0 202 -20 202 0 202 -20 -20 ecs30 Winter 2012 Lecture #12

  28. 20 20 20 20 20 20 20 202 123 123 123 123 123 123 202 20 -39 -39 -39 -39 -39 202 123 123 53 53 53 53 202 -39 -39 -39 111 111 111 202 53 53 53 53 0 0 202 111 111 111 111 111 -20 202 0 0 0 0 0 0 202 -20 -20 -20 -20 -20 -20 -20 ecs30 Winter 2012 Lecture #12

  29. 20 20 20 20 20 20 20 202 123 123 123 123 123 123 202 20 -39 -39 -39 -39 -39 202 123 123 53 53 53 53 202 -39 -39 -39 111 111 111 202 53 53 53 53 0 0 202 111 111 111 111 111 -20 202 0 0 0 0 0 0 202 -20 -20 -20 -20 -20 -20 -20 ecs30 Winter 2012 Lecture #12

  30. 20 20 20 20 20 20 20 202 123 123 123 123 123 123 202 20 -39 -39 -39 -39 -39 202 123 123 53 53 53 53 202 -39 -39 -39 111 111 111 202 53 53 53 53 0 0 202 111 111 111 111 111 -20 202 0 0 0 0 0 0 202 -20 -20 -20 -20 -20 -20 -20 ecs30 Winter 2012 Lecture #12

  31. 20 202 123 20 -39 123 53 -39 111 53 0 111 202 0 -20 -20 ecs30 Winter 2012 Lecture #12

  32. num[0] num[1] num[2] // Ordering for (i = 0; i < (N-1); i++) for (j = (N-1); j > i; j--) order(&(num[j-1]),&(num[j])); ecs30 Winter 2012 Lecture #12

  33. ecs30 Winter 2012 Lecture #12

  34. int num[N]; … // General Ordering for (i = 0; i < (N-1); i++) for (j = (N-1); j > i; j--) order(&(num[j]),&(num[j-1])); ecs30 Winter 2012 Lecture #12

  35. num[0] num[1] num[2] order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #12

  36. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) int num[N]; printf(“%d\n”, num[i]); // 0<= i < N printf(“%x\n”, num); What will we get? ecs30 Winter 2012 Lecture #12

  37. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[N]; printf(“%d\n”, num[i]); printf(“%x\n”, num); What will we get? num[1] num[2] ecs30 Winter 2012 Lecture #12

  38. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[N]; printf(“%x\n”, num); What will we get? num[1] num[2] ecs30 Winter 2012 Lecture #12

  39. #include <stdio.h> int main(void) { int rc; int num[3]; num[0] = 107; num[-20005675] = 105; printf("%x\n", num[0]); printf("%x\n", (unsigned int) *num); printf("%x\n", (unsigned int) &(num[0])); printf("%x\n", (unsigned int) &(num[1])); printf("%x\n", (unsigned int) &(num[2])); printf("%x\n", (unsigned int) &(num[-1])); printf("%x\n", (unsigned int) &(num[3])); printf("%x\n", (unsigned int) &num); printf("%x\n", (unsigned int) num); printf("%d\n", num[-20005675]); return 0; } ecs30 Winter 2012 Lecture #12

  40. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[N]; printf(“%x\n”, num); num[1] Equivalence between int num[]; and int *num; num[2] ecs30 Winter 2012 Lecture #12

  41. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[N]; int *ap; printf(“%x\n”, num); ap = num; printf(“%x\n”, ap); num[1] num[2] ecs30 Winter 2012 Lecture #12

  42. num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) void xyz(int *ap) {… printf(“%d\n”, ap[i]); } int main(void) { int num[N]; xyz(num); } num[0] num[1] num[2] ecs30 Winter 2012 Lecture #12

  43. void xyz(int *ap) {… printf(“%d\n”, ap[i]); } int main(void) { int num[N]; xyz(num); } void xyz(int *ap) {… printf(“%d\n”, *(ap + i)); } int main(void) { int num[N]; xyz(num); } ecs30 Winter 2012 Lecture #12

  44. Game of Life ecs30 Winter 2012 Lecture #14

  45. Pointer in C • Pointer in C is a value (32 bits, e.g.), and it can be mixed with integer in computing. • Pointer in C is the address • Furthermore, it is the address of the first byte of the data item (it is pointing to) ecs30 Winter 2012 Lecture #14

  46. void xyz(int *ap) {… printf(“%d\n”, ap[i]); } int main(void) { int num[N]; xyz(num); } void xyz(int *ap) {… printf(“%d\n”, *(ap + i)); } int main(void) { int num[N]; xyz(num); } ecs30 Winter 2012 Lecture #14

  47. int num2D[8][6]; 20 13 -39 53 111 0 -20 22 120 123 39 3 121 0 -20 202 2 23 31 153 11 100 -20 212 -20 723 -3 -53 181 0 -20 202 2 13 91 53 1 -99 -20 22 20 103 -9 253 19 0 -20 202 ecs30 Winter 2012 Lecture #14

  48. ecs30 Winter 2012 Lecture #14

  49. 0,0 0,1 0,2 0,3 int I_array[4][4]; char C_array[4][4]; 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 void printMatrix (char m[][]) { m[2][1] … } 3,0 3,1 3,2 3,3 I_array == &(I_array[0][0]) m[x][y] ~ *((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #14

  50. 0,0 0,1 0,2 0,3 char m[4][4]; 1,0 1,1 1,2 1,3 void printMatrix (char m[][4]) { m[2][1] … } 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 m == &(m[0][0]) m[x][y] ~ *((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #14

More Related