# Thread: two dimensional array

1. ## two dimensional array

my project is :
Create a two dimensional array. The array will have 4 rows and 13 columns.
- Each row will be a different suit of cards.
- The columns will store integers that will represent the values of the card (e.g. 2, 3, 4...10, J, Q, K)
- Populate each row of the array appropriately and print out the value of the array so the output would look like this:

2D
2D
4D
5D
...
10D
JD
QD
KD

AH
2H

i have coded
Code:
```#include <stdio.h>
#include <stdlib.h>
#define VALUES 14
#define ACE 1
#define KING 13
#define QUEEN 12
#define JACK 11
enum suits{ clubs , diamonds , hearts , spades ,numOfSuits };
int main ( void )

{
int arry [numOfSuits] [ VALUES ]= { { 4 } , { 0,2,3,4,5,6,7,8,9,10,11,12,13 } };
for( int x = clubs; x < numOfSuits; x++ ) {
for ( int y = 0;y<VALUES;y++){
arry[x][y]=x*13+(y);
}
}
for( int x = clubs; x < numOfSuits; x++ ) {
for ( int y = 0;y<VALUES;y++){
switch ( arry [x][y]%13)
{
case ACE:		printf("A"); break;
case QUEEN:		printf("Q"); break;
case JACK:		printf("J"); break;
case KING:		printf("K"); break;
default:		printf("%d",arry[x][y]%13);
}
switch ( arry[x][y]/13 )
{
case clubs:		printf(" clubs\n"); break;
case diamonds:	printf(" diamonds\n"); break;
case hearts:	printf(" hearts\n"); break;

}
}
}

return 0;
}```
and it skips K output, what am i doing wrong?

2. If you divide a number by 13, how do you propose to get a remainder of 13?

3. ya i know i mssed the array up with 14, but i changed it and still no luck

4. switch ( arry[x][y]/13 ) takes [x][y], assume its 26, divide it by 13 to get 2, which would be hearts

5. the output of the current program is :
A clubs
2 clubs
3 clubs
4 clubs
5 clubs
6 clubs
7 clubs
8 clubs
9 clubs
10 clubs
J clubs
Q clubs
0 diamonds
A diamonds
2 diamonds
3 diamonds
4 diamonds
5 diamonds
6 diamonds
7 diamonds
8 diamonds
9 diamonds
10 diamonds
J diamonds
Q diamonds
0 hearts
A hearts
2 hearts
3 hearts
4 hearts
5 hearts
6 hearts
7 hearts
8 hearts
9 hearts
10 hearts
J hearts
Q hearts
0~

6. Ask yourself again: is the remainder of 26 divided by 13 equal to 13? No? Then your check for King won't work.

7. oh duhhh!!! any hint on how to correct it?

8. Check for the answer that you will get.

9. i still can't figure out how to fix this. i'm new to C , taking intro to programming, so i'm VERY green to it

10. Originally Posted by leisiminger
i still can't figure out how to fix this. i'm new to C , taking intro to programming, so i'm VERY green to it
I'm assuming you're more familiar with division. What do you get as the remainder when you divide 26 by 13? Use that as your KING.

11. i'm not sure i'm taking you correctly, but should i make a new switch statement exclusively for king? i very do appreciate your help

12. Originally Posted by leisiminger
i'm not sure i'm taking you correctly, but should i make a new switch statement exclusively for king? i very do appreciate your help
You already have an case for KING. However, the value of KING is not 13. You should define KING to be the right thing (remember the defines? at the top?) -- remember that the right thing comes from a remainder of a division.

13. This doesn't do anything like what you think it does:
Code:
`	int arry [numOfSuits] [ VALUES ]= { { 4 } , { 0,2,3,4,5,6,7,8,9,10,11,12,13 } };`
This is a two-dimensional array, with 4x14 = 56 elements and you are only initialising 13 of them, and incorrectly at that. Not to mention it should be 4x13 NOT 4x14! Arrays start at zero!
Decrease the value of every single one of your defines by 1.

Popular pages Recent additions