So looking through my book and going over some exercises to prepare me for the next test, I came across a problem that I have no idea how to do. The guidelines were simple. Write a program that deals out two five card hands that will not repeat any cards twice.
However, the thing is, we can't use arrays. The problem says
"Your "main"function will call "rand()" to generate 10 numbers between 1 and 52. If there are any duplicate numbers, call rand() again to eliminate the duplicates.
You will write a function that takes a number between 1 and 52 and returns a character indicating one of the 4 suits: spades, hearts, diamonds or clubs.
You will write another function that takes the same number and returns a number between 1 and 13 to indicate the number of the card: ace (1) through king (13).
You can use any logic you like to decide on the suit and number, but be sure that your two functions will return 52 different possibilities for the 52 numbers"
If it was left up to me, I'd immediately go to arrays and I can't see a way to do it using just the guidelines. How would you start a program like this?
#include <iostream>
#include <vector>
#include <time.h>
usingnamespace std;
int rand( int min, int max );
int getCardSuit( int x );
int getCardNumber( int x );
int main()
{
srand( time(0) );
vector<int> vCards;
vCards.clear();
for( int i = 0; i < 52; i++ )
{
vCards.push_back(i);
cout << vCards.at(i) << " ";
char suit = getCardSuit( vCards.at(i) );
cout << suit << " ";
int number = getCardNumber( vCards.at(i) );
if ( number == 0 ) cout << 'A';
elseif ( number == 10 ) cout << 'J';
elseif ( number == 11 ) cout << 'Q';
elseif ( number == 12 ) cout << 'K';
else cout << number+1;
cout << endl;
}
cin.get();
return 0;
}
int rand( int min, int max )
{
int range = ( max - min ) + 1;
returnint(range*rand()/(RAND_MAX + 1.0)) + min;
}
int getCardSuit( int x )
{
return x%4+3;
}
int getCardNumber( int x )
{
return x%13;
}
I needed some practice with this anyways so I wrote this which basically uses vectors to display the card number, it's suit, and it's value
I'm still working on it, next post I should have it all completed for ya :)
#include <iostream>
#include <vector>
#include <time.h>
usingnamespace std;
int rand( int min, int max );
int getCardSuit( int x );
int getCardNumber( int x );
int main()
{
srand( time(0) );
vector<int> vCards; // creates a integer vector called vCards
vector<int>::iterator it; // creates an integer vector iterator
vCards.clear(); //initializes vector, clears everything inside of it
for( int i = 0; i < 10; i++ ) // ten cards, ten iterations
{
int tempCard = rand( 0, 51 ); // gets a random card
bool cardExists = false;
for( it = vCards.begin(); it < vCards.end(); it++ ) // check to see if it already exists in the vector
{
if( tempCard == *it ) //check if tempCard is the same as card at this location in vector
{
cardExists = true; // if so set bool to true, we use this to redo this iteration of the loop
}
}
if( cardExists == false )
{
vCards.push_back( tempCard ); //if it doesn't exist, add it as the next card in the vector
}
else
{
i--; // card already exists, redo this iteration of the loop
}
}
for( int i = 0; i < 10; i++ )
{
cout << i+1;
cout << " (card " <<vCards.at(i) << ") ";
char suit = getCardSuit( vCards.at(i) );
cout << suit << " ";
int number = getCardNumber( vCards.at(i) );
if ( number == 0 ) cout << 'A';
elseif ( number == 10 ) cout << 'J';
elseif ( number == 11 ) cout << 'Q';
elseif ( number == 12 ) cout << 'K';
else cout << number+1;
cout << endl;
}
cin.get();
return 0;
}
int rand( int min, int max )
{
int range = ( max - min ) + 1;
returnint(range*rand()/(RAND_MAX + 1.0)) + min;
}
int getCardSuit( int x )
{
return x%4+3;
}
int getCardNumber( int x )
{
return x%13;
}
there you go, this displays 10 random cards after making sure they don't already exist in the vector!
We try to encourage people to solve problems by themselves, and we help them if they're stuck in something in particular or they need some particular piece of information that is otherwise hard to find, such as specific code snippets.
We certainly don't write complete working programs. And for three reasons:
1. Problem solving is one of the basic skills used in computer programming. Doing other people's work doesn't help improve the skill of the one not doing work.
2. Doing other people's work also encourages freeloaders. They are annoying in a class and they are particularly annoying in the workplace. We try to get rid of them as soon as possible by not replying to blatant code requests ("solve this plz kthx" and the like).
3. Coding is an activity demanding large time resources. Many of us simply don't have time to do it even if the two above weren't true.
Just... don't do it. Tips, code snippets, help debugging, reading material, a push in the right direction, all that is fine. Complete programs, just don't.
PS: Oh, yeah. Another reason is that I could have been doing something a little more productive instead of explaining why we shouldn't write code for others. This isn't unproductive, but it is annoying.
What exactly is in your case the definition of an array? Is a string an array, an address? You would have in that case more or less the same card structure as in an array.
You could use a pointer to a certain adress and add for each card add your cardvalue to that adress.
As an alternative you could also use a bitfield.