Week 13 Structures Aaron Tan
Q1 What is the output? 2 #include typedef struct { int p; float q; } one_t; typedef struct { int p; float q; } two_t; int main(void) { one_t one = { 1, 2.3 }; two_t two; two = one; printf("%d %f\n", one.p, one.q); printf("%d %f\n", two.p, two.q); return 0; } Compilation error! Incompatible types when assigning to type 'two_t' from type 'one_t'
Q2a Which statements are correct? 3 typedef struct { int a; float b; } s_t; s_t s; i.scanf("%d", &(s.a)); ii.scanf("%d", s.&a); iii.scanf("%d", (&s).a); iv.scanf("%d", &s.a); a b s Target = s.a Hence, address of target is &(s.a) Since dot operator ‘.’ has higher precedence than address operator ‘&’, parentheses can be omitted: &s.a To read a value into member a of variable s :
Q2b Which statements are correct? 4 typedef struct { int a; float b; } s_t; s_t s; s_t *p; p = &s; i.scanf("%d", &p.a); ii.scanf("%d", &(p.a)); iii.scanf("%d", *p.a); iv.scanf("%d", p.a); v.scanf("%d", (*p).a); vi.scanf("%d", p->a); vii.scanf("%d", &(*p).a); viii.scanf("%d", &(*p.a)); ix.scanf("%d", &(p->a)); a b s *p is equivalent to s, hence target = (*p).a [Target is not *p.a because dot ‘.’ has higher precedence than ‘*’, hence *p.a is *(p.a)] Hence, address of target is &((*p).a) or &(*p).a Since (*p).a is equivalent to p->a, hence &((*p).a) is equivalent to &(p->a) To read a value into member a of variable s through p : p
Q3 Tiles (1/4) 5 Read an integer (>1) indicating number of tiles, followed by tiles’ data (length, width, price per m 2 ) Compute and output the difference in cost between the cheapest and most expensive tile. int scan_tiles(tile_t tiles[]); float difference(tile_t tiles[], int size); Enter number of tiles: 5 Enter data for 5 tiles: Largest difference = $15.90 Enter number of tiles: 5 Enter data for 5 tiles: Largest difference = $15.90
Q3 Tiles (2/4) 6 int scan_tiles(tile_t []); float difference(tile_t [], int); // Define tile_t below int main(void) { tile_t tiles[MAX_TILES]; int num_tiles; printf("Largest difference = $%.2f\n", difference(tiles, num_tiles)); return 0; } typedef struct { int length, width; float price; } tile_t; num_tiles = scan_tiles(tiles);
Q3 Tiles (3/4) 7 // To read tiles' data into array tiles // Return the number of tiles read int scan_tiles(tile_t tiles[]) { int num_tiles; printf("Enter number of tiles: "); printf("Enter data for %d tiles:\n", num_tiles); } int i; scanf("%d", &num_tiles); for (i=0; i<num_tiles; i++) { scanf("%d %d %f", &tiles[i].length, &tiles[i].width, &tiles[i].price); } return num_tiles;
Q3 Tiles (4/4) 8 // Return the difference in cost between // the cheapest tile and the most expensive tile. float difference(tile_t tiles[], int size) { } int i; float cost, min_cost, max_cost; min_cost = max_cost = tiles[0].length * tiles[0].width * tiles[0].price; for (i=1; i<size; i++) { cost = tiles[i].length * tiles[i].width * tiles[i].price; if (cost < min_cost) min_cost = cost; if (cost > max_cost) max_cost = cost; } return max_cost - min_cost; Do you need to sort? No, sorting takes O(n 2 ) comparisons, but this takes only O(n) comparisons.
Q6 Class Schedule Free period Most concurrent lessons Approach 1: Using a timeline array
Q6 Class Schedule 10 Approach 2: Using an array of endpoints typedef struct { int time; int type; } endpoint_t; #define START 1 #define FINISH -1
11 End of file