1 / 15

Case Study: Focus on Structures

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/31/05: time 45 minutes. Students were quite interested and challenged. Good “thinking” exercise. Rate: 3. B Smith:

justin
Download Presentation

Case Study: Focus on Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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/31/05: time 45 minutes. Students were quite interested and challenged. Good “thinking” exercise. Rate: 3 B Smith: Conisder skipping? No. There are good examples of use of structures, modulus, arrays of structures, pointers to struct. Case Study:Focus on Structures B Smith: Alex came in and discussed Mindstorms for 10 minutes, lecture took 40 to 45 min Math 130Lecture 21

  2. Overview • Case Study • deck of cards implementation • shuffling simulation • dealing

  3. 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;};

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

  5. 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”;

  6. 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 CardmyCard; printf(“%s”, myCard.suit); Card*cardPtr=&myCard; printf(“%s”, cardPtr->suit);

  7. Conditional Expressions • The operator ?: provides another way of implementing an if-else condition • The format is: expression1?expression2 :expression3 • expression1is evaluated: • if it is non-zero, the result is the value of expression2, • otherwise the result is derived from expression3

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

  9. Example • Card Shuffling and Dealing Program Using Structures

  10. 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() { ...

  11. 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 */ }

  12. fillDeck() 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"}; /* 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 ]; } }

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

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

  15. output

More Related