Like Tree7Likes

Bubble sort algorithm help please, in C

This is a discussion on Bubble sort algorithm help please, in C within the C Programming forums, part of the General Programming Boards category; I am making a poker hand evaluator, the coursework is to read a 5 hand input i.e 3H 4H 5H ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    35

    Bubble sort algorithm help please, in C

    I am making a poker hand evaluator, the coursework is to read a 5 hand input i.e 3H 4H 5H 6H 7H, then; 1, give a text description of the hand and 2 tell the score of the hand. I've split each card into 2 variables so we can looks for straights or flushes allot easier. I have card1value....card5value and card1suit...card5suit. I would like to eb able to put card1value..card5 value into an array soI can perform a bubble sort algorithm on them. How can I do this?

    By the way, I've only been taught C as part of a module at uni for 3 months, so I'm new to it, so I don't know if this is doable or just a limitation of C.

    Thank you ever so much for your help in advance

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Step 1 is to post the code you have now and your attempt at doing what you want to do.

    Question: So you want them in an array fine....what form are they in now? Single variables? It may be as simple as:
    struct card array[52];

    Most likely what you want can be achieved by simply changing the design of your program, not literally creating an array manually from scalar variables which would be kinda odd (you should just start with an array to begin with).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Perhaps the easy thing to do would be to start with
    Code:
    struct card {
        int rank;  // 2 through to J=11, Q=12, K=13, A=14
        char suit;  // HCDS
    };
    From there, you can have
    Code:
    struct card hand[5];
    where you can do things like
    Code:
    hand[0].rank = 3;
    hand[0].suit = 'C';
    With things in an array, you can then sort them easily.
    QuantumNut likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Code:
    #include <stdio.h>
    int main( void )
    {
       char inputtedhand[25];                       
       int index;
       int text;
       int tempvar;
       char tempsuit;
       int cardvalue, card1value, card2value, card3value, card4value, card5value;
       char cardsuit, card1suit, card2suit, card3suit, card4suit, card5suit;
       int card1text, card2text, card3text, card4text, card5text;
       int i,m;
       
       
        for( index==0; index<=5; index++)         
                       
                {for(i==0; i<=15; i+=3)
                  {
                  scanf( "%d", &inputtedhand[i]); 
                  
                  if (inputtedhand[i]=2)
                    { 
                       text= ("The two of"); 
                       tempvar=2; 
                    }        
                  else if (inputtedhand[i]=3)
                    {
                       text= ("the three of");
                       tempvar=3;
                     }
                  else if (inputtedhand[i]=4)
                    { 
                       text= ("the four of");
                       tempvar=4;
                    } 
                 else if (inputtedhand[i]=5)
                    {
                       text= ("the five of");
                       tempvar=5;
                    }
                 else if (inputtedhand[i]=6)
                    {
                       text= ("The six of");
                       tempvar=6; 
                    }
                 else if (inputtedhand[i]=7)
                    {
                       text= ("The seven of");
                       tempvar=7;
                    }    
                 else if (inputtedhand[i]=8)
                    {  
                       text= ("The eight of");
                       tempvar=8;
                    }  
                 else if (inputtedhand[i]=9)
                    {
                       text= ("The nine of");  
                       tempvar=9;
                    }
                 else if (inputtedhand[i]=0)
                    {
                       text= ("The ten of");
                       tempvar=10;
                    }
                else if (inputtedhand[i]="J")
                   {
                      text= ("the jack of");
                      tempvar=11;
                   }
                else if (inputtedhand[i]="Q")
                   {
                      text= ("the queen of");
                      tempvar=12;
                   }
                else if (inputtedhand[i]="K")
                   {
                      text= ("the kign of");
                      tempvar=13;
                   }
                else if (inputtedhand[i]="A")
                   {
                      text= ("the ace of");
                      tempvar=14;
                   }
                   
                   m=(i+1);
               
                  scanf("&d", &inputtedhand[m]);
                    {
                     if (inputtedhand[i]="C")
                        {
                           text=text+" Clubs";
                          
                        }
                     else if (inputtedhand[m]="D")
                        {
                           text=text+" Diamonds";
                          
                        }
                     else if (inputtedhand[m]="H")
                        {
                           text=text+" Hearts";
                           
                        }
                     else if (inputtedhand[m]="S")
                        {
                           text=text+" Spades";                       
                        }   
                   }
                      
                      
                       {
                         if (index=1)
                         {
                            tempvar=card1value;
                            tempsuit=card1suit;
                            text=card1text;                        
                         }  
                         else if (index=2)
                         {
                            tempvar=card2value;
                            tempsuit=card2suit;
                            text=card2text;
                         }  
                         else if (index=3)
                         {
                            tempvar=card3value;
                            tempsuit=card3suit;
                            text=card3text;
                         }  
                         else if (index=4)
                         {
                            tempvar=card4value;
                            tempsuit=card4suit;
                            text=card4text;
                         }   
                         else if (index=5)
                         {
                            tempvar=card5value;
                            tempsuit=card5suit;
                            text=card4text;   
                         }
                      }    
             }                                           /* i loop*/
          }                                              /* index loop */
          {
          /* displays the inputted hand example "The first card is the 2 of heart"*/
          scanf("%s", &card1text);
          printf("the first card is %c", card1text);
          scanf("%s", &card2text);
          printf("the second card is %c", card2text);
          scanf("%s", &card3text);
          printf("the third card is %c", card3text);
          scanf("%s", &card1text);
          printf("the forth card is %c", card4text);
          scanf("%s", &card1text);
          printf("the fifth card is %c", card5text);
          }
          
    
    }

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    That's is what i have at the moment, I want to put the 5 variables i have, the values for the 5 cards into an array to then perform a bubble sort on it. How do i put it into an array?

  6. #6
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Hey, BenBusy. University of portsmouth by any chance?

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Yes, and i saw a post of yours, but i couldn't work out how to send you a private message aha

  8. #8
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thats alright. Ive done mine by searching for multiplies, not sure if I have done it the correct way or not though :/ not really keen on the lecturer at all to be honest.

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    So again using a struct as Salem suggested would be the better choice here...you are probably not up to structs yet so instead it looks like you need to use Parallel arrays.

    Instead of
    Code:
       int cardvalue, card1value, card2value, card3value, card4value, card5value;
       char cardsuit, card1suit, card2suit, card3suit, card4suit, card5suit;
       int card1text, card2text, card3text, card4text, card5text;
    This
    Code:
      int cardvalue[5];
      int cardtext[5];
      char cardsuit[5];
    Then you use the "index" value that you are already using to refer to the current card, such as cardvalue[i], cardtext[i], cardsuit[i].
    Salem, SDH and QuantumNut like this.

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Is it possible to say:

    Code:
    int bubblesort [10]= card1value, card2value, card3value, card4value, card5value;

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,108
    Quote Originally Posted by BenBusby View Post
    Code:
    int bubblesort [10]= card1value, card2value, card3value, card4value, card5value;
    Something like this may work in a language like Python, but in C we have to do most things ourselves

    bubblesort[0] = card1value;
    bubblesort[1] = card2value;
    bubblesort[2] = card3value;
    bubblesort[3] = card4value;

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,108
    Quote Originally Posted by BenBusby View Post
    Code:
                {for(i==0; i<=15; i+=3)
                  {
                  scanf( "%d", &inputtedhand[i]); 
                  
                  if (inputtedhand[i]=2)
                    {
    You seem to be getting the equality operators reversed. '=' means to assign a value, and '==' means to compare for equality. The statement if (inputtedhand[i] = 2) assigns the value of 2 and then evaluates as true (0 is false, nonzero is true).

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Code:
    int main( void )
    {
       char inputtedhand[25];                       
       int index;
       int text;
       int tempvar;
       char tempsuit;     
       int cardvalue[5];
       int cardtext[5];
       char cardsuit[5];
       int i,m;
       
       
        for( index==0; index<=5; index++)         
                       
                {for(i==0; i<=15; i+=3)
                  {
                  scanf( "%d", &inputtedhand[i]); 
                  
                  if (inputtedhand[i]==2)
                    { 
                       text= ("The two of"); 
                       tempvar=2; 
                    }        
                  else if (inputtedhand[i]==3)
                    {
                       text= ("the three of");
                       tempvar=3;
                     }
                  else if (inputtedhand[i]==4)
                    { 
                       text= ("the four of");
                       tempvar=4;
                    } 
                 else if (inputtedhand[i]==5)
                    {
                       text= ("the five of");
                       tempvar=5;
                    }
                 else if (inputtedhand[i]==6)
                    {
                       text= ("The six of");
                       tempvar=6; 
                    }
                 else if (inputtedhand[i]==7)
                    {
                       text= ("The seven of");
                       tempvar=7;
                    }    
                 else if (inputtedhand[i]==8)
                    {  
                       text= ("The eight of");
                       tempvar=8;
                    }  
                 else if (inputtedhand[i]==9)
                    {
                       text= ("The nine of");  
                       tempvar=9;
                    }
                 else if (inputtedhand[i]==0)
                    {
                       text= ("The ten of");
                       tempvar=10;
                    }
                else if (inputtedhand[i]=="J")
                   {
                      text= ("the jack of");
                      tempvar=11;
                   }
                else if (inputtedhand[i]=="Q")
                   {
                      text= ("the queen of");
                      tempvar=12;
                   }
                else if (inputtedhand[i]=="K")
                   {
                      text= ("the kign of");
                      tempvar=13;
                   }
                else if (inputtedhand[i]=="A")
                   {
                      text= ("the ace of");
                      tempvar=14;
                   }
                   
                   m=(i+1);
               
                  scanf("&d", &inputtedhand[m]);
                    {
                     if (inputtedhand[m]=="C")
                        {
                           text=text+" Clubs";
                          
                        }
                     else if (inputtedhand[m]=="D")
                        {
                           text=text+" Diamonds";
                          
                        }
                     else if (inputtedhand[m]=="H")
                        {
                           text=text+" Hearts";
                           
                        }
                     else if (inputtedhand[m]=="S")
                        {
                           text=text+" Spades";                       
                        }   
                   }
                      
                      
                       {
                         if (index=1)
                         {
                            tempvar=cardvalue[index];
                            tempsuit=cardsuit[index];
                            text=cardtext[index];                        
                         }  
                         else if (index=2)
                         {
                            tempvar=cardvalue[index];
                            tempsuit=cardsuit[index];
                            text=cardtext[index];
                         }  
                         else if (index=3)
                         {
                            tempvar=cardvalue[index];
                            tempsuit=cardsuit[index];
                            text=cardtext[index];
                         }  
                         else if (index=4)
                         {
                            tempvar=cardvalue[index];
                            tempsuit=cardsuit[index];
                            text=cardtext[index];
                         }   
                         else if (index=5)
                         {
                            tempvar=cardvalue[index];
                            tempsuit=cardsuit[index];
                            text=cardtext[index];   
                         }
                      }    
             }                                           /* i loop*/
          }                                              /* index loop */
          {
          /* displays the inputted hand example "The first card is the 2 of heart"*/
          scanf("%s", &cardtext[1]);
          printf("the first card is %c", cardtext[1]);
          scanf("%s", &cardtext[2]);
          printf("the second card is %c", cardtext[2]);
          scanf("%s", &cardtext[3]);
          printf("the third card is %c", cardtext[3]);
          scanf("%s", &cardtext[4]);
          printf("the forth card is %c", cardtext[4]);
          scanf("%s", &cardtext[5]);
          printf("the fifth card is %c", cardtext[5]);
          }   
    
    }
    I followed the advice I got from Nonpuz, thank you btw So that's what I have now. Do know why if i input "AH 3H 4H 5H 6H" it returns "
    the first card is Athe second card is 3the third card is 4the forth card is 5the fifth card is 6"

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,108
    The line

    for( index==0; index<=5; index++)

    is not correct because you never initialized index. The general form of the for loop is normally

    for (ASSN; TEST; INCR)

    ASSN should be an assignment statement containing '='

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Forth line:
    int index; ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort algorithm - displaying content
    By scrfix in forum C Programming
    Replies: 7
    Last Post: 04-02-2012, 04:52 PM
  2. Bubble sort algorithm
    By Mr.Lnx in forum C Programming
    Replies: 37
    Last Post: 09-16-2011, 03:48 PM
  3. testing a bubble sort algorithm
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 12:00 AM
  4. Help, bubble-sort algorithm
    By webznz in forum C Programming
    Replies: 6
    Last Post: 10-30-2007, 01:28 AM
  5. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21