case study: focus on structures math 130 lecture 21 b smith: 10/04: required 35 minutes to complete....

15
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing the surveys. Good examples here B Smith: Conisder skipping? No. There are good examples of use of structures, modulus, arrays of structures, pointers to struct. B Smith: Alex came in and discussed Mindstorms for 10 minutes, lecture took 40 to 45 min B Smith: 10/31/05: time 45 minutes. Students were quite interested and challenged. Good “thinking” exercise. Rate: 3

Upload: kyree-dugdale

Post on 31-Mar-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

Case Study:Focus on Structures

Math 130

Lecture 21

B Smith:

10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing the surveys. Good examples here

B Smith:

10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing the surveys. Good examples here

B Smith:

Conisder skipping? No. There are good examples of use of structures, modulus, arrays of structures, pointers to struct.

B Smith:

Conisder skipping? No. There are good examples of use of structures, modulus, arrays of structures, pointers to struct.

B Smith:

Alex came in and discussed Mindstorms for 10 minutes, lecture took 40 to 45 min

B Smith:

Alex came in and discussed Mindstorms for 10 minutes, lecture took 40 to 45 min

B Smith:

10/31/05: time 45 minutes. Students were quite interested and challenged. Good “thinking” exercise. Rate: 3

B Smith:

10/31/05: time 45 minutes. Students were quite interested and challenged. Good “thinking” exercise. Rate: 3

Page 2: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

3

Overview

• Case Study deck of cards implementation shuffling simulation dealing

Page 3: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

4

Struct Case Study• card is the structure name and is used to declare variables of the

structure type• card contains two members of type char *

Namely, the members are face and suit.

struct card { char *face;//rank char *suit;//4suits};

struct card{char *face; char *suit;};

Page 4: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

5

typedef• Used to create shorter type names

• Allows for an alias or a pseudonym to be used as shorthand notation for previously defined names

• An example would be

typedef struct card Card;

• So have we created a new data type? No, only an alias, a synonym

Page 5: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

6

Initialization, Assignment

• Can be initialized using the following methods:

Card oneCard = {“Seven”, “Diamonds”};

or:

Card sevenDiamonds = oneCard;

or member by member:

Card sevenDiamonds;

sevenDiamonds.face = “Seven”;

sevenDiamonds.suit = “Diamonds”;

Page 6: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

7

Structure Member Access

• Accessing the structure’s members Dot operator ( . )

Arrow operator ( -> ) is used with pointers to structure variables

cardPtr->suit is equivalent to (*cardptr).suit

Card myCard;printf(“%s”, myCard.suit);

Card *cardPtr=&myCard;printf(“%s”, cardPtr->suit);

Page 7: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

8

Conditional Expressions

• The operator ?: provides another way of implementing an if-else condition

• The format is:

expression1 ? expression2 : expression3

expression1 is evaluated: • if it is non-zero, the result is the value of expression2, • otherwise the result is derived from expression3

Page 8: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

9

Conditional Expression Example

if (hours > 40) rate = payRate*1.5;else rate = payRate;

rate = (hours > 40) ? payRate*1.5 : payRate

Page 9: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

10

Example• Card Shuffling and Dealing Program Using Structures

Page 10: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

11

main()

#include <stdio.h>#include <stdlib.h>#include <time.h>

struct card { char *face; char *suit; }; typedef struct card Card; /* new type name for struct card */

void fillDeck( Card * wDeck, char * wFace[], char * wSuit[] );void shuffle( Card * wDeck );void deal(Card * wDeck );

int main()

{ ...

Page 11: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

12

main()

int main(){ Card deck[ 52 ]; char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

/* initialize array of pointers */ char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};

srand( time( NULL ) ); /* randomize */

fillDeck( deck, face, suit ); /* load the deck with Cards */ shuffle( deck ); /* put Cards in random order */ deal( deck ); /* deal all 52 Cards */

return 0; /* indicates successful termination */

}

Page 12: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

13

fillDeck()

/* place strings into Card structures */void fillDeck( Card * wDeck, char * wFace[], char * wSuit[] ){ int i;

for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = wFace[ i % 13 ]; wDeck[ i ].suit = wSuit[ i / 13 ]; }

}

char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

/* initialize array of pointers */ char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};

i i mod 13 i div 130 0 0

1 1 0

2 2 0

3 3 0

4 4 0

5 5 0

6 6 0

7 7 0

8 8 0

9 9 0

10 10 0

11 11 0

12 12 0

13 0 1

14 1 1

15 2 1

16 3 1

17 4 1

18 5 1

19 6 1

20 7 1

21 8 1

22 9 1

23 10 1

24 11 1

25 12 1

26 0 2

27 1 2

28 2 2

29 3 2

30 4 2

31 5 2

32 6 2

33 7 2

34 8 2

35 9 2

36 10 2

37 11 2

38 12 2

39 0 3

40 1 3

41 2 3

42 3 3

43 4 3

44 5 3

45 6 3

46 7 3

47 8 3

48 9 3

49 10 3

50 11 3

51 12 3

52 0 4

53 1 4

Page 13: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

14

void shuffle()

/* shuffle cards */void shuffle( Card * const wDeck ){ int i; /* counter */ int j; /* hold random value between 0 - 51 */ Card temp; /* temporary structure for swapping Cards */

/* loop through wDeck randomly swapping Cards */ for ( i = 0; i <= 51; i++ ) { j = rand() % 52; temp = wDeck[ i ]; wDeck[ i ] = wDeck[ j ]; wDeck[ j ] = temp; }}

Page 14: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

15

void deal()

/* deal cards */void deal(Card * wDeck ){ int i; /* counter */ for ( i = 0; i <= 51; i++ ) { printf( "%5s of %-8s%c", wDeck[ i ].face, wDeck[ i ].suit, ( i + 1 ) % 2 ? '\t' : '\n' ); }}

Page 15: Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing

16

output