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' );
				}
			}
		}
	}
}

It makes the 2D array 'deck' a global variable, right? i just started my C++ class last week so I'm still learning.

I'm not sure, so the shuffle is more thorough? I took this code from the book, and then my assignment was to fix a few things to make it more efficient.

I have a couple of code snippets that may help regarding shuffling:

http://www.daniweb.com/code/snippet1034.html
http://www.daniweb.com/code/snippet1019.html

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' );
				}
			}
		}
	}

There is an awful lot of looping and if-statements going on here which can be avoided if you have a way of getting from card to deck , which you do not currently have. Right now you have:

"Given a suit and a value, you can find where it is in the deck."

What you don't have is:

""Given the place in the deck, what suit and value do you have?",

which is why you are stuck going through all of them. You may want to redesign so that you can go both directions or at least go the direction you need to go.

Your shuffle seems backwards to me. Assuming card values mean something like
1 = 2 of clubs
2 = 3 of clubs
...
13 = A of clubs
14 = 2 of Diamonds
...
52 = A of Spades

I'd change the shuffle to something like this...

Fill an array with 1-52 representing an unshuffled deck
Get a random number between 0 and 51. This represents a position in this array.
Move that value to your deck array. Just go from one to the next in deck.
Remove the 'used' card by moving each and every value after position into the previous location (value in position+1 into position all the way through the end of the array.)
Do it again but this time getting the random number from 0-50 (only 51 cards left.)
Keep repeating until you've moved each and every value.

I managed to get the shuffle function to work, although the output is a little messed up. I just have one more issue to work out. After the dealing algorithm locates and deals the card, the algorithm continues searching the remainder of the deck. Could someone point out where in the code this happens, I don't really understand it.

I managed to get the shuffle function to work, although the output is a little messed up. I just have one more issue to work out. After the dealing algorithm locates and deals the card, the algorithm continues searching the remainder of the deck. Could someone point out where in the code this happens, I don't really understand it.

Please post your updated code and maybe an example of what is occurring.

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

int row;
int column;

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()
{
	for (int card = 1; card <= 52; card++)
	{

		for (int row = 0; row < 4; row++)
		{
			for (int column = 0; column < 13; column++)
			{
				int randCol = rand() % 4;
				int randRow = rand() % 13;
	           
				int placeholder = deck[row][column];
				deck[row][column] = deck[randCol][randRow];
				deck[randCol][randRow] = placeholder;

			}
		}

		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' );
				}
			}
		}
	}
}

Eight of Spades Ace of Clubs
King of Hearts Four of Spades
Five of Spades
Ten of Hearts Eight of Diamonds
Seven of Spades Queen of Diamonds
Eight of Hearts King of Diamonds Jack of Clubs
Seven of Hearts
Nine of Clubs Seven of Clubs
Jack of Diamonds Three of Diamonds
Nine of Diamonds Ten of Spades
Ten of Clubs Eight of Clubs Nine of Spades
Five of Hearts Six of Hearts
Two of Clubs Five of Diamonds
Queen of Spades Three of Spades
Five of Clubs Ace of Hearts
Press any key to continue . . .

Same problem as before. You have no way to get from card to deck, so you have to go through 52 if statements. You may want to have a card array of 52 integers, and do as Walt P suggested (i.e. 2 represents 3 of Clubs), so if

card[0] = 2;
card[1] = 5;

then the first two cards would be 3 of clubs, followed by 6 of clubs. You also have a problem here, I think:

for (int row = 0; row < 4; row++)
		{
			for (int column = 0; column < 13; column++)
			{
				int randCol = rand() % 4;
				int randRow = rand() % 13;
	           
				int placeholder = deck[row][column];
				deck[row][column] = deck[randCol][randRow];
				deck[randCol][randRow] = placeholder;

			}
		}
               deck[ row ][column] = card;

The loop variable column is out of scope here so you are using the global variable column. Is that what you want?

Alright I managed to get the program working right, thank you everyone for your help!

This question has already been answered. Start a new discussion instead.