1 / 49

ecs30 Winter 2012: Programming and Problem Solving Final Review

ecs30 Winter 2012: Programming and Problem Solving Final Review. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Final. About 50~75% from the two midterms Similar concepts, more mixings

lin
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving Final Review

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 Winter 2012:Programming and Problem SolvingFinal Review Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 final review

  2. Final • About 50~75% from the two midterms • Similar concepts, more mixings • Everything should be within “slides” • But, with extensions • Chapters 2~6, 8~12, 14 plus slides • Please, after the quarter, read 7 & 13. • Focusing on Problem solving in C! ecs30 Winter 2012 final review

  3. ecs30 Winter 2012 final review

  4. 0 = 0000 = 0 * 8 + 0 * 4 + 0 * 2 + 0 * 1 1 = 0001 = 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 2 = 0010 = 0 * 8 + 0 * 4 + 1 * 2 + 0 * 1 3 = 0011 = 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1 ....... 7 = 0111 = 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 8 = 1000 = 1 * 8 + 0 * 4 + 0 * 2 + 0 * 1 9 = 1001 = 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1 10 (a) = 1010 = 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 11 (b) = 1011 = 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 12 (c) = 1100 = 1 * 8 + 1 * 4 + 0 * 2 + 0 * 1 13 (d) = 1101 = 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 14 (e) = 1110 = 1 * 8 + 1 * 4 + 1 * 2 + 0 * 1 15 (f) = 1111 = 1 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Examples: 09FE (in Hex) == ? (in Binary) == ? (in Decimal) ecs30 Winter 2012 final review

  5. T * = (int) * T* xp; <type> * <variable name>; I declare a variable xp such that xp is an address containing a T-type object. address xp &xp x T(int) = 222 x (*xp) == 222; ecs30 Winter 2012 final review

  6. T * = (int) * T* xp; <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 final review

  7. printf(“%lf\n”, &miles); printf(“%lf\n”, miles); printf(“%lf\n”, 0x7014ae00); printf(“%lf\n”, 30.5); printf(“%lf\n”, *(&miles)); printf(“%lf\n”, *((double *) 0x7014ae00); 8 bytes 0x 7014ae00 0x 7014ae01 0x 7014ae02 0x 7014ae03 0x 7014ae04 0x 7014ae05 0x 7014ae06 0x 7014ae07 In C, Pointer = Address + Type! And, Address is merely a 32 bit unsigned integer! ecs30 Winter 2012 final review

  8. “Type” gives the semantic meaning of an address! $ ./a.out char = bffffa09 int = bffffa0c func = 1f62 func = 1f63 size = 4 #include <stdio.h> #include <stdlib.h> int main (void) { intx = 222; unsigned char*ptr_uchar = (unsigned char *)&x; printf(“char = %x\n”, (((char *) ptr_uchar) + 1)); printf(“int = %x\n”, (((int *) ptr_uchar) + 1)); /* pointer to function being casted */ ptr_uchar=(unsigned char *) main; printf(“func = %x\n”, (((int (*)(void)) ptr_uchar))); printf(“func = %x\n”, (((int (*)(void)) ptr_uchar) + 1)); printf(“size = %d\n”, sizeof(int (*)(void))); } ecs30 Winter 2012 final review

  9. *(ap + i) is the actual implementation of ap[i]! 0x000000de ?? 0x000000df • int x = 222; • &x • *x • *(&x) • *(((char *) ap) + 1) • *(((int *) ap) + 1) • *(((platnet_t *) ap) + 1) 0x000000e0 0x000000e1 0xbffffa08 222 0xbffffa09 0xbffffa0a 0xbffffa0b 0xbffffa0c ecs30 Winter 2012 final review

  10. addition ((type *) xp + k) == The value of address xp + sizeof(type) * k (int **) ~ ((int *) *) ~ type == (int *) ((type *) xp + k) == The value of address xp + sizeof((int *)) * k ecs30 Winter 2012 final review

  11. 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)); } What would be the valid input values for x such that the program won’t crash? ecs30 Winter 2012 final review

  12. num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 final review

  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 final review

  14. 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 final review

  15. 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 final review

  16. 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 final review

  17. 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 final review

  18. m[x][y] ~ *((char *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(char) m[x][y] ~ *((int *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(int) ecs30 Winter 2012 final review

  19. The end of a string ecs30 Winter 2012 final review

  20. 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 final review

  21. #include <stdio.h> #include <string.h> int main(void) { char dest_s[20]; char src_s[20] = “Jan. 30, 1996”; strncpy(dest_s, src_s, 9); fprintf(stdout, “%s\n”, dest_s); return 0; } ecs30 Winter 2012 final review

  22. #include <stdio.h> #include <string.h> int main(void) { char dest_s[20]; char src_s[20] = “Jan. 30, 1996”; fprintf(stdout, “%s\n”, strncpy(dest_s, src_s, 9)); return 0; } ecs30 Winter 2012 final review

  23. ‘U’ ‘C’ ‘ ’ ‘\0’ ? ? ? ? ? s1 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘U’ ‘C’ ‘ ’ ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ s1 strcat(s1, s2); ecs30 Winter 2012 final review

  24. #include <stdio.h> #include <string.h> int main(void) { char s1[9] = “UC “; char s2[9] = “Davis”; fprintf(stdout, “%d\n”, strcmp(s1, s2)); return 0; } ecs30 Winter 2012 final review

  25. #include <stdio.h> #include <string.h> int strcmp(char *s1, char *s2) { } ecs30 Winter 2012 final review

  26. Strings • strcpy, strncpy, strdup, strcmp ecs30 Winter 2012 final review

  27. char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30 Winter 2012 final review

  28. malloc & free • malloc: • Input: the size of the memory cells • return: the address of the first memory cell #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); ecs30 Winter 2012 final review

  29. malloc & free • free: • Input: an address • return: nothing #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); free(xp); ecs30 Winter 2012 final review

  30. int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30 Winter 2012 final review

  31. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); ecs30 Winter 2012 final review

  32. Recursion It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 final review

  33. tower3p Big ==8 Small == 1 ecs30 Winter 2012 final review

  34. from to Big == 5 Small == 1 Big == 8 Small == 6 ecs30 Winter 2012 final review

  35. Logic • Move the top part using 4 pegs • From > Aux2 • Move the bottom part using 3 pegs • From > To • Move the top part using 4 pegs • Aux2 > To ecs30 Winter 2012 final review

  36. Exit Condition ecs30 Winter 2012 final review

  37. int movecount = 1; void tower3p (char from_peg, char to_peg, char aux_peg, int big, int small) { if (big == small) { fprintf(stdout, "[%16d] ==> move disk %3d from %c to %c\n", movecount++, big, from_peg, to_peg); } else { tower3p(from_peg, aux_peg, to_peg, big-1, small); fprintf(stdout, "[%16d] ==> move disk %3d from %c to %c\n", movecount++, big, from_peg, to_peg); tower3p(aux_peg, to_peg, from_peg, big-1, small); } } ecs30 Winter 2012 final review

  38. #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet *next; struct planet *previous; }; sizeof(struct planet) == ? ecs30 Winter 2012 final review

  39. printf("%d\n", ((planet_t *) 0)->diameter); ecs30 Winter 2012 final review

  40. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", (int) &(((planet_t *) 0)->name)); printf("%d\n", (int) &(((planet_t *) 0)->diameter)); printf("%d\n", (int) &(((planet_t *) 0)->moons)); Expect: 0, 10, 18 MacBook/pc16 (i386): 0, 12, 20 pc26 (x86_64): 0, 16, 24 ecs30 Winter 2012 final review

  41. typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; &((planet_t *) 0)->diameter) “Be very careful! It’s Machine-Dependent actually!!” ecs30 Winter 2012 final review

  42. I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30 Winter 2012 final review

  43. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet[12]; fwrite(current_planet, sizeof(planet_t), 12, planetFile); fread(current_planet, sizeof(planet_t), 12, planetFile); ecs30 Winter 2012 final review

  44. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; } planet_t; sizeof(planet_t) == ? ecs30 Winter 2012 final review

  45. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; char tail2; } planet_t; sizeof(planet_t) == ? ecs30 Winter 2012 final review

  46. fwrite/fread Table 12.5 Pages 625~626 • format versus binary #include <stdio.h> size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); ecs30 Winter 2012 final review

  47. 2 1 byte File 1 byte 3 fopen(,) 1 FILE * 4 ecs30 Winter 2012 final review

  48. /usr/include/stdio.h typedef struct __sFILE { unsigned char *_p; /* current position in (some) buffer */ int _r; /* read space left for getc() */ int _w; /* write space left for putc() */ short _flags; /* flags, below; this FILE is free if 0 */ short _file; /* fileno, if Unix descriptor, else -1 */ struct __sbuf _bf;/* the buffer (at least 1 byte, if !NULL) */ int _lbfsize; /* 0 or -_bf._size, for inline putc */ /* operations */ void *_cookie; /* cookie passed to io functions */ int (*_close)(void *); int (*_read) (void *, char *, int); fpos_t (*_seek) (void *, fpos_t, int); int (*_write)(void *, const char *, int); /* separate buffer for long sequences of ungetc() */ struct __sbuf _ub; /* ungetc buffer */ … } FILE; ecs30 Winter 2012 final review

  49. ecs30 Winter 2012 final review

More Related