460 likes | 593 Views
ecs30 Winter 2012: Programming and Problem Solving #12: Chapter 5~8 – from Loops to Arrays. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. We skip Chapter 7 for now… Chapter 8, Array!. Object. Object. Object. Object.
E N D
ecs30 Winter 2012:Programming and Problem Solving#12: Chapter 5~8 – from Loops to Arrays Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #12
We skip Chapter 7 for now… Chapter 8, Array! ecs30 Winter 2012 Lecture #12
Object ecs30 Winter 2012 Lecture #12
Object Object Object Object Object ecs30 Winter 2012 Lecture #12
Object: {0,1}* ecs30 Winter 2012 Lecture #12
Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* Object: {0,1}* ecs30 Winter 2012 Lecture #12
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
Declare the array Access the array Memory Cell models for the array ecs30 Winter 2012 Lecture #12
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
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
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
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
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
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
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
int num3D[8][6][4]; structchess_board piece[8][8]; ecs30 Winter 2012 Lecture #12
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
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
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
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
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
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
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
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
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
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
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
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
20 123 -39 53 111 0 -20 202 ecs30 Winter 2012 Lecture #12
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
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
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
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
20 202 123 20 -39 123 53 -39 111 53 0 111 202 0 -20 -20 ecs30 Winter 2012 Lecture #12
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
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
num[0] num[1] num[2] order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #12
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
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
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
#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
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
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
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
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