// // Hmwk 31 // This program reads in 5 cards (9C, QD, etc) and writes them out sorted by suits // (clubs, diamonds, hearts, spades, and then within a group of cards of the // same suit by face value (lowest to highest, with ace being high). #include using namespace std; struct Card { char suit; // holds the suit of the card (C,D,S,H) int face; // holds 2-14 for 2..Ace }; // prototypes bool before(Card,Card); void print(Card*,int); void selectionSort(Card*, int); void fetchCards(Card*,int); int main() { Card* deck = new Card[5]; fetchCards(deck, 5); selectionSort(deck,5); print(deck,5); delete [] deck; // deallocate dynamic memory deck = NULL; // defensive programming return 0; } void fetchCards(Card* deck,int N) { char temp; cout << "Enter Cards: "; for(int i=0;i> temp; // Deal with the 10, have to read another char if (temp == '1') cin >> temp; cin >> deck[i].suit; // Just in case the user entered the suit in lower case. deck[i].suit = toupper(deck[i].suit); switch (temp) { case 'J': case 'j': deck[i].face=11; // jack break; case 'Q': case 'q': deck[i].face=12; // queen break; case 'K': case 'k': deck[i].face=13; // king break; case 'A': case 'a': deck[i].face=14; // ace break; case '0': deck[i].face=10; // The face was a 10 break; default: deck[i].face = temp - '0'; } // switch }// for } void selectionSort(Card *arrayIn, int N) { for(int length = N; length > 1; length--) { // Find imax, the index of the largest int imax = 0, i; for(i = 1; i < length; i++) if (before(arrayIn[imax],arrayIn[i])) imax = i; // Swap arrayIn[imax] & the last element Card temp = arrayIn[imax]; arrayIn[imax] = arrayIn[length - 1]; arrayIn[length - 1] = temp; } // for } void print(Card* deck,int N) { cout << "Cards sorted: "; for (int i=0;i