430 likes | 559 Views
ecs30 Winter 2012: Programming and Problem Solving # 13: Chapter 5~9 – 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!. int num[8]. num[-1]. num[0].
E N D
ecs30 Winter 2012:Programming and Problem Solving#13: Chapter 5~9 – 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 #13
We skip Chapter 7 for now… Chapter 8, Array! ecs30 Winter 2012 Lecture #13
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 #13
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 #13
int num3D[8][6][4]; structchess_board piece[8][8]; ecs30 Winter 2012 Lecture #13
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 #13
num[0] num[1] num[2] order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #13
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 #13
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 #13
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 #13
#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 #13
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 #13
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 #13
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 #13
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 #13
*(ap + i) is the actual implementation of ap[i]! 0x000000de ?? 0x000000df • int x = 222; • &x • *x • *(&x) • *(((char *) &x) + 1) • *(((int *) &x) + 1) 0x000000e0 0x000000e1 0xbffffa08 222 0xbffffa09 0xbffffa0a 0xbffffa0b 0xbffffa0c ecs30 Winter 2012 Lecture #13
*(ap + i) is the actual implementation of ap[i]! 0x000000de ?? 0x000000df • int x = 222; • &x • *x • *(&x) • *(((char *) ap) + 1) • *(((int *) ap) + 1) 0x000000e0 0x000000e1 0xbffffa08 222 0xbffffa09 0xbffffa0a 0xbffffa0b 0xbffffa0c ecs30 Winter 2012 Lecture #13
(int *) nump[N]; (int **) nump; (int *) * nump; num[2] num[1] num[0] nump[0] nump[1] nump[2] (4 bytes) (4 bytes) (4 bytes) ecs30 Winter 2012 Lecture #13
int *nump[N]; int **nump; num[1] num[0,2] nump[0] nump[1] nump[2] (4 bytes) (4 bytes) (4 bytes) ecs30 Winter 2012 Lecture #13
num_1 int yyy; Main() { j = 5; // Ordering for (i = 0; i < 2; i++) { static int j; for (j = 2; j > i; j--) order(&(num[j]),&(num[j-1])); } } ecs30 Winter 2012 Lecture #13
You can extend this to solve a general sorting problem! 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 < 2; i++) for (j = 2; j > i; j--) order(&(num[j]),&(num[j-1])); // Results for (i=0; i<3; i++){fprintf(stdout,”%d\n",num[i]);} return 0; } Array Loop Call-by-Reference ecs30 Winter 2012 Lecture #13
Write a program!! ecs30 Winter 2012 Lecture #13
num[0] num[1] num[2] order(&(num[1]),&(num[2])); ecs30 Winter 2012 Lecture #13
num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) int num[3]; printf(“%x\n”, num); What will we get? ecs30 Winter 2012 Lecture #13
num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[3]; printf(“%x\n”, num); What will we get? num[1] num[2] ecs30 Winter 2012 Lecture #13
num[0] num[1] num[2] (4 bytes) (4 bytes) (4 bytes) num[0] int num[3]; printf(“%x\n”, num); What will we get? num[1] num[2] ecs30 Winter 2012 Lecture #13
*(ap + i) is the actual implementation of ap[i]! 0x000000de ?? 0x000000df • int x = 222; • &x • *x • *(&x) • *(((char *) ap) + 1) • *(((int *) ap) + 1) 0x000000e0 0x000000e1 0xbffffa08 222 0xbffffa09 0xbffffa0a 0xbffffa0b 0xbffffa0c ecs30 Winter 2012 Lecture #13
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 #13
Hangman • How to enter a word/string? • How long is your string? • Then, we know where to stop! ecs30 Winter 2012 Lecture #13
Hangman • How to enter a word/string? • How long is your string? • Then, we know where to stop! • A library of functions to handle “strings” easier! ecs30 Winter 2012 Lecture #13
The end of a string Dept[4] = 0; Dept[4] = ‘\0’; ecs30 Winter 2012 Lecture #13
strings #define ERR_PREFIX “*****Error - “ char string_var[30] = ERR_PREFIX; char string_var[30] = “*****Error - “; fprintf(stderr, “%s Wrong File Name\n”, string_var); fprintf(stderr, “%s Wrong File Name\n”, ERR_PREFIX); fprintf(stderr, “%s Wrong File Name\n”, “*****Error -”); ecs30 Winter 2012 Lecture #13
strings char string_var[30] = “*****Error - “; fprintf(stderr, “%s Wrong File Name\n”, string_var); * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 ?? … \42 \42 \42 \42 \42 \69 \114 \114 \111 \114 \32 \45 \32 \0 ?? … string_var ecs30 Winter 2012 Lecture #13
strings char string_var[30] = “*****Error - “; fprintf(stderr, “%s Wrong File Name\n”, string_var); * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 ?? … 2a 2a 2a 2a 2a 45 72 72 6f 72 20 2d 20 00 ?? … string_var ecs30 Winter 2012 Lecture #13
strings char string_var[30] = “*****Error - “; fprintf(stderr, “%s Wrong File Name\n”, string_var); bzero(string_var, 30); \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 … string_var ecs30 Winter 2012 Lecture #13
strings char string_var[30] = “*****Error - “; char string_var[] = “*****Error - “; fprintf(stderr, “%s Wrong File Name\n”, string_var); * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 ?? … * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
intstrlen(char * s); int strlen(char *s) { // } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break;} return i; } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break;} return i; } What is/are the problem(s) here? fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break; i++;} return i; } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (1){if(s[i++] == ‘\0’) break;} return (i-1); } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (s[i++] != ‘\0’); return (i-1); } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13
int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break;} return i; } What are the problems here? fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #13