Example Using Arrays Using TopDown Design and Functions


























- Slides: 26
Example Using Arrays Using Top-Down Design and Functions to Simplify Code and Create Modules L 19 a 1
Problem: Dealing Cards Deal a hand of cards to a player l Write a program that deals a hand of cards to a player assuring that no cards are repeated. l The user must supply the number of cards per hand. l L 19 a 2
Algorithm: Dealing Cards Print Explanation to user l Get number of cards per hand from user l for each card in the hand l o Randomly generate a card o Assure that card hasn’t already been dealt. o Put the new card in the player’s hand o Record this card as a dealt card l L 19 a Print the suit and value of the cards in the player’s hand 3
Dealing Cards Thinking it through Draw a card by randomly getting a suit, a number between 0 and 3, inclusive & randomly getting a card value (1 - 13). l Store the cards in a hand by having 4 arrays, one for each suit, that has a 1 as the element at the appropriate index to indicate the card’s presence, 0 its absence. l Keep track of already used cards in 4 arrays (same storage method as hand) l L 19 a 4
Dealing Cards Design & write function prototypes void Print. Explanation (void) ; int Get. Num. Per. Hand (int min, int max) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]); void Init. Array (int array[ ], int num. Elems) ; int Get. Random. Suit (void) ; int Get. Random. Value (void) ; int Already. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Deal (int suit, int value, int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ]); void Record. As. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Print. Hand (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ] );
Dealing Cards Write and test Print. Explanation( ) /********************************************* * Filename: cards. c * * Author: * * Description: This program deals a hand of cards to a player. The number of * * cards is specified by the user. * *********************************************/ #include <stdio. h> void Print. Explanation (void) ; int main ( ) { Print. Explanation ( ) ; return 0; } /************************* * Print. Explanation is a procedure that prints the program explanation for the user * It takes no arguments and returns no values *************************/ void Print. Explanation (void) { printf (“This program deals a hand of cards to you. You will be askedn”); printf (“to enter the number of cards you’d like in the hand. nnn”); } L 19 a 6
Dealing Cards Write and test Initialize. Arrays( ) /************************************ * Filename: cards. c * * Author: * * Description: This program deals a hand of cards to a player. * * The number of cards is specified by the user. * ************************************/ #include <stdio. h> #include <stdlib. h> #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 void Print. Explanation (void) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]); void Init. Array (int array[ ], int num. Elems) ; int main ( ) { int hand. Hearts[SIZE], hand. Clubs[SIZE], hand. Diamonds[SIZE], hand. Spades[SIZE] ; int dealt. Hearts[SIZE], dealt. Clubs[SIZE], dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; } Print. Explanation ( ) ; Initialize. Arrays (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); return 0;
Dealing Cards Write and test Initialize. Arrays( ) /*********************** * Initialize. Arrays initializes all of the arrays passed to it * to hold all zeros by passing each to the Init. Array function. ***********************/ void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) { Init. Array (hand. Hearts, SIZE) ; Init. Array (hand. Clubs, SIZE) ; Init. Array (hand. Diamonds, SIZE) ; Init. Array (hand. Spades, SIZE) ; Init. Array (dealt. Hearts, SIZE) ; Init. Array (dealt. Clubs, SIZE) ; Init. Array (dealt. Diamonds, SIZE) ; Init. Array (dealt. Spades, SIZE) ; } L 19 a 8
Dealing Cards Write and test Init. Array( ) /*********************** * Init. Array initializes the array of ints passed to it * to hold all zeros. The number of elements in the * array is passed in as num. Elems ***********************/ void Init. Array (int array[ ], int num. Elems) { int i ; for (i = 0 ; i < num. Elems ; i++) { array [ i ] = 0 ; } } L 19 a 9
Dealing Cards Write and test Get. Num. Per. Hand( ) /********************************* int main ( ) * Filename: cards. c * { * Author: * int num, hand. Hearts[SIZE], hand. Clubs[SIZE]; * Description: This program deals a hand of cards to a * int hand. Diamonds[SIZE], hand. Spades[SIZE] ; * player. The number of cards is specified * int dealt. Hearts[SIZE], dealt. Clubs[SIZE]; * by the user. * *********************************/ int dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; #include <stdio. h> Print. Explanation ( ) ; #include <stdlib. h> Initialize. Arrays (hand. Hearts, hand. Clubs, #define HEARTS 0 hand. Diamonds, hand. Spades, #define CLUBS 1 dealt. Hearts, dealt. Clubs, #define DIAMONDS 2 dealt. Diamonds, dealt. Spades); #define SPADES 3 num = Get. Num. Per. Hand (MIN, MAX) ; #define SIZE 14 #define MIN 1 printf (“num = %dn”, num) ; #define MAX 52 return 0; void Print. Explanation (void) ; } void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]); void Init. Array (int array[ ], int num. Elems) ; int Get. Num. Per. Hand (int min, int max) ;
Dealing Cards Write and test Get. Num. Per. Hand ( ) /************************* * Get. Num. Per. Hand gets the number of cards per hand from the user and returns it to the * calling function. It assures that the number of cards is between the minimum amount passed to * this function and the maximum amount passed to this function. *************************/ int Get. Num. Per. Hand (int min, int max) { int num ; printf (“Enter the number of cards per hand : “) ; scanf (“%d”, &num) ; /* assure input is between min and max */ while ( num < min || num > max ) { printf (“The number of cards per hand must be between %d and %d, inclusiven”, min, max) ; printf (“Enter the number of cards per hand : “) ; scanf (“%d”, &num) ; } return num ; } L 19 a 11
Dealing Cards Write and test Get. Random. Suit( ) & Get. Random. Value( ) /********************************* int main ( ) * Filename: cards. c * { * Author: * int i, num, hand. Hearts[SIZE], hand. Clubs[SIZE]; * Description: This program deals a hand of cards to a * int hand. Diamonds[SIZE], hand. Spades[SIZE] ; * player. The number of cards is specified * int dealt. Hearts[SIZE], dealt. Clubs[SIZE]; * by the user. * *********************************/ int dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; #include <stdio. h> int suit, value ; #include <stdlib. h> Print. Explanation ( ) ; #define HEARTS 0 Initialize. Arrays (hand. Hearts, hand. Clubs, #define CLUBS 1 hand. Diamonds, hand. Spades, #define DIAMONDS 2 dealt. Hearts, dealt. Clubs, #define SPADES 3 dealt. Diamonds, dealt. Spades); #define SIZE 14 #define MIN 1 num = Get. Num. Per. Hand (MIN, MAX) ; #define MAX 52 for ( i = 0 ; i < num ; i++ ) void Print. Explanation (void) ; { void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], suit = Get. Random. Suit ( ) ; int hand. Diamonds[ ], int hand. Spades[ ], value = Get. Random. Value ( ) ; int dealt. Hearts[ ], int dealt. Clubs[ ], printf (“%d: %d of %dn”, i, value, suit) ; int dealt. Diamonds[ ], int dealt. Spades[ ]); } void Init. Array (int array[ ], int num. Elems) ; return 0; int Get. Num. Per. Hand (int min, int max) ; }
Dealing Cards Write and test Get. Random. Suit( ) & Get. Random. Value( ) /************************* * Get. Random. Suit gets a random number between 0 and 3 to indicate the suit of the drawn card. *************************/ int Get. Random. Suit (void) { int suit ; suit = rand ( ) % 4 ; return suit ; } /************************* * Get. Random. Value gets a random number between 1 and 13, the value of the drawn card. *************************/ int Get. Random. Value (void) { int value ; value = rand ( ) % 13 + 1 ; return value ; } L 19 a 13
Dealing Cards Write and test Already. Used( ) /********************************* int main ( ) * Filename: cards. c * { * Author: * int i, num, hand. Hearts[SIZE], hand. Clubs[SIZE]; * Description: This program deals a hand of cards to a * int hand. Diamonds[SIZE], hand. Spades[SIZE] ; * player. The number of cards is specified * int dealt. Hearts[SIZE], dealt. Clubs[SIZE]; * by the user. * int dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; *********************************/ int suit, value ; #include <stdio. h> Print. Explanation ( ) ; #include <stdlib. h> Initialize. Arrays (hand. Hearts, hand. Clubs, #define HEARTS 0 hand. Diamonds, hand. Spades, #define CLUBS 1 dealt. Hearts, dealt. Clubs, #define DIAMONDS 2 dealt. Diamonds, dealt. Spades); #define SPADES 3 num = Get. Num. Per. Hand (MIN, MAX) ; #define SIZE 14 for ( i = 0 ; i < num ; i++ ) #define MIN 1 { #define MAX 52 suit = Get. Random. Suit ( ) ; void Print. Explanation (void) ; value = Get. Random. Value ( ) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], while (Already. Used (suit, value, dealt. Hearts, int hand. Diamonds[ ], int hand. Spades[ ], dealt. Clubs, dealt. Diamonds, int dealt. Hearts[ ], int dealt. Clubs[ ], dealt. Spades) ) int dealt. Diamonds[ ], int dealt. Spades[ ]); { void Init. Array (int array[ ], int num. Elems) ; suit = Get. Random. Suit ( ) ; int Get. Num. Per. Hand (int min, int max) ; value = Get. Random. Value ( ) ; int Already. Used (int suit, int value, int dealt. Hearts[ ], } int dealt. Clubs[ ], int dealt. Diamonds[ ], } return 0; int dealt. Spades[ ]) ; }
Dealing Cards - Write and test Already. Used( ) /************************* * Already. Used returns true if a card has already been dealt and false if it hasn’t been dealt yet. * The function takes the suit and value of the card to be checked and the four arrays of dealt * cards, dealt. Hearts, dealt. Clubs, dealt. Diamonds, and dealt. Spades as arguments. *************************/ int Already. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) { int used; switch (suit) { case HEARTS : used = dealt. Hearts [value] ; break; case CLUBS : used = dealt. Clubs [value] ; break; case DIAMONDS : used = dealt. Diamonds [value] ; break; case SPADES default } return used ; } : used = dealt. Spades [value] ; break; : printf (“Error in suit in Already. Usedn”) ; used = -1 ;
Dealing Cards Write and test Deal( ) & Record. As. Used( ) /*********************************************** * Filename: cards. c * * Author: * * Description: This program deals a hand of cards to a player. The number of * * cards is specified by the user. * ***********************************************/ #include <stdio. h> #include <stdlib. h> #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void Print. Explanation (void) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Init. Array (int array[ ], int num. Elems) ; int Get. Num. Per. Hand (int min, int max) ; int Already. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Deal (int suit, int value, int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ]) ;
Dealing Cards Write and test Deal( ) & Record. As. Used( ) int main ( ) { int i, num, suit, value ; int hand. Hearts[SIZE], hand. Clubs[SIZE], hand. Diamonds[SIZE], hand. Spades[SIZE] ; int dealt. Hearts[SIZE], dealt. Clubs[SIZE], dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; } Print. Explanation ( ) ; Initialize. Arrays (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); num = Get. Num. Per. Hand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; while (Already. Used (suit, value, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades ) ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; } Deal (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades) ; Record. As. Used (dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades) ; } return 0 ;
Dealing Cards Write and test Deal( ) /************************* * Deal takes the suit and value of a card and the four arrays of cards representing the player’s * hand, hand. Hearts, hand. Clubs, hand. Diamonds, and hand. Spades as arguments. The element of * the appropriate array is changed to 1 to indicate that the card is in the hand. *************************/ void Deal (int suit, int value, int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ]) { switch (suit) { case HEARTS : hand. Hearts [value] = 1 ; break ; case CLUBS : hand. Clubs [value] = 1 ; break ; ; case DIAMONDS : hand. Diamonds [value] = 1 ; break ; case SPADES default : hand. Spades [value] = 1 ; break ; : printf ( “ Error in suit in Deal n ” ) ; } } L 19 a 18
Dealing Cards Write and test Record. As. Used( ) /************************* * Record. As. Used takes the suit and value of a card and the four arrays of dealt cards, dealt. Hearts, * dealt. Clubs, dealt. Diamonds, and dealt. Spades as arguments. The element of the appropriate * array is changed to 1 to indicate that the card has been dealt. *************************/ void Record. As. Used ( int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ] , int dealt. Spades[ ] ) { switch (suit) { case HEARTS : dealt. Hearts [value] = 1 ; break ; case CLUBS : dealt. Clubs [value] = 1 ; break ; case DIAMONDS : dealt. Diamonds [value] = 1 ; break ; case SPADES default : dealt. Spades [value] = 1 ; break ; : printf (“Error in suit in Record. As. Usedn”) ; } } L 19 a 19
Dealing Cards - Write and test Print. Hand( ) /*********************************************** * Filename: cards. c * * Author: * * Description: This program deals a hand of cards to a player. The number of * * cards is specified by the user. * ***********************************************/ #include <stdio. h> #include <stdlib. h> #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void Print. Explanation (void) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Init. Array (int array[ ], int num. Elems) ; int Get. Num. Per. Hand (int min, int max) ; int Already. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Deal (int suit, int value, int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ]) ; void Print. Hand (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ] ) ;
Dealing Cards Write and test Print. Hand( ) int main ( ) { int i, num, suit, value ; int hand. Hearts[SIZE], hand. Clubs[SIZE], hand. Diamonds[SIZE], hand. Spades[SIZE] ; int dealt. Hearts[SIZE], dealt. Clubs[SIZE], dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; } Print. Explanation ( ) ; Initialize. Arrays (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); num = Get. Num. Per. Hand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; while (Already. Used (suit, value, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades ) ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; } Deal (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades) ; Record. As. Used (dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); } Print. Hand (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades ) ; return 0;
Dealing Cards - Write and test Print. Hand( ) /************************* * Print. Hand takes the number of cards in the hand the four arrays of cards representing the * player’s hand, hand. Hearts, hand. Clubs, hand. Diamonds, and hand. Spades as arguments. The * values and suits of each card held in the hand is printed. *************************/ void Print. Hand (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ] ) { int i ; for ( i = 1 ; i < SIZE ; i++ ) { if (hand. Hearts [ i ] == 1) { printf (“%d of Heartsn”, i ) ; } } for ( i = 1 ; i < SIZE ; i++ ) { if (hand. Clubs [ i ] == 1) { printf (“%d of Clubsn”, i ) ; } }
Dealing Cards Write and test Print. Hand( ) (continued) for ( i = 1 ; i < SIZE ; i++ ) { if (hand. Diamonds [ i ] == 1) { printf (“%d of Diamondsn”, i ) ; } } for ( i = 1 ; i < SIZE ; i++ ) { if (hand. Spades [ i ] == 1) { printf (“%d of Spadesn”, i ) ; } } } L 19 a /* end of Print. Hand Function */ 23
Dealing Cards - Final Version /*********************************************** * Filename: cards. c * * Author: * * Description: This program deals a hand of cards to a player. The number of * * cards is specified by the user. * ***********************************************/ #include <stdio. h> #include <stdlib. h> #include <time. h> #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void Print. Explanation (void) ; void Initialize. Arrays (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ], int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Init. Array (int array[ ], int num. Elems) ; int Get. Num. Per. Hand (int min, int max) ; int Already. Used (int suit, int value, int dealt. Hearts[ ], int dealt. Clubs[ ], int dealt. Diamonds[ ], int dealt. Spades[ ]) ; void Deal (int suit, int value, int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ]) ; void Print. Hand (int hand. Hearts[ ], int hand. Clubs[ ], int hand. Diamonds[ ], int hand. Spades[ ] ) ;
Dealing Cards - Final Version int main ( ) { int i, num, suit, value ; int hand. Hearts[SIZE], hand. Clubs[SIZE], hand. Diamonds[SIZE], hand. Spades[SIZE] ; int dealt. Hearts[SIZE], dealt. Clubs[SIZE], dealt. Diamonds[SIZE], dealt. Spades[SIZE] ; Print. Explanation ( ) ; Initialize. Arrays (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); srand ( time (0) ) ; num = Get. Num. Per. Hand (MIN, MAX) ; } /* Deal a hand of cards making sure that each card hasn’t already been dealt */ for ( i = 0 ; i < num ; i++ ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; while (Already. Used (suit, value, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades ) ) { suit = Get. Random. Suit ( ) ; value = Get. Random. Value ( ) ; } Deal (suit, hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades) ; Record. As. Used (value, dealt. Hearts, dealt. Clubs, dealt. Diamonds, dealt. Spades); } Print. Hand (hand. Hearts, hand. Clubs, hand. Diamonds, hand. Spades ) ; return 0;
Further Improvements ? l We have a design error ! o We should have realized that Deal and Record. As. Used would have same exact code. o We could have had one function called Add. Card that would have the same code as Deal or Record. As. Used and passed in the arrays for the hand when dealing and the arrays for dealt when recording a used card. o Wouldn’t this have been easier to handle if we had spotted it sooner ? l l We should rewrite Print. Hand( ) so that it prints Jack, Queen and King instead of 11, 12, & 13. Anything else ? L 19 a 26