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.

Actually, permutations lend themselves to recursive solutions very well. :) Of course, for a sequence of binary results (like a coin flip) there is a very good iterative shortcut, but I suspect that this is homework -- hence he doesn't have a lot of choice in the implementation.

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

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

Except that the behavior of calloc is implementation defined when requesting allocation of a 0 size memory block. Generally it will return NULL, so you could have well set s to NULL instead. And if it returns a value different from NULL, you cannot use the memory it's pointing to.
So, I still don't see the point :)

>> I need to figure out how to search a binary tree that would have the results of the coin toss

I would suggest to draw an example tree (for a 3 coin toss for example) and see what it looks like. Figure out the best locations to show the results (where in the tree would you have access to all 3 tosses ?), and then implement the generation of that tree recursively.

This is pretty basic info, I know, but learning recursion is best done by figuring it out ... So, give it a try, and get back if you're stuck ...

Recursion in this case should be fairly easy to understand, if you can walk through what, exactly, is happening. And in this case, Infinity08's comment about this really being an iterative problem is a good place to start.

Let's think about a simple adder. The Process takes the lowest (rightmost) ordered bits, adds them, and notes any carry bit. It then moves left one bit, adds them and the carry bit, and notes any carry. The process is repeated until all of the bits are added.

Here's a quick illustration of the results of a 2-bit (pardon the pun) adder.

00 01 10 11
1 1 1 1
== == == ==
01 10 11 00

In 4 passes, the adder has solved all possible combinations involving the incrementing of a 2-bit value. In a sense, that is exactly what your recursive program is going to do. Except that instead of iterating (looping) on each bit, the recursion will happen in such a way that each call of the flip() function handles all (both) of the states for its assigned bit.

To start with, allocate a buffer for *s* with 1 more character than you're trying to solve. (If you want a 2-bit solution, allocate a 3 bit string.)

Now let's modify your flip() function just a bit. The recursion logic will be exactly what you've built. The only thing that needs help is what happens within the function.

We're going to build the output string so when we determine a value, we need to place it in the string. Once the value is stored in the string, we'll recursively call the function again to process the next character. When we have set a value for all of the characters, we'll print the string.

Take a look at the code below. It's only a small change from what you have, and should work just fine with the rest of your program.

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

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:

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

The second argument is the size of one element in bytes, and the first argument is the amount of elements you want to allocate memory for. The total amount of memory allocated will thus be the product of the two arguments. So, you're asking to allocate 0 * sizeof(char) = 0 bytes.

The recursion can be only *N* calls deep. Each recursive call (depth) sets T/H for the current character, puts it into the string, and calls the function again to handle the next character in the string.

So since we're building a string, we have to have a buffer large enough for the string. That includes the final 0 byte (string terminator).

So if you're generating a string of 15 characters, you're going to need a 16 character buffer. If you're generating a string of 7 characters, you're going to need an 8 character buffer.

Hint -- Immediately before you call calloc(), you get the value for size, which is the number of times the coin is to be flipped.

:)

Obviously, you're struggling with this, so....

If you want a buffer of 100 characters, you can have the compiler generate them with the statement:

char Buffer[100];

If you don't know how big the buffer needs to be so you plan to allocate it at run time, (just like you're doing here) you declare a pointer to the buffer and call calloc() or malloc() to assign memory to the buffer

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

>> 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 :)

0

Featured Post

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net. To view more iPhone tutorials, visit www.sdkexpert.net.
This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…

Summary:
This tutorial covers some basics of pointer, pointer arithmetic and function pointer.
What is a pointer:
A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…