0

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

5
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by tessen
0

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.

0

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

0

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.

0

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

0

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?

0

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.

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.