(C++) Counting duplicates in an array, and then moving that counted number to a corresponding position in a separate array [closed]

StackOverflow https://stackoverflow.com/questions/16705226

  •  30-05-2022
  •  | 
  •  

Question

I've just begun learning to code c++, so please bear with me, my knowledge is very very limited. This is my first program written from scratch. I'm writing a program that looks at a hand of five cards, and then tells the user it's type, such as royal flush, straight, etc.

First, I have the user input their hand, and then store that hand into an array.

What I need to do is then search through that array for duplicates (such as there being two 3's in the array), count the number of duplicates (2 in this instance), and then put that number 2 into another array at the corresponding position of the card.

For example: User has a hand of S11H11D11C11S10 (S - spades, H - Hearts, D- Diamond, and C - Clubs) The number 11 here represents a Jack, with an Ace being a rank 1 and a King a rank 13.

So the user has 4 jacks in their hand, and a 10 of spades.

I need the program to count those four jacks, and then put into another array at the 11th spot. This will tell me the user has four cards, and they are Jacks.

You can view my code here:

HEADER:

#ifndef SUIT_H_INCLUDED
#define SUIT_H_INCLUDED
#include <string>

using namespace std;


struct Card {
int rank1;
int rank2;
int rank3;
int rank4;
int rank5;

char suit1;
char suit2;
char suit3;
char suit4;
char suit5;


};

#endif // SUIT_H_INCLUDED

MAIN FUNCTION:

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

using namespace std;



int main()
{

int usrhand[10];


char a; //letters for declaring cards in hand
char b;
char c;
char d;
char e;

int f; //numbers for declaring rank of cards
int g;
int h;
int i;
int j;

Card hand; //initiates a structure for the players hand

    hand.suit1 = a;
    hand.suit2 = b;
    hand.suit3 = c;
    hand.suit4 = d;
    hand.suit5 = e;

    hand.rank1 = f;
    hand.rank2 = g;
    hand.rank3 = h;
    hand.rank4 = i;
    hand.rank5 = j;



cout << "Welcome to Card Sorter (TM) designed by ----" << endl;
cout << " " <<endl;
cout << "You will be prompted to enter your 5 card hand." <<endl;
cout << "I will prompt you for each card, one by one." <<endl;
cout << "I will ask you for the suit and then the rank of the card." <<endl;
cout << "Keep in mind: Spades=S, Hearts=H, Diamonds=D, Clubs=C" <<endl;
cout << "Also keep in mind the value of Ace is low, and is rank 1" <<endl;
cout << "While the value of a King is high and is rank 13." <<endl;
cout << "Let's begin." <<endl;
cout << " " << endl;
cout << "What is the suit of your first card (S,H,D,C)?" <<endl;
cin >> a;
cout << "What is the rank of your first card (1-13)" <<endl;
cin >> f;
cout << "What is the suit of your second card? (S,H,D,C)?" <<endl;
cin >> b;
cout << "What is the rank of your second card (1-13)" <<endl;
cin >> g;
cout << "What is the suit of your third card? (S,H,D,C)?" <<endl;
cin >> c;
cout << "What is the rank of your third card (1-13)" <<endl;
cin >> h;
cout << "What is the suit of your fourth card? (S,H,D,C)?" <<endl;
cin >> d;
cout << "What is the rank of your fourth card (1-13)" <<endl;
cin >> i;
cout << "What is the suit of your final card? (S,H,D,C)?" <<endl;
cin >> e;
cout << "What is the rank of your final card (1-13)" <<endl;
cin >> j;
usrhand[0] = a;
usrhand[1] = f;
usrhand[2] = b;
usrhand[3] = g;
usrhand[4] = c;
usrhand[5] = h;
usrhand[6] = d;
usrhand[7] = i;
usrhand[8] = e;
usrhand[9] = j;

cout <<" Lets take a look at your current hand. " <<endl;
printf("%c", usrhand[0]);
printf("%d", usrhand[1]);
printf("%c", usrhand[2]);
printf("%d", usrhand[3]);
printf("%c", usrhand[4]);
printf("%d", usrhand[5]);
printf("%c", usrhand[6]);
printf("%d", usrhand[7]);
printf("%c", usrhand[8]);
printf("%d", usrhand[9]);

//need to count duplicate ranks in this array (ie. player has 4 cards of the same value, let's say value is 5)
// and then place the counted value in another array at the
// correct position, the position that matches the value in this case (so array2[4] = 4)
// Then I will program something to examine the second array for its number values and print out the players hand
// (ie. array2[4] = 4, player has four 5's this is a Four of a Kind)

    return 0;
}
Was it helpful?

Solution

Since you only need to find duplicate ranks, you can do it like this:

int dupCounter[13];
for (int i=0; i<13; i++)
{
    dupCounter[i] = 0;
}
for (int i=0; i<5; i++)
{
    dupCounter[usrhand[i*2+1]-1] ++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top