280 likes | 399 Views
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
E N D
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
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
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
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
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
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
“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
*(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
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
num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 final review
#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
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
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
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
The end of a string ecs30 Winter 2012 final review
char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30 Winter 2012 final review
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
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
int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30 Winter 2012 final review
Recursion It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 final review
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
printf("%d\n", ((planet_t *) 0)->diameter); ecs30 Winter 2012 final review
#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
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
I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30 Winter 2012 final review
#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
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
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