150 likes | 244 Views
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:
E N D
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
Overview • Case Study • deck of cards implementation • shuffling simulation • dealing
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;};
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
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”;
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);
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
Conditional Expression Example if (hours > 40) rate = payRate*1.5; else rate = payRate; rate = (hours > 40) ?payRate*1.5: payRate
Example • Card Shuffling and Dealing Program Using Structures
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() { ...
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 */ }
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 ]; } }
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; } }
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' ); } }