Question: a labs homework script file is Prog2a.cpp #include #include using namespace std; const string face[] = { Ace, 2, 3, 4, 5, 6, 7, 8,
a labs homework
script file is Prog2a.cpp
#include
#include
using namespace std;
const string face[] = { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
const string suit[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
string random_card(bool verbose=false) {
string card;
card = face[ rand()%13 ];
card += " of ";
card += suit[ rand()%4 ];
if (verbose)
cout << card << " ";
return card;
}
int main(int argc, char *argv[])
{
bool verbose = false;
int seedvalue = 0;
for (int i=1; i string option = argv[i]; if (option.compare(0,6,"-seed=") == 0) { seedvalue = atoi(&argv[i][6]); } else if (option.compare("-verbose") == 0) { verbose = true; } else cout << "option " << argv[i] << " ignored "; } srand(seedvalue); // declare a table called deck that keeps track of // card faces dealt for each suit -- initialize to 0 while (1) {string card = random_card(verbose); // reverse engineer card suit and face // break out of loop if stopping criteria met } // print formatted table contents to stdout } You are given a code sketch for Part A of the assignment. This code sketch includes a near-empty main program that you fill in as well as two global arrays of strings representing the rank and suit of a card, and a function for generating random cards. Carry this code over from Part A to Part B. In fact, carry ALL the code from Part A over to Part B and modify what's needed to meet the change in specs. (The alternative is start over but that would be a waste of time.) Getting started and what you need to do To help you get started, run the Hydra script /home/cs140/labs/lab2/copy to obtain the following files: Prog2a.cpp (sketch code for Part A), prog2a, prog2b (Hydra solution executables), and a makefile. Your job is to write the missing source code which must behave as described next. The two executables take two optional arguments: -seed=N where N is a non-negative integer for seeding the random number generator; and -verbose causes the random_card() function to print each card to stdout before returning it to the calling function. Note: That there is no error checking on the command line arguments; if you feed the programs garbage, they may crash or get stuck in a infinite loop -- you get out of the latter by typing ctrl-C. Make Prog2a.cpp compile and do the following. Generate random cards until all the different face cards have been seen for a given suit (i.e., "Jack", "Queen", and "King"). then print a table showing how many cards of each suit and rank you were dealt along the way. Flag the suit that caused termination by adding "**" at the end its output line. The first step is to work out how to parse the string representing a card into suit and rank and translate those into the indices for the corresponding global string arrays. That is, reverse engineer what the random_card() function does. Caveat: Do not simply use integer division to reverse the modulo arithmetic, instead use string comparisons. Test the code by temporarily printing the suit and rank indices to stdout. Break out of the loop after some small number of iterations. The next step is to add a table that keeps track of which cards you are dealt (counts of suit and rank pairs). Implement this table using a static two-dimensional whose content you initialize to zero before entering the while loop. The table should have 4 rows and 13 columns corresponding to the fixed number of suits and the number of ranks, respectively. Test the code by printing the table to stdout as shown below after you break out of the loop. Again, do so after some small number of iterations. The last step is to replace the finite-number-of-iterations termination criterion with the one requested which is based on all face cards having been seen for a given suit. That is, step thru the table for each suit and set a Boolean variable to true if the condition is met. Use this variable to break out of the loop. Test your Prog2a exectuable using different seed values. Use the verbose command-line option to print the cards to stdout so you can double-check your table output. When the code works as intended, clean it up and add a few comments. Prog2a example outputUNIX> ./prog2a Clubs : 0 0 0 0 0 2 0 0 1 2 1 1 1 ** Diamonds : 1 0 1 0 0 1 0 1 1 0 3 0 0 Hearts : 0 0 1 0 0 0 1 0 1 0 1 0 1 Spades : 1 0 0 0 0 0 0 0 0 0 1 1 0 UNIX> UNIX> ./prog2a -seed=140 Clubs : 1 1 1 3 0 1 1 1 0 2 2 0 2 Diamonds : 3 1 2 0 2 0 1 1 2 2 1 2 2 ** Hearts : 1 3 1 1 1 1 2 0 0 3 3 0 0 Spades : 0 1 0 1 2 2 0 0 1 0 1 1 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
