Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

For 50 points, 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.

For 100 points, write the non-existent Prog2b.cpp code and have it keep track of the order in which a card rank is observed for each suit. You do this by inserting the cards at the back of linked lists, using one list for each suit. The exception applies: each time a rank is seen again, the card gets moved to the front of the list. When the stopping criterion from Prog2a is encountered, break the infinite-loop and print the contents of each linked list to stdout as shown below.

Add an array of linked lists: the length of the array is fixed at 4 like before, while the length of each linked list varies from suit to suit. Each new card is added to the appropriate list thru use of an insert() function. Declare a list class based on a single linked list. The list class needs to define a private node datatype, and it must include a constructor for properly initializing an empty list, a destructor for deallocating all the nodes in a list as well as the list itself, and the mentioned insert function which works as described next; no other list class functions are needed. Overload operator<<() used by ostream and have it print the contents of the list. Since access is needed to private data members, make the overloaded output operator a friend of the list class. See the code handouts for how to set this up.

The list::insert() function is where the fun work takes place. The function takes a single integer argument, namely, the rank index of a card where index refers to the position of the rank in the global rank string array. If the rank index is not held by any node in the linked list, a new node is created and added to the end of the list that stores the rank index value. However, if a node is found to already hold the present rank index argument, that node is moved to the front the linked list. That is, the node in question is unlinked from where it is and inserted after the head node.

Hint: You are using a single linked list which means you cannot go back once you have a match on the rank index. One option is to look ahead instead of advancing and then taking a look a the rank index. Another option is to maintain two pointers, namely, one pointing to previous node and one pointing to current node.

Hint: Write generic code that works under all circumstances rather than have several codes for special cases. For example, write the insert() function so that it can handle a rank match regardless of where the matching node resides in the list. Draw a sketch of the different scenarios that need to be handled and infer from it how to do it generically.

Prog2a example output

UNIX> ./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 

Prog2b example output

UNIX> ./prog2b Clubs : 10 6 Queen King 9 Jack ** Diamonds : Jack 9 6 Ace 3 8 Hearts : 9 3 King 7 Jack Spades : Queen Ace Jack UNIX> UNIX> ./prog2b -seed=140 Clubs : 10 Jack King 4 2 Ace 7 6 3 8 Diamonds : King 3 Ace 9 Queen 5 10 8 7 2 Jack ** Hearts : Jack 2 10 7 6 3 Ace 5 4 Spades : 6 5 Jack 4 9 Queen 2 

Try the above with the -verbose option on. Then try using a different seed.

Please help! Thanks a lot!!!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions