Question: In c++ in the following code #include #include #include using namespace std; const int MAX_SIZE = 20; // Card struct to store card information struct

In c++ in the following code

#include #include #include

using namespace std;

const int MAX_SIZE = 20;

// Card struct to store card information struct Card { char suit; string rank; };

// SortedList class using dynamic allocated arrays class SortedList { private: Card* cards; int size; int capacity; public: SortedList() { capacity = MAX_SIZE; cards = new Card[capacity]; size = 0; }

~SortedList() { delete[] cards; }

// Add new card to list in sorted order void putItem(Card card) { // If list is full, resize if (size == capacity) { capacity *= 2; Card* newCards = new Card[capacity]; for (int i = 0; i < size; i++) { newCards[i] = cards[i]; } delete[] cards; cards = newCards; }

int i = size - 1; while (i >= 0 && compareTo(card, cards[i]) < 0) { cards[i + 1] = cards[i]; i--; } cards[i + 1] = card; size++; }

// Remove card from list void deleteItem(Card card) { int i = 0; while (i < size && compareTo(card, cards[i]) != 0) { i++; } if (i < size) { for (int j = i; j < size - 1; j++) { cards[j] = cards[j + 1]; } size--; } }

// Get card from list Card getItem(Card card) { int i = 0; while (i < size && compareTo(card, cards[i]) != 0) { i++; } if (i < size) { return cards[i]; } else { return {' ', ""}; } }

// Print all cards in list void printAll() { for (int i = 0; i < size; i++) { cout << cards[i].suit << cards[i].rank; if (i < size - 1) { cout << ","; } } cout << endl; }

// Compare two cards based on their suit and rank int compareTo(Card c1, Card c2) { if (c1.suit < c2.suit) { return -1; } else if (c1.suit > c2.suit) { return 1; } else { try { int rank1 = stoi(c1.rank); int rank2 = stoi(c2.rank); if (rank1 < rank2) { return -1; } else if (rank1 > rank2) { return 1; } else { return 0; } } catch (const std::invalid_argument& e) { // Set rank fields to empty strings c1.rank = ""; c2.rank = ""; return 0; } } } }; int main() { string fileName; cout << "Enter a file name: "; cin >> fileName; // Create new SortedList instance SortedList* myList = new SortedList();

// Read cards from file and add them to list ifstream inputFile; inputFile.open(fileName); if (!inputFile) { cout << "Error: Could not open file." << endl; return 1; } char suit; string rank; // Step 1: Read the first 20 cards in the first line of the file, then put them into the list using putItem() for (int i = 0; i < 20 && inputFile >> suit >> rank; i++) { Card card = {suit, rank}; myList->putItem(card); } // Step 2: Print all cards in list myList->printAll();

// Step 3: Delete cards indicated in second line of file using deleteItem() while (inputFile >> suit >> rank) { Card cardToDelete = {suit, rank}; myList->deleteItem(cardToDelete); } // Step 4: Add items in third line to list using putItem() while (inputFile >> suit >> rank) { Card cardToAdd = {suit, rank}; myList->putItem(cardToAdd); } inputFile.close();

// Step 5: Search for elements in list using getItem() and output results cout << "C9 " << (myList->getItem({'C', "9"}).rank == "" ? "NO" : "YES") << ", "; cout << "C10 " << (myList->getItem({'C', "10"}).rank == "" ? "NO" : "YES") << endl;

// Step 6: Print all cards in list myList->printAll();

delete myList;

return 0; } im not getting the desired output

follow the following steps

1. Create a list by dynamic allocated array and set the size to 20 2. Read the first 20 cards in the first line of the file, the put them one by one into the list by implementing and using putItem(). The list must be kept sorted in ascending order. Then print out all the cards in the list in one line separating by commas. 3. Then delete the cards indicated in the second line of the file by using deleteItem() Then print out all the cards in the list in one line separating by commas. 4. Then put the items in the third line in to the list. Must use putItem() Then print out all the cards in the list in one line separating by commas. 5. Search the current list for the elements in the list. Then output the result as the follows. Yes or No depends on whether the card exists in the current list. Must implement and use getItem() C9 NO, C10 YES 6. A printAll() function should be defined and called in order to output all the contents in the list. 7. A compareTo() function must be defined and used to compare which card is greater, less, or equal.

to achieve right output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!