Array Help

Write a program to simulate a deck of 52 playing cards.

Represent your deck as a 2D Array where the value in
the cell is the position of the card in the deck.

Represent the names for the suits and faces as
an array of Strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void main()
{
	const int ROWS = 4;
	string suits [ROWS] = 
	{
		"Diamonds", "Clubs", "Hearts", "Spades"
	};

	const int COLS = 13;
	string faces [COLS] = 
	{
			"Ace", "Deuce", "Three", "Four", "Five", "Six",
		"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"
	};

	int deck [ROWS][COLS] =
	{
		{1,2,3,4,5,6,7,8,9,10,11,12,13},
		{14,15,16,17,18,19,20,21,22,23,24,25,26},
		{27,28,29,30,31,32,33,34,35,36,37,38,39},
		{40,41,42,43,44,45,46,47,48,49,50,51,52}
	};

	
	


The problem: I am trying to connect the 1d arrays to the 2d arrays. Right now when I write:
cout << deck[3][5] << endl;
The output answer is 45. How do I populate the 2d array with the 1d arrays so that when I cout deck[3][5] I get it to say "Six of Spades"?
@MarkyMark

Your code could be compressed down quite a bit, to just one 2D array, like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Playing Cards.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>

using std::cout;
using std::endl;
using std::string;

void main()
{
 const int ROWS = 4;
 const int COLS = 14;
 srand((unsigned) time(0));
 string deck [ROWS][COLS] =
 {
	{"Diamonds","Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"},
	{"Clubs","Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"},
	{"Hearts","Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"},
	{"Spades","Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
 };
 for (int x=0;x<7;x++)
 {
	int card=rand()%13+1;
	int suit=rand()%4;
	cout << deck[suit][card] << " of " << deck[suit][0] << " .." << endl; 
 }
}
Last edited on
Hey man that edit was helpful! The way may teacher wants me to actually do the arrays though is by specifically using a 1D array of strings for the suits and a 1D array of strings for the faces and finally a 2D array of integers for the deck. I still cannot figure out how to populate the cells of the deck with the strings in the other two arrays. I know I would use a forward loop of some kind but I keep running into the error of the compiler not be able to populate an array of integers with arrays of strings. Any ideas?
Try this (pseudo-code)

1
2
3
4
5
6
7
8
9
10
Define class Card with members suit and value (both strings)
Define deck as a 2D array of Cards (ROWS x COLS)

FOR each ROW
    FOR each COL
        Populate Card at the appropriate location in the deck with:
            suit name from suit array
            value from faces array
    end FOR
end FOR
This is the only way I can see to use all three arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Card Deck.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
 bool ok;
 const int ROWS = 4;
 int card_face,card_suit,card;
 string suits [ROWS] = 
 {
	"Diamonds", "Clubs", "Hearts", "Spades"
 };

 const int COLS = 13;
 string faces [COLS] = 
 {
	"Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"
 };

 int deck [ROWS][COLS] =
 {
	{1,2,3,4,5,6,7,8,9,10,11,12,13},
	{14,15,16,17,18,19,20,21,22,23,24,25,26},
	{27,28,29,30,31,32,33,34,35,36,37,38,39},
	{40,41,42,43,44,45,46,47,48,49,50,51,52}
 };
 cout << "Enter a number for a card, 1 to 52" << endl;
 do
 {
	ok = false;
	cin >> card;
	for(int x=0;x<4;x++)
	 for (int y=0;y<13;y++)
		if ( deck[x][y] == card)
		 ok = true;
	if(!ok)
	 cout << "That card is not in this deck.." << endl << "Enter a different number" << endl;
 }while (!ok);
 card--;
 card_suit = card/13;
 card_face = card%13;

 cout << "You chose the " << faces[card_face] << " of " << suits[card_suit] << endl;
 return 0;
}
Last edited on
This is a really odd way to represent the cards and the deck. Use your original 2 arrays of strings - there's no sense duplicating values.

The key is to realize that the value of the card is represented by it's position in the array, and the position of that card in the deck is represented by the value in the array. This is backwards from anything that would make sense, but it's what your prof wants.

Given your main() function, you print out the cards like this:
1
2
3
4
5
6
for (int suit = 0; suit<ROWS; ++suit) {
    for (int value = 0; value<COLS; ++value) {
        cout << faces[value] << " of " << suits[suit] << " is card " << deck[suit][value] << '\n';
    }
}
cout.flush();
This question got my interest. It reminded me of previous questions on the web based on this same deck structure. This instructor or university has made the students from previous semesters solve problems with a deck of cards using this same structure.

dhayden said:
This is a really odd way to represent the cards and the deck.
Agreed. For example, when I wrote a function displayDeckInOrder(), I decided it was a reasonable place to use a goto to escape out of two for loops. [These are the same loops where whitenite1 used an OK flag.] Any time I have a place to use a goto, I know there is something wrong. Yes, I did rewrite to use a "found" flag but that complicates the for loop while conditions and the post loop uses of the loop counters.
Last edited on
1
2
3
4
5
6
7
int deck [ROWS][COLS] =
{
	{1,2,3,4,5,6,7,8,9,10,11,12,13},
	{14,15,16,17,18,19,20,21,22,23,24,25,26},
	{27,28,29,30,31,32,33,34,35,36,37,38,39},
	{40,41,42,43,44,45,46,47,48,49,50,51,52}
};

In this 2d array (or matrix), "deck", the value of each element can be calculated like deck[row][col] == row*10 + col + 1. Meaning such an array would have no use in a program. I doubt the professor would give you suck odd assignment, maybe you forgot to mention something?
Maybe I did. My class is today so I will see what I'm doing wrong and let you know if your interested.
Topic archived. No new replies allowed.