Card game

(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;
}
Well your code starts by picking the same card 4 times.
So what's the right code Salem 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 am sorry, you're correct and thank you Salem C.

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.

Just help me out with a bit clear hint.

1
2
3
4
5
6
7
  sum=deck[i]+deck[j]+deck[k]+deck[l];
                    if (sum==24){
	cout << "sum=" << sum << ", C1=" << i << ", C2=" << j
     << ", C3=" << k << ", C4=" << l << endl;
                        c++;}
                        else
							sum=0;

sum=24, C1=0, C2=0, C3=8, C4=12


when i=0 deck[0]=1
when j=0 deck[0]=1
when k=8deck[8]=9
when l=12 deck[12]=13

1+1+9+13=24

C1=0, C2=0, C3=8, C4=25

deck[0]=1
deck[0]=1
deck[8]=9
deck[25]=13
24


Okay I get it when you say it picks 4 cards 4 times but isn't it supposed to be that way because a deck has 4 of every card?
Did you try
1
2
3
4
5
6
7
for(int i=0; i<siz; i++)
{
  for (int j=i+1; j<siz; j++)
  {
    for (int k=j+1; k<siz; k++)
    {
      for (int l=k+1; l<siz; l++)

There you go!
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!!
Topic archived. No new replies allowed.