Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Array numbers from the biggest to the smallest
    text
    copied!<p>i am a begginer programmer in c++. I found a very interesting exercise in an old c++ book, which doesn't have the solution for that exercise, so i hope you guys can help me:</p> <p>It wants me to create a deck of 52 cards, then shuffle the deck and deal the cards to 4 players: </p> <p>This is an example of the code: </p> <pre><code>cout &lt;&lt; "Player 1: "; for (int j=0; j&lt;13; j++) { card[j].display(); cout &lt;&lt; ", "; } </code></pre> <blockquote> <p>Player 1: 8, 7, 3, 2, A, K, 5, 4, Q, 9, 3, A, 2 </p> </blockquote> <p>Now it wants me to array those numbers from the biggest to smallest: A, A, K, Q, 9 ,8, 7,5 ,4, 3, 3,2, 2</p> <p>After having done a research on the internet, i learnt how to find the biggest number of an array, but i still don't know how to array those numbers from the biggest to smallest. I am using GNU Compiler. </p> <p>Thanks.</p> <p>For those who want to all the code, here it is:</p> <pre><code>#include &lt;iostream&gt; #include &lt;conio.h&gt; #include &lt;cstdlib&gt; #include &lt;ctime&gt; using namespace std; enum Suit {clubs, diamonds, hearts, spades}; class deck { private: int number; Suit suit; public: void setcards(int n, Suit s) { number = n; suit = s; } void display(); }; void deck::display() { if (number &gt;= 2 &amp;&amp; number &lt;= 10) cout &lt;&lt; number; else switch(number) { case 11: cout &lt;&lt; "J"; break; case 12: cout &lt;&lt; "Q"; break; case 13: cout &lt;&lt; "K"; break; case 14: cout &lt;&lt; "A"; break; } switch(suit) { case clubs: cout &lt;&lt; static_cast &lt;char&gt; (5); break; case diamonds: cout &lt;&lt; static_cast &lt;char&gt; (4); break; case hearts: cout &lt;&lt; static_cast &lt;char&gt; (3); break; case spades: cout &lt;&lt; static_cast &lt;char&gt; (6); break; } } class game { private: int j; enum {cardmax = 52}; deck card[cardmax]; public: game() { for (int j=0; j&lt;cardmax; j++) { int num = (j % 13) + 2; Suit su = Suit(j / 13); card[j].setcards(num, su); } } void shuffle() { char ans; cout &lt;&lt; "Would you like to shuffle the deck? (y/n): "; cin &gt;&gt; ans; if (ans == 'y') { srand(time(NULL)); for (j=0; j&lt;cardmax; j++) { int k = rand() % 52; deck temp = card[j]; card[j] = card[k]; card[k] = temp; } } else if (ans =='n') deal(); } void deal(); }; void game::deal() { const char esc = 27; const char enter = '\r'; char ans; cout &lt;&lt; "Press ESCAPE to exit the game or ENTER to deal: "; ans = getche(); if (ans == esc) exit(0); else if (ans == enter) { cout &lt;&lt; "\n\nPlayer 1: "; for (j=0; j&lt;13; j++) { card[j].display(); cout &lt;&lt; ", "; } cout &lt;&lt; "\nPlayer 2: "; for (j=13; j&lt;26; j++) { card[j].display(); cout &lt;&lt; ", "; } cout &lt;&lt; "\nPlayer 3: "; for (j=26; j&lt;39; j++) { card[j].display(); cout &lt;&lt; ", "; } cout &lt;&lt; "\nPlayer 4: "; for (j=39; j&lt;52; j++) { card[j].display(); cout &lt;&lt; ", "; } cout &lt;&lt; endl; cout &lt;&lt; "\nWould you like to deal again? (y/n): "; cin &gt;&gt; ans; if (ans == 'y') { shuffle(); deal(); } else if (ans == 'n') exit(0); } } int main() { game cards; cards.shuffle(); cards.deal(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload