mikeregas
asked on
permutation code for flipping a coin
I am trying to write a program that produce all possible out come of flipping a coin "x" number of times, it has to be recursive and it produce an out come similar to this:
coin flipped 2 times
HTHHTHTT
I have some of the code done, but the output is a little confusing to what it should be.
thanks.
coin flipped 2 times
HTHHTHTT
I have some of the code done, but the output is a little confusing to what it should be.
thanks.
#include <stdio.h>
char coinflip(char* s, int size, int pos);
int main(void)
{
char* s;
int size;
int pos;
printf("How many times will you be flipping a coin?\n");
scanf("%d", &size);
s=calloc(0, sizeof(char));
coinflip(s, size, 0);
printf("%s", s);
printf("Here are all your possible outcomes:\n");
system("pause");
return 0;
}
char coinflip(char* s, int size, int pos)
{
if(size==pos)
{
return 0;
//return coinflip(s, size, pos);
}
//posibility 1
printf("H");
return coinflip("H", size, pos+1);
//posibility 2
return coinflip("T", size, pos+1);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I understand what you are saying and I changed the flip function to what you suggested. However when I run the program and enter lets say 2 for flips it returns something way off like HH^2^2||||0F{... twice, why is that
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In the above example that you just gave me when the if returns 0; it goes to possibilty 1 and then possiblity 2 or does it just go to possibilty 1, because my out come is somewhat different than yours. it is:
HH
HH
HH
HH
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kdo
I feel like I am bugging the hell out of you, but you are the only one that has really helped me understand this stuff. The last thing is the last set of outcomes appears twice
ie.
HH
HT
TH
TT
TT
Got any ideas
I feel like I am bugging the hell out of you, but you are the only one that has really helped me understand this stuff. The last thing is the last set of outcomes appears twice
ie.
HH
HT
TH
TT
TT
Got any ideas
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
lol I know exactly what you mean. That was it, I entered 15 flips and the program stopped responding any ideas?
ASKER
actually as soon as I got to 8 flips it ran through it, it just didnt get to the end of the program. It said the program stopped responding
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is alot of outputs. I am doing this for calloc(): s=calloc(0, sizeof(char));
should the sizeof be different ?
should the sizeof be different ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so that would be s=calloc(0, sizeof(char+1));
or am I ttotally off on this
or am I ttotally off on this
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so I want it to be 0 * sizeof(char) = 1 bytes and in order to do that I would have to do this
s= (char*) calloc (size,sizeof(char)); am I getting close here?
s= (char*) calloc (size,sizeof(char)); am I getting close here?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think that I am making this more difficult than it should be, but I would think that it would be size+1.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so with size+1 the program should not crash?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry
s= (char*) calloc (size+1,sizeof(char));// this will keep it from crashing
s= (char*) calloc (size,sizeof(char));//or will this and I assume that the above would be the better choice when you get into the really high number of tosses correct. this one worked for 24 tosses it just takes a while to run through the program. I am testing the above one with 34 and as you can imagine it is still running
s= (char*) calloc (size+1,sizeof(char));// this will keep it from crashing
s= (char*) calloc (size,sizeof(char));//or will this and I assume that the above would be the better choice when you get into the really high number of tosses correct. this one worked for 24 tosses it just takes a while to run through the program. I am testing the above one with 34 and as you can imagine it is still running
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks you guys were awesome and you both really helped me out alot, I really appreciate it.
Mike
Mike
>> I am testing the above one with 34 and as you can imagine it is still running
I don't think the point is to go that far ;) The point of an assignment like this is, is to come up with the algorithm and a proper implementation. The actual result is less important (as long as it's correct of course ;) ).
If it's working for sizes up to 5 (the ouput is still easy to verify in that case), then you're probably ok :)
I don't think the point is to go that far ;) The point of an assignment like this is, is to come up with the algorithm and a proper implementation. The actual result is less important (as long as it's correct of course ;) ).
If it's working for sizes up to 5 (the ouput is still easy to verify in that case), then you're probably ok :)
ASKER