Home › Forums › C Programming › coin toss
- This topic has 3 replies, 2 voices, and was last updated 16 years, 2 months ago by GWILouisaxwzkla.
- AuthorPosts
- October 4, 2008 at 5:26 pm #2141MelodeeBreParticipant
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…
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<br />#include <stdio.h><br /><br />char coinflip(char* s, int size, int pos);<br /><br />int main(void)<br />{<br />char* s;<br />int size;<br />int pos;<br /><br />printf("How many times will you be flipping a coin?n");<br />scanf("%d", &size);<br /><br /><br /><br />s=calloc(0, sizeof(char));<br /><br />coinflip(s, size, 0);<br />printf("%s", s);<br /><br />printf("Here are all your possible outcomes:n");<br /><br /><br /><br />system("pause");<br />return 0;<br />}<br /><br />char coinflip(char* s, int size, int pos)<br />{<br />if(size==pos)<br />{<br />return 0;<br />//return coinflip(s, size, pos);<br />}<br /><br />//posibility 1<br /><br />printf("H");<br />return coinflip("H", size, pos+1);<br />//posibility 2<br /><br /><br />return coinflip("T", size, pos+1);<br />}<br />it is a little bit sloppy but that is just because i was trying a bunch of different things. thanx
- October 7, 2008 at 9:42 pm #3458GWILouisaxwzklaParticipant
This seems to work:
/****************************************************************
* File Name : c:programshelppermutations.cpp
* Date : August,1,2002
* Comments : new project
* Compiler/Assembler :
*
*
*
*
*
* Program Shell Generated At: 5:59:12 p.m.
=-=-=-=-=-=-=-=–=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >using namespace std;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
void coinPermutations ( int flips , char * permutations );
void coinPermutations ( char face , int flips , char * permutations );
//##################################################################################//main function ******************************
int main ( )
{
int numberOfFlips;cout << "enter number of flips: ";
cin >> numberOfFlips;
cout << "permutations: " << endl;
char * permutations = new char [ numberOfFlips ];
coinPermutations ( numberOfFlips , permutations );
delete permutations;return 0 ;
}/******************************* FUNCTION DEFINITION ******************************
Name : coinPermutatins
Parameters :face a(n) char
Returns: Void type
Comments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void coinPermutations ( int flips , char * permutations )
{permutations [ flips ] = 0;
coinPermutations ( ‘h’ , flips – 1, permutations );
coinPermutations ( ‘t’ , flips – 1, permutations );return;
}
/******************************* FUNCTION DEFINITION ******************************Name : coinPermutatins
Parameters :face a(n) char
Returns: Void type
Comments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void coinPermutations ( char face , int flips , char * permutations )
{if ( flips == 0 )
{
permutations [ flips ] = face;
cout << permutations << endl;
return;
}permutations [ flips ] = face;
coinPermutations ( ‘t’ , flips – 1 , permutations ) ;
coinPermutations ( ‘h’ , flips – 1 , permutations );return;
}output:
enter number of flips: 4
permutations:
ttth
htth
thth
hhth
tthh
hthh
thhh
hhhh
tttt
httt
thtt
hhtt
ttht
htht
thht
hhht
Press any key to continue - October 8, 2008 at 7:08 pm #3459GWILouisaxwzklaParticipant
Small change:
/****************************************************************
* File Name : c:programshelppermutations.cpp
* Date : August,1,2002
* Comments : new project
* Compiler/Assembler :
*
*
*
*
*
* Program Shell Generated At: 5:59:12 p.m.
=-=-=-=-=-=-=-=–=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >using namespace std;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
void coinPermutations ( int flips , char * permutations );
void coinPermutations ( char face , int flips , char * permutations );//##################################################################################
//main function ******************************
int main ( )
{
int numberOfFlips;cout << "enter number of flips: ";
cin >> numberOfFlips;
cout << "permutations: " << endl;
char * permutations = new char [ numberOfFlips + 1 ];
coinPermutations ( numberOfFlips , permutations );
delete [] permutations;return 0 ;
}/******************************* FUNCTION DEFINITION ******************************
Name : coinPermutatins
Parameters :face a(n) char
Returns: Void type
Comments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void coinPermutations ( int flips , char * permutations )
{permutations [ flips ] = 0;
coinPermutations ( ‘h’ , flips – 1, permutations );
coinPermutations ( ‘t’ , flips – 1, permutations );return;
}
/******************************* FUNCTION DEFINITION ******************************Name : coinPermutatins
Parameters :face a(n) char
Returns: Void type
Comments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void coinPermutations ( char face , int flips , char * permutations )
{if ( flips == 0 )
{
permutations [ flips ] = face;
cout << permutations << endl;
return;
}permutations [ flips ] = face;
coinPermutations ( ‘t’ , flips – 1 , permutations ) ;
coinPermutations ( ‘h’ , flips – 1 , permutations );return;
} - October 14, 2008 at 5:20 pm #3460GWILouisaxwzklaParticipant
heres a simpler version ( no driver function ). Seems to work fine – needs to be tested….
/****************************************************************
* File Name : c:programshelppermutations.cpp
* Date : August,1,2002
* Comments : new project
* Compiler/Assembler :
*
*
*
*
*
* Program Shell Generated At: 5:59:12 p.m.
=-=-=-=-=-=-=-=–=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/#include < iostream >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >using namespace std;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
void coinPermutations ( int flips , char * permutations );
//##################################################################################
//main function ******************************
int main ( )
{
int numberOfFlips;cout << "enter number of flips: ";
cin >> numberOfFlips;
cout << "permutations: " << endl;
char * permutations = new char [ numberOfFlips + 1 ];
permutations [ numberOfFlips ] = 0;
coinPermutations ( numberOfFlips – 1 , permutations );
delete [] permutations;return 0 ;
}/******************************* FUNCTION DEFINITION ******************************
Name : coinPermutatins
Parameters :face a(n) char
Returns: Void type
Comments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void coinPermutations ( int flips , char * permutations )
{if ( flips == 0 )
{
permutations [ flips ] = ‘t’;
cout << permutations << endl;
permutations [ flips ] = ‘h’;
cout << permutations << endl;
return;
}permutations [ flips ] = ‘t’;
coinPermutations ( flips – 1 , permutations ) ;
permutations [ flips ] = ‘h’;
coinPermutations ( flips – 1 , permutations );return;
}
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.