Home Forums C Programming coin toss

Viewing 3 reply threads
  • Author
    Posts
    • #2141
      MelodeeBre
      Participant

      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…

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

    • #3458
      GWILouisaxwzkla
      Participant

      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

    • #3459
      GWILouisaxwzkla
      Participant

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

    • #3460
      GWILouisaxwzkla
      Participant

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

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.