hey guys, im trying to write a program recursively that takes in how many times the user wants to flip a coin, and prints out all the possibilities. i am having a hard time figuring it out. id apreciate some input. here is the code i have so far...

#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);
}

it is a little bit sloppy but that is just because i was trying a bunch of different things. thanx

If I am making heads or tails of what you're saying what you want it is called permutation.
There's lots of algorithms out there for it. Use you favorite search engine.

i understand the idea of the program, it is just putting it into code that i am having trouble with.

Is it part of the requirements to use recursive function? This program can be done easily without recursive function. Just use a loop is enough.

Also, check out rand() function to generate random number, and srand() for seeding the random generator.

it has to be recursive. thats why its giving me trouble. im still new at recursion

Is it part of the requirements to use recursive function? This program can be done easily without recursive function. Just use a loop is enough.

Also, check out rand() function to generate random number, and srand() for seeding the random generator.

Why would one need to generate a random number to display all the permutations?

Why would one need to generate a random number to display all the permutations?

My understanding of the question is that it is simulating the flipping of a coin, which is 50% head and 50% tail.

No he is wanting to print all the possible outcomes when a coin is flipped X times

I see....I intuitively think of simulation whenever I saw the flipping of coins or rolling of dice.

Don't forget that you should be printing a sequence in your base case. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.