Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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

#include

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

int deck[4][13]={0};

int breakLoop;

while (1) {string card = random_card(verbose);

string card = random_card(verbose);

stringstream input;

string faceValue;

string bridgeWord;

string suitColor;

input << card;

input >> faceValue >> bridgeWord >> suitColor;

int faceTrack, suitTrack;

for(int a=0; a<4 ;a++){

if(suitColor == suit[a]){

suitTrack = a;

}

}

for(int b=0; b<13 ; b++){

if(faceValue == face[b])

{

faceTrack = b;

}

deck[suitTrack][faceTrack]++;

if(deck[0][10]!=0&&deck[1][11]!=0&&deck[0][12]!=0)

{breakLoop=0;

break;}

if(deck[1][10]!=0&&deck[1][11]!=0&&deck[1][12]!=0)

{breakLoop=1;

break;}

if(deck[2][10]!=0&&deck[2][11]!=0&&deck[2][12]!=0)

{breakLoop=2;

break;}

if(deck[3][10]!=0&&deck[3][11]!=0&&deck[3][12]!=0)

{breakLoop=3;

break;}

for(int i=0; i<4;i++)

{cout << setw(8) << suit[i] << ":";

for(int j=0;j<13;j++)

{cout<

if(j==13&&i==breakLoop)

{cout<<"**";

}

cout << endl;

}

}

}

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

Lords Of Finance The Bankers Who Broke The World

Authors: Liaquat Ahamed

1st Edition

0143116800, 978-0143116806

Students also viewed these Databases questions