### Checking for duplicate values in 2D array

Hi, I need help on checking duplicate values on a user input 2D array. I have this code so far and I am stuck on how to move on forward.

What I want it to do:
To check if the user entered 2 card values that are the same (the suit values doesn't matter if its the same). And if it is the same ask them for a new card value.

What it does:
After I enter my first card value, my code to let the user know they entered 2 same values pops up.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#define _CRT_SECURE_NO_WARNINGS #define col 5 #define row 2 #include #include #include void main(void) { int card[row][col]; int i, j, l, k; char units[20] = "cards"; printf("\Please enter the card value followed by its suit\n"); for (j = 0; j < col; j++) { for (i = 0; i < row; i++) { int rest = 13; if (i == 1) { strcpy(units, "suits"); rest = 4; } scanf("%i", &card[i][j]); while (card[i][j] > rest || card[i][j] < 1) { printf("\nOnly enter %s value between 1 to %i.\n", units, rest); scanf("%i", &card[i][j]); } for (k = 0; k < row; k++) { for (l = 0; l < col; l++) { while (card[k][j] == card[i][j]) { printf("You entered 2 identical cards, try again:\n"); scanf("%i", &card[i][j]); } } } } } printf("\nThe card value and suit that you've chosen are:\n"); for (i = 0; i < row; i++) { if (i == 1) { printf("\n"); } for (j = 0; j < col; j++) { printf("%i ", card[i][j]); } } while (!_kbhit()); }``````
Last edited on
please explain in moar words what it does not do correctly vs what you want it to do.

what is a card, is it an int from {0-52? something like this? something else?}

if its a standard 52 card deck (which it seems to be) you can just make a counter, eg
int counter[52] = {0};
and when the user puts a card in, ++ that card:
counter[card_ID]++;
and then
if(counter[card_ID] > 1) //they put in a duplicate, deal with it and set counter[card_ID] back to 1.

this could make the task much easier than having to loop over all the cards looking for matches etc. say the 2 of clubs is card #3. you have none, they put one in, you have 1, they put another one in later, you catch it without loops or searching its just known to already be in use.
Last edited on
Oh okay, i just edited my question

For the cards it's a user input value that goes in a 2D array (2 rows and 5 columns)
the 1st row is for the values and the 2nd row is for suit.
eg.

1st row: 1 9 8 13 1
2nd row: 3 2 1 1 3

What I want my code to do is to check if the user entered duplicate values (in this case its the value "1") and tell them to enter a new value since it can't be duplicate.

Last edited on
Assuming that you are wedded to C, maybe something like the following.
Note that a struct is much better than a 2d array.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #define HAND_SIZE 5 #define NUM_VALUES 13 #define NUM_SUITS 4 typedef struct Card { int value; int suit; } Card; void read_card(Card *card) { for (;;) { int v, s; scanf("%d %d", &v, &s); if (v >= 1 && v <= NUM_VALUES && s >= 1 && s <= NUM_SUITS) { *card = (Card){v, s}; break; } printf("Invalid card. Try again:\n"); } } void print_cards(Card *cards, int size) { for (int c = 0; c < size; ++c) printf("%2d %d\n", cards[c].value, cards[c].suit); } int equal(Card *a, Card *b) { return a->value == b->value && a->suit == b->suit; } int main() { Card hand[HAND_SIZE]; printf("Enter the card value followed by its suit\n"); for (int c = 0; c < HAND_SIZE; ++c) { read_card(&hand[c]); for (int i = 0; i < c; ++i) if (equal(&hand[c], &hand[i])) { printf("Duplicate card. Try again.\n"); --c; } } print_cards(hand, HAND_SIZE); return 0; }``````

for the approach I said, if you defined suits 0-3 and cards 1-13 (ace, 2..10, jack, queen, king) you can map as I said above into a table of 'used cards'. int deck[13*4+1], and any given card is deck[suit*13+faceval]. I suggest you name the constants with some enums, eg enum suits{clubs, ..}