I have an assignment for class and I am having a bit of trouble.
I need to do a few things to improve the efficiency of this program. The first thing I am having trouble with is that we are supposed to modify the shuffle function to loop row by row and column by column through the array, touching every element once. I'm not really sure how to even start this, any help would be appreciated.
Program is as follows:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
#include <iomanip>
using std::setw;
int deck[ 4 ][ 13 ];
void shuffle();
void deal();
// Main
int _tmain(int argc, _TCHAR* argv[])
{
for ( int row = 0; row <= 3; row++)
{
for (int column = 0; column <= 12; column++)
{
deck[ row ][ column ] = 0;
}
}
srand(time(0));
shuffle();
deal();
}
// Shuffle Function
void shuffle()
{
int row;
int column;
for (int card = 1; card <= 52; card++)
{
do
{
row = rand() % 4;
column = rand() % 13;
} while( deck[ row ][ column ] != 0);
deck[ row ][column] = card;
}
}
// Deal Function
void deal()
{
static const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
static const char *face[ 13] =
{ "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
for (int card = 1; card <= 52; card++)
{
for (int row = 0; row <= 3; row++)
{
for (int column = 0; column <= 12; column++ )
{
if (deck[ row ][ column ] == card)
{
cout << setw( 5 ) << right << face[ column ]
<< " of " << setw( 8 ) << left << suit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
}
}
}
}
}