week 13 structures aaron tan. q1 what is the output? 2 #include typedef struct { int p; float q; }...

11
CS1010 Discussion Group C03 Week 13 Structures Aaron Tan

Upload: lee-perkins

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

CS1010 Discussion Group C03

Week 13Structures

Aaron Tan

Page 2: 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

Q1 What is the output?

2

#include <stdio.h>

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'

Page 3: 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

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 bs

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:

Page 4: 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

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 bs

*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

Page 5: 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

Q3 Tiles (1/4)

5

Read an integer (>1) indicating number of tiles, followed by tiles’ data (length, width, price per m2)

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: 5Enter data for 5 tiles:5 8 0.203 5 0.186 10 0.314 6 0.272 4 0.38Largest difference = $15.90

Page 6: 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

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);

Page 7: 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

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;

Page 8: 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

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(n2) comparisons, but this takes only O(n) comparisons.

Page 9: 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

Q6 Class Schedule

9

0 1000

10

30

40

60

80

100

150

160

170

180

200

210

230

240

260

280

Free period

Free period

Free period

Most concurrent

lessons

Approach 1: Using a timeline array

9200 240210 23030 6080 10010 40200 260260 280150 180160 170

Page 10: 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

Q6 Class Schedule

10

Approach 2: Using an array of endpoints

9200 240210 23030 6080 10010 40200 260260 280150 180160 170

typedef struct {int time;int type;

} endpoint_t;

#define START 1#define FINISH -1

Page 11: 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

11

End of file