Thread: Maths question

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

    Maths question

    Code:
    if ((royalflush)>=1){          
                printf("The hand is a Royal Flush\n"); handscore[counter]=(180+cardvalue[4]); break;
    Thats a rudimentary way of differentiating between the various poker hands; so here I can find the winner:

    Code:
    if(handscore[1]>handscore[2]){
           printf("\nPlayer 1 wins");  
       }else if (handscore[1]==handscore[2]){
           printf("\ndraw");
       }else if (handscore[2]>handscore[1]){
           printf("\nPlayer 2 wins");
       }else{
       printf("This shouldn't happen");
       }
    SO, why do i get -62 as the result, I've added this to my code, and it says the answer is -62. the cardvalue[4] is definitely 14

    Code:
     printf("\n%d\n", cardvalue[1=4]);
    The numbers are sorted so term 4 in the array cardvalue is the largest number

    the variable 'counter' counts 1,2 in a while loop to run the algorithm twice for both inputted hands.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    There's a problem with your code, cardvalue[1=4] is a syntax error.

    Assuming this isn't the problem, there's obviously something wrong with your logic in assigning card values.

    1) C arrays start at 0, are you sure you are always using zero-based indices?
    2) Are you initialising the values correctly?
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Do you want me to post the full code? I'm sure its correct until the like '180+....' line..

    i wrote
    Code:
    printf("\n%d\n", cardvalue[1=4]);
    in here, so the mistake was just a typo, nits fine in my actual code..

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Code:
      #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
                                      
        int main (void)
        {
        char inputtedhand[64];
            int NumC=0, NumD=0, NumS=0, NumH=0;
             
            int handcount=0;
            int tempvar;
            char cardvalue[10];
                                         
            int i=0, n=0, m=0, z=0;
            int index=0, counter=0;
            
            char handscore[5];
            
         
        while(counter++<2)
        {
          scanf("%15c", inputtedhand);
          memset(cardvalue,'\0',sizeof(cardvalue));
          NumS=0, NumD=0, NumC=0, NumH=0, index=0;
          
          if(counter==1)
          {
          printf("The hand for Player 1 is:\n");
          }else if (counter==2){
          printf("\nThe hand for player 2 is:\n");
          }
          
          for(i=0; i<15; i+=3)
          {
          m=(i+1); //SUIT
          n=(m+1); //SPACE
          inputtedhand[i]=toupper(inputtedhand[i]);
          inputtedhand[m]=toupper(inputtedhand[m]);
                  if (inputtedhand[i]==50){
                            printf( "The two of "); 
                            tempvar=2;
                            /* The the first value if a 2, "The two of" will be dsplayed at the output, and the number 2 will be stored as a temporary variable for later */
                  }else if (inputtedhand[i]==51){
                            printf( "The three of ");
                            tempvar=3;               
                  }else if (inputtedhand[i]==52){             
                            printf( "The four of "); 
                            tempvar=4;               
                  }else if (inputtedhand[i]==53){               
                            printf("The five of ");
                            tempvar=5;               
                  }else if ( inputtedhand[i]==54){               
                            printf("The six of "); 
                            tempvar=6;               
                  }else if ( inputtedhand[i]==55){
                             printf("The seven of "); 
                             tempvar=7;               
                  }else if ( inputtedhand[i]==56){
                            printf("The eight of "); 
                            tempvar=8;               
                  }else if (inputtedhand[i]==57){
                            printf("The nine of "); 
                            tempvar=9;               
                  }else if ( inputtedhand[i]==48){
                             printf("The ten of "); 
                             tempvar=10;               
                  }else if (inputtedhand[i]==74){
                             printf("The Jack of "); 
                             tempvar=11;               
                  }else if (inputtedhand[i]==81){
                            printf("The Queen of "); 
                            tempvar=12;
                  }else if (inputtedhand[i]==75){
                             printf("The King of "); 
                             tempvar=13;               
                  }else if (inputtedhand[i]==65){
                             printf("The Ace of "); 
                             tempvar=14;               
                  }else{               
                             printf("%c",inputtedhand[i]); printf("is not recognised ");
                  }             
                                   
                  if (inputtedhand[m]==67){
                             printf("Clubs\n");  
                             NumC++;
                             /* This will complete the name for the first card, eg. "The two of Clubs" and counts the number of clubs there has been, this will be useful then looking for flushes */
                  }else if (inputtedhand[m]==68){
                             printf("Diamonds\n");
                             NumD++;                   
                  }else if (inputtedhand[m]==72){
                             printf("Hearts\n"); 
                             NumH++;
                  }else if (inputtedhand[m]==83){   
                             printf("Spades\n");
                             NumS++;                   
                  }else{                 
                             printf(" SUIT ");  printf("%c",inputtedhand[m]);  printf(" not recognised\n");
               }
               cardvalue[index]=tempvar;
               index++;     
          } //End of FOR loop
    int flush=0; 
          if((NumH==5) || (NumC==5) || (NumD==5) || (NumS==5)){
             flush=1;
          }else{
             flush=0;
          }         
          
    int outside, inside, first, second;
    {
    for(outside=4; outside>0; outside--)
        {
        for(inside=0; inside<outside; inside++)
          {
          
             first= cardvalue[inside];
             second= cardvalue[inside+1];
                if (first>second)
                    { 
                       cardvalue[inside]=second;
                       cardvalue[inside+1]=first;                
                    }
          }
         }   
    } 
       printf("%d", cardvalue[0]);
       printf(" (0)\n");
       printf("%d", cardvalue[1]);
       printf(" (1)\n");
       printf("%d", cardvalue[2]);
       printf(" (2)\n");
       printf("%d", cardvalue[3]);
       printf(" (3)\n");
       printf("%d", cardvalue[4]);
       printf(" (4)\n"); 
    
    int pair=0, pair1=0, pair2=0, pair3=0, pair4=0;
    
    if(cardvalue[0]==cardvalue[1]){
          pair1++; pair++;
    }if(cardvalue[1]==cardvalue[2]){
          pair2++; pair++;
    }if(cardvalue[2]==cardvalue[3]){
          pair3++; pair++;
    }if(cardvalue[3]==cardvalue[4]){
          pair4++; pair++;   
    }
    
    int straight=0, straightflush=0, royalflush=0, fourkind=0, fullhouse=0, threekind=0, twopair=0, highcard=1;
    
        if((cardvalue[0]+1==cardvalue[1]) && (cardvalue[1]+1==cardvalue[2]) && (cardvalue[2]+1==cardvalue[3]) && (cardvalue[3]+1==cardvalue[4]))
       {
           if(flush==1)
          {
              if (cardvalue[0]==10)
             {
                 royalflush++;
                // printf("getting here royalflush");
             }else{
                 straightflush++;
                //printf("getting here straightfflush");
                  }
          }else{
              straight++;
             //printf("getting here straight");
          }
       }else{
               if ((pair1==1 && pair2==1 && pair3==1) || (pair4==1 && pair3==1 && pair2==1)){
                  fourkind++;            
               }else if((pair1==1 && pair2==1 && pair4==1) || (pair1==1 && pair3==1 && pair4==1)){
                  fullhouse++;              
              }else if ((pair1==1 && pair2==1) || (pair2==1 && pair3==1) || (pair3==1 && pair4==1)){
                  threekind++;
                 }else if ((pair1==1 && pair3==1) || (pair1==1 && pair4==1) || (pair2==1 && pair4==1)){
                  twopair++;
                  }else if(flush==1){
                 flush++;
         }else{ 
             highcard++;
          }
          }
          
          while(1)
          {
            if ((royalflush)>=1){          
                printf("The hand is a Royal Flush\n"); 
                handscore[counter]=(180+cardvalue[4]); break;
            }else if (straightflush==1){         
                printf("The hand is the Straight Flush\n"); 
                handscore[counter]=160+cardvalue[3]; break;         
            }else if (fourkind>=1){         
                printf("The hand is a Four of a Kind\n");
                handscore[counter]=140+cardvalue[4]; break;         
            }else if (fullhouse>=1){     
                printf("The hand is a Full House\n"); 
                handscore[counter]=120+cardvalue[4]; break;         
            }else if (flush>=1){         
                printf("The hand is a Flush\n");
                handscore[counter]=100+cardvalue[4]; break;         
            }else if (straight>=1){         
                printf("The hand is a Straight\n"); 
                handscore[counter]=80+cardvalue[3]; break;         
            }else if (threekind>=1){       
                printf("The hand is  a Three of a Kind\n"); 
                handscore[counter]=60+cardvalue[4]; break;         
            }else if (twopair>=1){         
                printf("The hand is a Two Pair\n");
                handscore[counter]=40+cardvalue[4]; break;         
            }else if (pair>=1){         
                printf("The hand is a Pair\n");
                handscore[counter]=20+cardvalue[4]; break;         
            }else if (highcard>=1){         
                printf("\n The hand is a High Card\n"); 
                handscore[counter]=cardvalue[4]; break;         
            }else{       
                printf("\n The hand scores 0 (this should not happen)");
                    /* This is to ensure the above has been executed as planned, if this comes uop then there is an error */
            }
          }
        
          
    } //End of WHILE loop
        if(handscore[0]>handscore[1]){
           printf("\nPlayer 1 wins");  
       }else if (handscore[0]==handscore[1]){
           printf("\ndraw");
       }else if (handscore[1]>handscore[1]){
           printf("\nPlayer 2 wins");
       }else{
       printf("This shouldn't happen");
       }
          
         printf("\n%d\n", handscore[1]);     
         printf("%d", handscore[0]);
       
    }//End of program

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Anyone have any ideas?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    [
    Code:
    else if (handscore[1]>handscore[1]){
    This will never happen

    Kurt

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    A development process
    Then break your 200+ line main() into more manageable blocks of code.

    Second, if you're going to have massive functions, then indentation is absolutely vital.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void)
    {
      char inputtedhand[64];
      int NumC = 0, NumD = 0, NumS = 0, NumH = 0;
    
      int handcount = 0;
      int tempvar;
      char cardvalue[10];
    
      int i = 0, n = 0, m = 0, z = 0;
      int index = 0, counter = 0;
    
      char handscore[5];
    
    
      while (counter++ < 2) {
        scanf("%15c", inputtedhand);
        memset(cardvalue, '\0', sizeof(cardvalue));
        NumS = 0, NumD = 0, NumC = 0, NumH = 0, index = 0;
    
        if (counter == 1) {
          printf("The hand for Player 1 is:\n");
        } else if (counter == 2) {
          printf("\nThe hand for player 2 is:\n");
        }
    
        for (i = 0; i < 15; i += 3) {
          m = (i + 1);              //SUIT
          n = (m + 1);              //SPACE
          inputtedhand[i] = toupper(inputtedhand[i]);
          inputtedhand[m] = toupper(inputtedhand[m]);
          if (inputtedhand[i] == 50) {
            printf("The two of ");
            tempvar = 2;
            /* The the first value if a 2, "The two of" will be dsplayed at the output, and the number 2 will be stored as a temporary variable for later */
          } else if (inputtedhand[i] == 51) {
            printf("The three of ");
            tempvar = 3;
          } else if (inputtedhand[i] == 52) {
            printf("The four of ");
            tempvar = 4;
          } else if (inputtedhand[i] == 53) {
            printf("The five of ");
            tempvar = 5;
          } else if (inputtedhand[i] == 54) {
            printf("The six of ");
            tempvar = 6;
          } else if (inputtedhand[i] == 55) {
            printf("The seven of ");
            tempvar = 7;
          } else if (inputtedhand[i] == 56) {
            printf("The eight of ");
            tempvar = 8;
          } else if (inputtedhand[i] == 57) {
            printf("The nine of ");
            tempvar = 9;
          } else if (inputtedhand[i] == 48) {
            printf("The ten of ");
            tempvar = 10;
          } else if (inputtedhand[i] == 74) {
            printf("The Jack of ");
            tempvar = 11;
          } else if (inputtedhand[i] == 81) {
            printf("The Queen of ");
            tempvar = 12;
          } else if (inputtedhand[i] == 75) {
            printf("The King of ");
            tempvar = 13;
          } else if (inputtedhand[i] == 65) {
            printf("The Ace of ");
            tempvar = 14;
          } else {
            printf("%c", inputtedhand[i]);
            printf("is not recognised ");
          }
    
          if (inputtedhand[m] == 67) {
            printf("Clubs\n");
            NumC++;
            /* This will complete the name for the first card, eg. "The two of Clubs" and counts the number of clubs there has been, this will be useful then looking for flushes */
          } else if (inputtedhand[m] == 68) {
            printf("Diamonds\n");
            NumD++;
          } else if (inputtedhand[m] == 72) {
            printf("Hearts\n");
            NumH++;
          } else if (inputtedhand[m] == 83) {
            printf("Spades\n");
            NumS++;
          } else {
            printf(" SUIT ");
            printf("%c", inputtedhand[m]);
            printf(" not recognised\n");
          }
          cardvalue[index] = tempvar;
          index++;
        }                           //End of FOR loop
        int flush = 0;
        if ((NumH == 5) || (NumC == 5) || (NumD == 5) || (NumS == 5)) {
          flush = 1;
        } else {
          flush = 0;
        }
    
        int outside, inside, first, second;
        {
          for (outside = 4; outside > 0; outside--) {
            for (inside = 0; inside < outside; inside++) {
    
              first = cardvalue[inside];
              second = cardvalue[inside + 1];
              if (first > second) {
                cardvalue[inside] = second;
                cardvalue[inside + 1] = first;
              }
            }
          }
        }
        printf("%d", cardvalue[0]);
        printf(" (0)\n");
        printf("%d", cardvalue[1]);
        printf(" (1)\n");
        printf("%d", cardvalue[2]);
        printf(" (2)\n");
        printf("%d", cardvalue[3]);
        printf(" (3)\n");
        printf("%d", cardvalue[4]);
        printf(" (4)\n");
    
        int pair = 0, pair1 = 0, pair2 = 0, pair3 = 0, pair4 = 0;
    
        if (cardvalue[0] == cardvalue[1]) {
          pair1++;
          pair++;
        }
        if (cardvalue[1] == cardvalue[2]) {
          pair2++;
          pair++;
        }
        if (cardvalue[2] == cardvalue[3]) {
          pair3++;
          pair++;
        }
        if (cardvalue[3] == cardvalue[4]) {
          pair4++;
          pair++;
        }
    
        int straight = 0, straightflush = 0, royalflush = 0, fourkind = 0, fullhouse = 0, threekind =
            0, twopair = 0, highcard = 1;
    
        if ((cardvalue[0] + 1 == cardvalue[1]) && (cardvalue[1] + 1 == cardvalue[2])
            && (cardvalue[2] + 1 == cardvalue[3]) && (cardvalue[3] + 1 == cardvalue[4])) {
          if (flush == 1) {
            if (cardvalue[0] == 10) {
              royalflush++;
              // printf("getting here royalflush");
            } else {
              straightflush++;
              //printf("getting here straightfflush");
            }
          } else {
            straight++;
            //printf("getting here straight");
          }
        } else {
          if ((pair1 == 1 && pair2 == 1 && pair3 == 1) || (pair4 == 1 && pair3 == 1 && pair2 == 1)) {
            fourkind++;
          } else if ((pair1 == 1 && pair2 == 1 && pair4 == 1)
                     || (pair1 == 1 && pair3 == 1 && pair4 == 1)) {
            fullhouse++;
          } else if ((pair1 == 1 && pair2 == 1) || (pair2 == 1 && pair3 == 1)
                     || (pair3 == 1 && pair4 == 1)) {
            threekind++;
          } else if ((pair1 == 1 && pair3 == 1) || (pair1 == 1 && pair4 == 1)
                     || (pair2 == 1 && pair4 == 1)) {
            twopair++;
          } else if (flush == 1) {
            flush++;
          } else {
            highcard++;
          }
        }
    
        while (1) {
          if ((royalflush) >= 1) {
            printf("The hand is a Royal Flush\n");
            handscore[counter] = (180 + cardvalue[4]);
            break;
          } else if (straightflush == 1) {
            printf("The hand is the Straight Flush\n");
            handscore[counter] = 160 + cardvalue[3];
            break;
          } else if (fourkind >= 1) {
            printf("The hand is a Four of a Kind\n");
            handscore[counter] = 140 + cardvalue[4];
            break;
          } else if (fullhouse >= 1) {
            printf("The hand is a Full House\n");
            handscore[counter] = 120 + cardvalue[4];
            break;
          } else if (flush >= 1) {
            printf("The hand is a Flush\n");
            handscore[counter] = 100 + cardvalue[4];
            break;
          } else if (straight >= 1) {
            printf("The hand is a Straight\n");
            handscore[counter] = 80 + cardvalue[3];
            break;
          } else if (threekind >= 1) {
            printf("The hand is  a Three of a Kind\n");
            handscore[counter] = 60 + cardvalue[4];
            break;
          } else if (twopair >= 1) {
            printf("The hand is a Two Pair\n");
            handscore[counter] = 40 + cardvalue[4];
            break;
          } else if (pair >= 1) {
            printf("The hand is a Pair\n");
            handscore[counter] = 20 + cardvalue[4];
            break;
          } else if (highcard >= 1) {
            printf("\n The hand is a High Card\n");
            handscore[counter] = cardvalue[4];
            break;
          } else {
            printf("\n The hand scores 0 (this should not happen)");
            /* This is to ensure the above has been executed as planned, if this comes uop then there is an error */
          }
        }
      }                             //End of WHILE loop
    
      if (handscore[0] > handscore[1]) {
        printf("\nPlayer 1 wins");
      } else if (handscore[0] == handscore[1]) {
        printf("\ndraw");
      } else if (handscore[1] > handscore[1]) {
        printf("\nPlayer 2 wins");
      } else {
        printf("This shouldn't happen");
      }
    
      printf("\n%d\n", handscore[1]);
      printf("%d", handscore[0]);
    }                               //End of program
    Three, beware of uninitialised variables.
    Code:
        if (counter == 1) {
          printf("The hand for Player 1 is:\n");
        } else if (counter == 2) {
          printf("\nThe hand for player 2 is:\n");
        }
    Here, you seem to know that counter is either 1 or 2.

    But here...
    Code:
      if (handscore[0] > handscore[1]) {
        printf("\nPlayer 1 wins");
      } else if (handscore[0] == handscore[1]) {
        printf("\ndraw");
      } else if (handscore[1] > handscore[1]) {
        printf("\nPlayer 2 wins");
      } else {
        printf("This shouldn't happen");
      }
    You're testing 0 and 1
    Now 0 is garbage for sure.

    Four, beware of overflow
    > handscore[counter] = (180 + cardvalue[4]);
    Now for some odd reason, you have an array of chars for this, which might limit you to -128 to 127 (notice this is less than 180).

    Consider making it an array of ints.
    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.

  8. #8
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    If you're experiencing unexpected results, debug your program until something happens that is unexpected. Then carefully examine expression and variables involved. If you still can't figure it out, post:
    1) the unexpected action
    2) where it happens
    3) what you expected to happen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths question ?????
    By ssharish2005 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-26-2008, 05:53 PM
  2. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 01:13 PM
  3. Maths Question about Braces ({[]})
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2004, 10:40 PM
  4. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 01:04 PM
  5. quick maths question sorry
    By anthonye in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-28-2002, 08:25 PM