cmsc 1041 examples using top-down design and functions to simplify code and create modules

47
CMSC 104 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Upload: eugenia-cobb

Post on 17-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 1

Examples

Using Top-Down Design

and Functions to Simplify Code

and Create Modules

Page 2: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 2

Problem: Interest

Compute interest that is compounded annually.

Write an interactive program that allows the user to calculate the interest accrued in a savings account that is compounded annually over a period of years.

The user must supply the principal amount, interest rate and the number of years .

Page 3: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 3

Algorithm: Interest

Print explanation of the program Get principal from user. Get interest rate from user. Get number of years from user. For the number of years specified

o Calculate the amount in the account at the end of the year. (amount += amount * rate)

interest accrued = amount - principal Print report.

Page 4: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 4

Deciding on functions: InterestWrite the Function Prototypes

void PrintInstructions (void) ; float GetPrincipal (float max, float min) ; float GetInterestRate (float max, float min) ; int GetYears (int max, int min) ; float CalculateAmount (float principal, float rate, int years) ; void PrintReport (float principal, float rate, int years, float amount, float interest) ;

Page 5: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 5

Interest : Design the Report

Interest rate : 7.0000 % Period : 20 years

Principal at start of period : 1000.00 Interest accrued : 2869.68

Total amount at end of period : 3869.68

Page 6: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 6

Improved Interest with functionsProgrammed Incrementally, #1

Write and test PrintInstructions( )

/*****************************************************************************************\* Filename: interest.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program computes the interest accrued in an account ** that compounds interest annually. *\*****************************************************************************************/

#include <stdio.h>

void PrintInstructions (void) ;

main ( ){ PrintInstructions ( ) ;}

/*************************************************** PrintInstructions is a procedure that prints the program instructions for the user* It takes no arguments and returns no values \*************************************************/void PrintInstructions (void){

printf (“This program computes the interest accrued in an account\n”) ;printf (“that compounds interest annually. You will need to enter the\n”) ;printf (“amount of the principal, the interest rate and the number of years.\n\n”) ;

}

Page 7: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 7

Interest : Output #1

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Page 8: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 8

Improved Interest with functionsProgrammed Incrementally, # 2

Add to main ( ), write & debug GetPrincipal ( )

/*****************************************************************************************\* Filename: interest.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program computes the interest accrued in an account ** that compounds interest annually. *\******************************************************************************************/

#include <stdio.h>

#define MAXPRIN 100000.00#define MINPRIN 0.00

void PrintInstructions (void) ;float GetPrincipal (float max, float min) ;

main ( ){

float principal ;

PrintInstructions ( ) ;principal = GetPrincipal (MAXPRIN, MINPRIN) ;printf (“The principal is %.2f\n”, principal) ;

}

Page 9: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 9

Improved Interest with functionsProgrammed Incrementally, # 2

Writing of GetPrincipal ( )

/*************************************************** GetPrincipal gets the principal amount from the user and returns it to the calling function.* It assures that the principal is between the minimum amount passed to this function and the* maximum amount passed to this function.\*************************************************/float GetPrincipal (float max, float min){

float principal ;

printf (“Enter the principal amount : “) ;scanf (“%f”, &principal) ;

/* assure input is between min and max */while ( principal < min || principal > max ){ printf (“The principal amount must be a value between %.2f and %.2f\n”, min, max) ; printf (“Enter the principal amount : “); scanf (“%f”, &principal);}

return principal ;}

Page 10: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 10

Interest : Output #2Test GetPrincipal ( )

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Enter the principal amount : 200000.00The principal amount must be a value between 0.00 and 100000.00Enter the principal amount : -5.00The principal amount must be a value between 0.00 and 100000.00Enter the principal amount : 100000.01The principal amount must be a value between 0.00 and 100000.00Enter the principal amount : - .01The principal amount must be a value between 0.00 and 100000.00Enter the principal amount : 1000.00The principal is 1000.00

Page 11: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 11

Improved Interest with functionsProgrammed Incrementally, # 3

Add to main ( ), write and test GetRate ( )

/*****************************************************************************************\* Filename: interest.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program computes the interest accrued in an account ** that compounds interest annually. *\*****************************************************************************************/

#include <stdio.h>

#define MAXPRIN 100000.00#define MINPRIN 0.00#define MAXRATE 1.00#define MINRATE 0.00

void PrintInstructions (void) ;float GetPrincipal (float max, float min) ;float GetRate ( float max, float min);

main ( ){

float principal, rate ;

PrintInstructions ( ) ;principal = GetPrincipal (MAXPRIN, MINPRIN) ;rate = GetRate (MAXRATE, MINRATE) ;printf (“The interest rate is %.4f\n”, rate) ;

}

Page 12: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 12

Improved Interest with functionsProgrammed Incrementally, # 3

Write and test GetRate ( )/*************************************************** GetRate gets the interest rate from the user and returns it to the calling function.* It assures that the rate is between the minimum amount passed to this function and the* maximum amount passed to this function.\*************************************************/float GetRate (float max, float min){

float rate;

printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ;scanf (“%f”, &rate);

/* assure input is between min and max */while ( rate < min || rate > max ){ printf (“The interest rate must be between %.4f and %.4f\n”, min, max ) ; printf (“Enter the interest rate as a decimal (for 7%% enter .07) : “) ; scanf (“%f”, &rate);}

return rate ;}

Page 13: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 13

Interest : Output #3Test GetRate ( )

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : 5The interest rate must be between 0.0000 and 1.0000Enter the interest rate as a decimal (for 7% enter .07) : - .0001The interest rate must be between 0.0000 and 1.0000Enter the interest rate as a decimal (for 7% enter .07) : .07The interest rate is .0007

Page 14: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 14

Improved Interest with functionsProgrammed Incrementally, # 4

Add to main ( ), write and test GetYears ( )

/***********************************************************\* Filename: interest.c ** Author: Sue Bogar

** Date written: 12/4//99 ** Description: This program computes the interest ** accrued in an account that ** compounds interest annually. *\***********************************************************/

#include <stdio.h>

#define MAXPRIN 100000.00#define MINPRIN 0.00#define MAXRATE 1.00#define MINRATE 0.00#define MINYEARS 1#define MAXYEARS 100

void PrintInstructions (void) ;float GetPrincipal (float max, float min) ;float GetRate ( float max, float min) ;int GetYears (int max, int min) ;

main ( ){

float principal, rate ;int years ;

PrintInstructions ( ) ;principal = GetPrincipal (MAXPRIN, MINPRIN) ;rate = GetRate (MAXRATE, MINRATE) ;years = GetYears (MAXYEARS, MINYEARS) ;printf (“years is %d\n”, years) ;

}

Page 15: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 15

Improved Interest with functionsProgrammed Incrementally, # 4

Write and test GetYears ( )

/*************************************************** GetYears gets the years of the investment from the user and returns it to the calling function.* It assures that the years are between the minimum amount passed to this function and the* maximum amount passed to this function.\*************************************************/int GetYears (int max, int min){

int years ;

printf (“Enter the number of years : “);scanf (“%d”, &years);

/* assure input is between min and max */while ( years < min || years > max ){ printf (“The number of years must be between 1 and 100, inclusive\n”) ; printf (“Enter the number of years : “); scanf (“%d”, &years); }

return years ;}

Page 16: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 16

Interest : Output #4Test GetYears ( )

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : .07Enter the number of years : 0The number of years must be between 1 and 100, inclusiveEnter the number of years : 101The number of years must be between 1 and 100, inclusiveEnter the number of years : 20years is 20

Page 17: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 17

Improved Interest with functionsProgrammed Incrementally, # 5

Add to main ( ), write & test CalculateAmount ( )

/***********************************************************\* Filename: interest.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program computes the interest ** accrued in an account that ** compounds interest annually. *\***********************************************************/

#include <stdio.h>

#define MAXPRIN 100000.00#define MINPRIN 0.00#define MAXRATE 1.00#define MINRATE 0.00#define MINYEARS 1#define MAXYEARS 100

void PrintInstructions (void) ;float GetPrincipal (float max, float min) ;float GetRate ( float max, float min) ;int GetYears (int max, int min) ;float CalculateAmount (float principal, float rate, int years ) ;

main ( ){

float principal, rate, amount ;int years ;

PrintInstructions ( ) ;principal = GetPrincipal (MAXPRIN, MINPRIN) ;rate = GetRate (MAXRATE, MINRATE) ;years = GetYears (MAXYEARS, MINYEARS) ;amount = CalculateAmount (principal, rate, years) ;printf (“amount is %.2f\n”, amount) ;

}

Page 18: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 18

Improved Interest with functionsProgrammed Incrementally, # 5

Write and test CalculateAmount( )

/*************************************************** CalculateAmount calculates and returns the amount in the account when the account* is opened with the principal amount passed in, at the annual percentage rate passed in,* compounded annually, for the number of years passed in.\*************************************************/float CalculateAmount (float principal, float rate, int years){

int i ;

/* Calculate total amount in the account after the specified number of years */for ( i = 0 ; i < years ; i++ ){ principal += principal * rate ;}

return principal;}

Page 19: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 19

Interest : Output #5Test CalculateAmount ( )

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : .07Enter the number of years : 20amount is 3869.68

Page 20: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 20

Improved Interest with functionsProgrammed Incrementally, # 6

Add to main ( ), write & test PrintReport ( )

/***********************************************************\* Filename: interest.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program computes the interest ** accrued in an account that ** compounds interest annually. *\***********************************************************/

#include <stdio.h>

#define MAXPRIN 100000.00#define MINPRIN 0.00#define MAXRATE 1.00#define MINRATE 0.00#define MINYEARS 1#define MAXYEARS 100

void PrintInstructions (void) ;float GetPrincipal (float max, float min) ;float GetRate ( float max, float min) ;int GetYears (int max, int min) ;float CalculateAmount (float principal, float rate, int years ) ;void PrintReport (float principal, float rate, int years, float amount, float interest) ;

main ( ){

float principal, rate, amount, interest ;int years ;

PrintInstructions ( ) ;

/Get valid input from user */principal = GetPrincipal (MAXPRIN, MINPRIN) ;rate = GetRate (MAXRATE, MINRATE) ;years = GetYears (MAXYEARS, MINYEARS) ;

/* Do Calculations */amount = CalculateAmount (principal, rate, years) ;interest = amount - principal ;

PrintReport (principal, rate, years, amount, interest ) ;}

Page 21: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 21

Improved Interest with functionsProgrammed Incrementally, # 6

Write and test PrintReport( )

/*************************************************** PrintReport takes the original principal amount, the interest rate, amount of years of the* investment, amount in the account at the end of the term and the accrued interest as* arguments. It prints these values in a report format.\*************************************************/void PrintReport (float principal, float rate, int years, float amount, float interest) {

printf (“\n\n”) ;printf (“Interest rate : %.4f %%\n”, 100 * rate ) ;printf (“ Period : %d years\n\n”, years ) ;printf (“ Principal at start of period : %9.2f”, principal ) ;printf (“ Interest accrued : %9.2f”, interest ) ;printf (“Total amount at end of period : %9.2f”, amount) ;

}

Page 22: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 22

Interest : Output #6Test PrintReport ( )

This program computes the interest accrued in an account that compounds interest annually. You will need to enter the amount of the principal, the interest rate and the number of years.

Enter the principal amount : 1000.00Enter the interest rate as a decimal (for 7% enter .07) : .07Enter the number of years : 20

Interest rate : 7.0000 % Period : 20 years

Principal at start of period : 1000.00 Interest accrued : 2869.68 Total amount at end of period : 3869.68

Page 23: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 23

Problem: Dealing Cards

Deal a hand of cards to a player Write a program that deals a hand of

cards to a player assuring that no cards are repeated.

The user must supply the number of cards per hand.

Page 24: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 24

Algorithm: Dealing Cards

Print Explanation to user Get number of cards per hand from user for each card in the hand

o Randomly generate a cardo Assure that card hasn’t already been dealt.o Put the new card in the player’s hando Record this card as a dealt card

Print the suit and value of the cards in the player’s hand

Page 25: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 25

Dealing CardsThinking it through

Draw a card by randomly getting a suit, a number between 0 and 3, inclusive & randomly getting a card value (1 - 13).

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.

Keep track of already used cards in 4 arrays (same storage method as hand)

Page 26: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsDesign & 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[ ] );

Page 27: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 27

Dealing CardsWrite and test PrintExplanation( )

/*****************************************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** Description: This program deals a hand of cards to a player. The number of ** cards is specified by the user. *\*****************************************************************************************/

#include <stdio.h>

void PrintExplanation (void) ;

main ( ){ PrintExplanation ( ) ;}

/*************************************************** 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”);

}

Page 28: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test InitializeArrays( )

/************************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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) ;

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

Page 29: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 29

Dealing CardsWrite 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) ;

}

Page 30: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 30

Dealing CardsWrite 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 ;

}

}

Page 31: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test GetNumPerHand( )

/*****************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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) ;

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

Page 32: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 32

Dealing CardsWrite 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 < min || 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 ;}

Page 33: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test GetRandomSuit( )

& GetRandomValue( )/*****************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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) ;

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

Page 34: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 34

Dealing CardsWrite 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 ( ) % 13 + 1 ;

return value ;}

Page 35: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test AlreadyUsed( )

/*****************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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[ ]) ;

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 ( ) ; } }}

Page 36: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

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 = dealtDiamonds [value] ; break; case SPADES : used = dealtSpades [value] ; break; default : printf (“Error in suit in AlreadyUsed\n”) ; used = -1 ; } return used;}

Page 37: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test Deal( ) & RecordAsUsed( )

/**********************************************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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[ ]) ;

Page 38: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test Deal( ) & RecordAsUsed( )

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

Page 39: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 39

Dealing CardsWrite 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 : handDiamonds [value] = 1 ; break ; case SPADES : handSpades [value] = 1 ; break ; default : printf (“Error in suit in Deal\n”) ; }}

Page 40: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 40

Dealing CardsWrite 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 : dealtDiamonds [value] = 1 ; break ; case SPADES : dealtSpades [value] = 1 ; break ; default : printf (“Error in suit in RecordAsUsed\n”) ; }}

Page 41: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing Cards - Write and test PrintHand( )/**********************************************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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[ ] ) ;

Page 42: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing CardsWrite and test PrintHand( )

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

Page 43: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

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

Page 44: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 44

Dealing CardsWrite 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 ) ; } }}

Page 45: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing Cards - Final Version/**********************************************************************************************\* Filename: cards.c ** Author: Sue Bogar ** Date written: 12/4//99 ** 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 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[ ] ) ;

Page 46: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

Dealing Cards - Final Versionmain ( ){ 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 (handHearts, handClubs, handDiamonds, handSpades) ; RecordAsUsed (dealtHearts, dealtClubs, dealtDiamonds, dealtSpades); }

PrintHand (handHearts, handClubs, handDiamonds, handSpades ) ; }

Page 47: CMSC 1041 Examples Using Top-Down Design and Functions to Simplify Code and Create Modules

CMSC 104 47

Further Improvements ? We have a design error !

o We should have realized that Deal and RecordAsUsed would have same exact code.

o We 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.

o Wouldn’t this have been easier to handle if we had spotted it sooner ?

We should rewrite PrintHand( ) so that it prints Jack, Queen and King instead of 11, 12, & 13.

Anything else ?