Link to home
Start Free TrialLog in
Avatar of mikeregas
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.
#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);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikeregas
mikeregas

ASKER

I need this do be done recursively, and I understand that you can not just give me the answer. I just need some clear direction on what it is I am trying to accomplish. I am lost with recursion and it confuses the hell out of me. I need to figure out how to search a binary tree that would have the results of the coin toss, if it is 2 tosses or 100 tosses and I need to get it to print to the screen the resullts of the binary 1 for heads and 0 for tails. 1=H and 0=T
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol I know exactly what you mean.  That was it, I entered 15 flips and the program stopped responding any ideas?
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is alot of outputs. I am doing this for calloc():  s=calloc(0, sizeof(char));
should the sizeof be different ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so that would be  s=calloc(0, sizeof(char+1));
or am I ttotally off on this
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think that I am making this more difficult than it should be, but I would think that it would be size+1.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so with size+1 the program should not crash?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks you guys were awesome and you both really helped me out alot, I really appreciate it.
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 :)