consecutive numbers

This is a discussion on consecutive numbers within the C Programming forums, part of the General Programming Boards category; Do I need to use bubble sort on the array first?...

  1. #16
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Do I need to use bubble sort on the array first?
    Last edited by SDH; 01-21-2013 at 04:08 PM.

  2. #17
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    Do I need to use bubble on the array first?
    Yes, you should sort array first. But don't forget to sort both arrays!

    If you assume that difference==4 means that user have straight it's wrong. Because 4h 4s 6d 6c 8c will give you diff==4 and it's not a straight. You need different approach.

    I also noticed that you have many variables like i, a or c that you use with for loop. Only i would be enough. You should keep your program less complex and more understandable.

  3. #18
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    ok thank you very much, what other method to work out a straight would you use instead?

  4. #19
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    1 sort arrays
    2 use int difference[4] instead int difference

    Post your code when you finish this!

  5. #20
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have just added bubble sort to the code and that works perfect. Where in the code do I add the new array?
    here is the code too
    Code:
    #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        int temp;
        int j;
        char array[5];
        char array1[5];
        n = 5;
        
        
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
        for (i=0;i<n; i++)
        {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
          }   
       }
       
       for(i=0;i<5;i++)
          printf("%c",array1[i]);
       printf("\n");
       /////////////////////////////////////////////////////////////////////////////////////
        /////////////////////BUBBLE SORT^/////////////////////////////////////////////
        }
        for (i = 0; i < n; i++) {
            count1[array[i]]++;    //if array[i] is N, then count1[N]++//
        }   
        for (i = 0; i < 127; i++) {
            if (count1[i] == 5) {
                printf("Flush.\n");
            }
        }
        for (a = 0; a < n; a++) {
            count2[array1[a]]++;    //if array[a] is N, then count1[N]++//
        }
        for (a = 0; a < 127; a++) {
            if (count2[a] == 5) {
                printf("Flush.\n");
            }else if (count2[a] == 4) {
                printf("Four of a kind.\n");
            }else if (count2[a] == 3) {
                printf("Three of a kind.\n");
            }else if (count2[a] == 2) {
                printf("Pair.\n");
            }else if (count2[a] && count2[a] && count2[a] == 2) {
                printf("Two Pair.\n");
            }
            
        }
        return 0;
        }

  6. #21
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    You are using int i, j, a and c with for loop. You do not need them all. You only need i and j. So get rid of extra variables, leave just the ones you really need. Try to implement new array by yourself(easiest way to learn), then post code.

  7. #22
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have an idea how to intialize it but I am not sure on where to put it in the code and how to write it in, any clues?

  8. #23
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    I have an idea how to intialize it but I am not sure on where to put it in the code and how to write it in, any clues?
    You didn't sort both arrays! Implement it after sort. After you implement it, use printf to see if you are getting correct value. I won't reply anymore if you don't post your solution. You should at least give a try, otherwise you won't learn.

  9. #24
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    here is what I have come up with so far. Am I getting any closer?7
    Code:
    #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        int temp;
        int j;
        int difference= 0;
        char array[5];
        char array1[5];
        n = 5;
        
        
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
        for (i=0;i<n; i++)
        {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
          }   
       }
       
       for(i=0;i<5;i++)
          printf("%c",array1[i]);
       printf("\n");
       
       for(i=0;i<n-1;i++) {
        difference=(array[i+1]-array[i]);
        printf("%d",difference);
        }
        }

  10. #25
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Initialize difference as int difference[4] and use it as difference[i] in for loop.

    Then if all elements of array called difference are 1 it's a straight.

    And again you failed to sort both arrays.

  11. #26
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    thanks for all the help you are giving me. For some reason it still wont work, where am going wrong on using bubble sort to sort both arrays?
    Code:
    #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        int temp;
        int j;
        char array[5];
        char array1[5];
        n = 5;
        
        
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
        for (i=0;i<n; i++)
        {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
          }   
       }
       for (i=0;i<n; i++)
        {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array[i] > array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
             }
          }   
       }
       }
       for(i=0;i<5;i++)
          printf("%c",array1[i]);
          printf("\n");
          printf("%c",array[i]);
        
        }
        for (i = 0; i < n; i++) {
            count1[array[i]]++;    //if array[i] is N, then count1[N]++//
        }   
        for (i = 0; i < 127; i++) {
            if (count1[i] == 5) {
                printf("Flush.\n");
            }
        }
        for (a = 0; a < n; a++) {
            count2[array1[a]]++;    //if array[a] is N, then count1[N]++//
        }
        for (a = 0; a < 127; a++) {
            if (count2[a] == 5) {
                printf("Flush.\n");
            }else if (count2[a] == 4) {
                printf("Four of a kind.\n");
            }else if (count2[a] == 3) {
                printf("Three of a kind.\n");
            }else if (count2[a] == 2) {
                printf("Pair.\n");
            }else if (count2[a] && count2[a] && count2[a] == 2) {
                printf("Two Pair.\n");
            }
            
        }
        return 0;
        }
    thank you very much

  12. #27
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    If you don't know how to do this things maybe this program is still to complex for you and you should learn with something easier than this. Also try with pen and pape. Write down your algorithm and then implement it step by step and use printf to see where are you getting.

  13. #28
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Just thought I would kep you updated, I have managed to get the code to work properly using a couple more loops and counts. Thanks for your help.

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

Similar Threads

  1. Replies: 10
    Last Post: 02-08-2012, 02:17 PM
  2. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  3. Consecutive one's problem
    By Samyx in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2009, 02:45 PM
  4. consecutive characters
    By stewie1986 in forum C Programming
    Replies: 1
    Last Post: 12-01-2007, 07:02 AM
  5. First Consecutive composites Help
    By ch4 in forum C Programming
    Replies: 10
    Last Post: 11-23-2007, 03:55 PM

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