(Game: pick four cards) Write a program that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. Below is the function I called.
sample output: The number of picks that yeilds the sum 24 is: 12517.
my output: 330496
[code]
int permut(const int deck[], int siz)
{
int sum,c=0;
for(int i=0; i<siz; i++)
{
for (int j=0; j<siz; j++)
{
for (int k=0; k<siz; k++)
{
for (int l=0; l<siz; l++)
{
sum=deck[i]+deck[j]+deck[k]+deck[l];
if (sum==24)
c++;
}
}
}
}
return c;
}
I'd expect you to spend more than 2 minutes thinking about what I said, rather than bouncing right back with the "feed me" response.
Just coding the first thing that comes into your head, then dumping it on a forum when it doesn't work is not a long term strategy. You need to THINK your way though your problems and learn to analyse how things go wrong.
One of those skills being the ability to debug code, either by using a debugger, or just putting lots of cout statements all over the place.
For example, start with a deck of just 4 cards, and add this after you calculate the sum.
1 2 3
sum = deck[i]+deck[j]+deck[k]+deck[l];
cout << "sum=" << sum << ", C1=" << i << ", C2=" << j
<< ", C3=" << k << ", C4=" << l << endl;
Now in reality, there is only one possible sum for 4 cards.
The question you might want to think about is why you have so many.
I spent some time trying to do it, splashing every code I could think of. that included assigning four arrays,size of 13 for club, diamond, spades and heart. and inside the 4th loop making sum=c[i]+d[j]+s[k]+h[l]. that yielded too less than sample, ie: 1291.
Tried to manipulate initial and final values of i,j,k,l that yeilded 1291.
Increased the value of i by 0, j by 1, k by 2, l by 3, to avoid picking same card, still too high number.
I probably don't understand the question, it says it picks 4 cards, does not say picks all possible four cards to make a sum of 24.
The deadline of submission has passed but I still want to do it right.
I tried right now and felt stupid because I had tried
1 2 3 4 5 6 7 8
for(int i=0; i<siz; i++)
{
for (int j=1; j<siz; j++)
{
for (int k=2; k<siz; k++)
{
for (int l=3; l<siz; l++)
Never even thought of the process you presented above in the reply. I will take some time and try to understand what it does.
there should be a section for super beginner lol. I'd probably belong there. But thanks for a new way of seeing things. I appreciate your time. Goes without saying, it worked. I'll keep posting in this forum from now on. Once again thanks Salem!!