1 / 28

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

mona-joyner
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. 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

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

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

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

  7. “Type” gives the semantic meaning of an address! #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)); printf(“planet_t = %x\n”, (((planet_t *) ptr_uchar) + 1)); /* pointer to function being casted – the following NOT in * the final */ 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))); } $ ./a.out char = bffffa09 int = bffffa0c func = 1f62 func = 1f63 size = 4 ecs30 Winter 2012 final review

  8. *(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

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

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

  11. #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

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

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

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

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

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

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

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

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

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

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

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

  23. #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

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

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

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

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

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

More Related