Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 2 Administrivia 14 students dropped from Bb not officially dropped …. yet Lab 9 due today Next project has been posted Grades have been brought up to date on Bb See me if you dont have access (may have been dropped) Test 02 will be Friday, April 29

3 3 Overview Case Study deck of cards implementation shuffling simulation dealing

4 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;};

5 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

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

7 7 Structure Member Access Accessing the structures 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);

8 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

9 9 Conditional Expression Example if (hours > 40) rate = payRate*1.5; else rate = payRate; rate = (hours > 40) ? payRate*1.5 : payRate

10 10 Example Card Shuffling and Dealing Program Using Structures

11 11 main() #include 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() {...

12 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 */ }

13 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"};

14 14 void shuffle() /* shuffle cards */ void shuffle( Card * const wDeck ) { int i; /* counter */ int j; /* hold random value between */ 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; }

15 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' ); }

16 16 output


Download ppt "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."

Similar presentations


Ads by Google