L19 a 1 Example Using Arrays Using Top-Down Design and Functions to Simplify Code and Create Modules.

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Fundamental of C programming
EXAMPLES. Example 1: Write a Java method that performs addition on two binary numbers. Each binary number is kept in an integer array
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
EMB1006 JAVA Part 2 Arrays and onwards Jonathan-Lee Jones.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 10 Program Organization (continued)
CMSC 104, Version 9/011 Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading None.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Chapter 7 - Pointers Outline 7.1 Introduction
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Andy Wang Object Oriented Programming in C++ COP 3330
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Introduction to Programming
Incremental Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Example Using Functions
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Relational, Logical, and Equality Operators
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Examples Example Problems, their Algorithms, and their C Source Code.
Arrays I Handling lists of data.
Task 2 Implementation help
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Incremental Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
and Functions to Simplify Code
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
and Functions to Simplify Code
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

L19 a 1 Example Using Arrays Using Top-Down Design and Functions to Simplify Code and Create Modules

L19 a 2 Problem: Dealing Cards l 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.

L19 a 3 Algorithm: Dealing Cards l Print Explanation to user l Get number of cards per hand from user l for each card in the hand oRandomly generate a card oAssure that card hasn’t already been dealt. oPut the new card in the player’s hand oRecord this card as a dealt card l Print the suit and value of the cards in the player’s hand

L19 a 4 Dealing Cards Thinking it through l 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)

Dealing Cards Design & write function prototypes void PrintExplanation (void) ; int GetNumPerHand (int min, int max) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]); void InitArray (int array[ ], int numElems) ; int GetRandomSuit (void) ; int GetRandomValue (void) ; int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void Deal (int suit, int value, int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ]); void RecordAsUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void PrintHand (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ] );

L19 a 6 Dealing Cards Write and test PrintExplanation( ) /*****************************************************************************************\ * 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 void PrintExplanation (void) ; int main ( ) { PrintExplanation ( ) ; return 0; } /************************************************** * PrintExplanation is a procedure that prints the program explanation for the user * It takes no arguments and returns no values \*************************************************/ void PrintExplanation (void) { printf (“This program deals a hand of cards to you. You will be asked\n”); printf (“to enter the number of cards you’d like in the hand.\n\n\n”); }

Dealing Cards Write and test InitializeArrays( ) /************************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]); void InitArray (int array[ ], int numElems) ; int main ( ) { int handHearts[SIZE], handClubs[SIZE], handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE], dealtDiamonds[SIZE], dealtSpades[SIZE] ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); return 0; }

L19 a 8 Dealing Cards Write and test InitializeArrays( ) /*********************************************\ * InitializeArrays initializes all of the arrays passed to it * to hold all zeros by passing each to the InitArray function. \*********************************************/ void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) { InitArray (handHearts, SIZE) ; InitArray (handClubs, SIZE) ; InitArray (handDiamonds, SIZE) ; InitArray (handSpades, SIZE) ; InitArray (dealtHearts, SIZE) ; InitArray (dealtClubs, SIZE) ; InitArray (dealtDiamonds, SIZE) ; InitArray (dealtSpades, SIZE) ; }

L19 a 9 Dealing Cards Write and test InitArray( ) /*********************************************\ * InitArray initializes the array of ints passed to it * to hold all zeros. The number of elements in the * array is passed in as numElems \*********************************************/ void InitArray (int array[ ], int numElems) { int i ; for (i = 0 ; i < numElems ; i++) { array [ i ] = 0 ; }

Dealing Cards Write and test GetNumPerHand( ) /*****************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]); void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int main ( ) { int num, handHearts[SIZE], handClubs[SIZE]; int handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE]; int dealtDiamonds[SIZE], dealtSpades[SIZE] ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); num = GetNumPerHand (MIN, MAX) ; printf (“num = %d\n”, num) ; return 0; }

L19 a 11 Dealing Cards Write and test GetNumPerHand ( ) /************************************************** * GetNumPerHand 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 GetNumPerHand (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 max ) { printf (“The number of cards per hand must be between %d and %d, inclusive\n”, min, max) ; printf (“Enter the number of cards per hand : “) ; scanf (“%d”, &num) ; } return num ; }

Dealing Cards Write and test GetRandomSuit( ) & GetRandomValue( ) /*****************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]); void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int main ( ) { int i, num, handHearts[SIZE], handClubs[SIZE]; int handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE]; int dealtDiamonds[SIZE], dealtSpades[SIZE] ; int suit, value ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); num = GetNumPerHand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; printf (“%d: %d of %d\n”, i, value, suit) ; } return 0; }

L19 a 13 Dealing Cards Write and test GetRandomSuit( ) & GetRandomValue( ) /************************************************** * GetRandomSuit gets a random number between 0 and 3 to indicate the suit of the drawn card. \*************************************************/ int GetRandomSuit (void) { int suit ; suit = rand ( ) % 4 ; return suit ; } /************************************************** * GetRandomValue gets a random number between 1 and 13, the value of the drawn card. \*************************************************/ int GetRandomValue (void) { int value ; value = rand ( ) % ; return value ; }

Dealing Cards Write and test AlreadyUsed( ) /*****************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]); void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; int main ( ) { int i, num, handHearts[SIZE], handClubs[SIZE]; int handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE]; int dealtDiamonds[SIZE], dealtSpades[SIZE] ; int suit, value ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); num = GetNumPerHand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; while (AlreadyUsed (suit, value, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades) ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; } return 0; }

Dealing Cards - Write and test AlreadyUsed( ) /************************************************** * AlreadyUsed 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, dealtHearts, dealtClubs, dealtDiamonds, and dealtSpades as arguments. \*************************************************/ int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) { int used; switch (suit) { case HEARTS : used = dealtHearts [value] ; break; case CLUBS : used = dealtClubs [value] ; break; case DIAMONDS : used = dealtDia monds [value] ; break; case SPADES : used = dealtSpades [value] ; break; default : printf (“Error in suit in AlreadyUsed\n”) ; used = -1 ; } return used ; }

Dealing Cards Write and test Deal( ) & RecordAsUsed( ) /**********************************************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void Deal (int suit, int value, int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ]) ;

Dealing Cards Write and test Deal( ) & RecordAsUsed( ) int main ( ) { int i, num, suit, value ; int handHearts[SIZE], handClubs[SIZE], handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE], dealtDiamonds[SIZE], dealtSpades[SIZE] ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); num = GetNumPerHand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; while (AlreadyUsed (suit, value, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades ) ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; } Deal (handHearts, handClubs, handDiamonds, handSpades) ; RecordAsUsed (dealtHearts, dealtClubs, dealtDiamonds, dealtSpades) ; } return 0 ; }

L19 a 18 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, handHearts, handClubs, handDiamonds, and handSpades 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 handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ]) { switch (suit) { case HEARTS : handHearts [value] = 1 ; break ; case CLUBS : handClubs [value] = 1 ; break ;; case DIAMONDS : handDia monds [value] = 1 ; break ; case SPADES : handSpades [value] = 1 ; break ; default : printf ( “ Error in suit in Deal \n ” ) ; }

L19 a 19 Dealing Cards Write and test RecordAsUsed( ) /************************************************** * RecordAsUsed takes the suit and value of a card and the four arrays of dealt cards, dealtHearts, * dealtClubs, dealtDiamonds, and dealtSpades as arguments. The element of the appropriate * array is changed to 1 to indicate that the card has been dealt. \*************************************************/ void RecordAsUsed ( int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ] ) { switch (suit) { case HEARTS : dealtHearts [value] = 1 ; break ; case CLUBS : dealtClubs [value] = 1 ; break ; case DIAMONDS : dealtDia monds [value] = 1 ; break ; case SPADES : dealtSpades [value] = 1 ; break ; default : printf (“Error in suit in RecordAsUsed\n”) ; }

Dealing Cards - Write and test PrintHand( ) /**********************************************************************************************\ * 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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void Deal (int suit, int value, int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ]) ; void PrintHand (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ] ) ;

Dealing Cards Write and test PrintHand( ) int main ( ) { int i, num, suit, value ; int handHearts[SIZE], handClubs[SIZE], handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE], dealtDiamonds[SIZE], dealtSpades[SIZE] ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); num = GetNumPerHand (MIN, MAX) ; for ( i = 0 ; i < num ; i++ ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; while (AlreadyUsed (suit, value, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades ) ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; } Deal (handHearts, handClubs, handDiamonds, handSpades) ; RecordAsUsed (dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); } PrintHand (handHearts, handClubs, handDiamonds, handSpades ) ; return 0; }

Dealing Cards - Write and test PrintHand( ) /************************************************** * PrintHand takes the number of cards in the hand and the four arrays of cards representing the * player’s hand, handHearts, handClubs, handDiamonds, and handSpades as arguments. The * values and suits of each card held in the hand is printed. \*************************************************/ void PrintHand (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ] ) { int i ; for ( i = 1 ; i < SIZE ; i++ ) { if (handHearts [ i ] == 1) { printf (“%d of Hearts\n”, i ) ; } for ( i = 1 ; i < SIZE ; i++ ) { if (handClubs [ i ] == 1) { printf (“%d of Clubs\n”, i ) ; }

L19 a 23 Dealing Cards Write and test PrintHand( ) (continued) for ( i = 1 ; i < SIZE ; i++ ) { if (handDiamonds [ i ] == 1) { printf (“%d of Diamonds\n”, i ) ; } for ( i = 1 ; i < SIZE ; i++ ) { if (handSpades [ i ] == 1) { printf (“%d of Spades\n”, i ) ; } } /* end of PrintHand Function */

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 #define HEARTS 0 #define CLUBS 1 #define DIAMONDS 2 #define SPADES 3 #define SIZE 14 #define MIN 1 #define MAX 52 void PrintExplanation (void) ; void InitializeArrays (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ], int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void InitArray (int array[ ], int numElems) ; int GetNumPerHand (int min, int max) ; int AlreadyUsed (int suit, int value, int dealtHearts[ ], int dealtClubs[ ], int dealtDiamonds[ ], int dealtSpades[ ]) ; void Deal (int suit, int value, int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ]) ; void PrintHand (int handHearts[ ], int handClubs[ ], int handDiamonds[ ], int handSpades[ ] ) ;

Dealing Cards - Final Version int main ( ) { int i, num, suit, value ; int handHearts[SIZE], handClubs[SIZE], handDiamonds[SIZE], handSpades[SIZE] ; int dealtHearts[SIZE], dealtClubs[SIZE], dealtDiamonds[SIZE], dealtSpades[SIZE] ; PrintExplanation ( ) ; InitializeArrays (handHearts, handClubs, handDiamonds, handSpades, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); srand ( time (0) ) ; num = GetNumPerHand (MIN, MAX) ; /* Deal a hand of cards making sure that each card hasn’t already been dealt */ for ( i = 0 ; i < num ; i++ ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; while (AlreadyUsed (suit, value, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades ) ) { suit = GetRandomSuit ( ) ; value = GetRandomValue ( ) ; } Deal (suit, handHearts, handClubs, handDiamonds, handSpades) ; RecordAsUsed (value, dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); } PrintHand (handHearts, handClubs, handDiamonds, handSpades ) ; return 0; }

L19 a 26 Further Improvements ? l We have a design error ! oWe should have realized that Deal and RecordAsUsed would have same exact code. oWe could have had one function called AddCard that would have the same code as Deal or RecordAsUsed and passed in the arrays for the hand when dealing and the arrays for dealt when recording a used card. oWouldn’t this have been easier to handle if we had spotted it sooner ? l We should rewrite PrintHand( ) so that it prints Jack, Queen and King instead of 11, 12, & 13. l Anything else ?

L19 a 27 Final Exam l Final Exam: Monday Dec 18th, at 8:30 – 10:30 pm in Room 305, the regular classroom. l There will be no Make-ups after the final exam. If you know you can not make it, please inform me now, so I can schedule a makeup sometime before the final.