consecutive numbers

This is a discussion on consecutive numbers within the C Programming forums, part of the General Programming Boards category; I am trying to get a program to read a set of numbers from user input, and then from there ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    consecutive numbers

    I am trying to get a program to read a set of numbers from user input, and then from there manipulate the numbers and display messages depending on what numbers are present.

    For consecutive numbers I have thought of minus-ing one number from the other, then the difference would be 1, so consequently they would be consecutive. Or could do the highest number minus the lowest number.

    Now the problem I am having is I am unsure on how to get the code to read specific values of the array. The values of the array are obtained from user input.

    Here is the part of my code that I think would assist you in helping me identify how to target specific values within the array.

    Code:
     #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        char array[127];
        char array1[127];
        n = 5;
        for (c = 0; c < n; c++) {
            scanf(" %c %c ", &array1[c], &array[c]);
        }
        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");

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, if you want to associate a number with a text message, you could do it by storing up the messages, in a 2D char array. Then, because of the way arrays are stored in C, you can refer to any message by the outer dimension's index number:

    Code:
    messages[4][100]={
       {"Anything with cheese"},
       {"Pizza for me!"},
       {"Fish taco's!"}
       {"Fruit!"};
    
    printf(What is your selection for the next meal [0-3]?  ");
    fflush(stdout);
    scanf("%d", &choice);
    printf("you chose: %s \n",messages[choice]);
    If you only want to display a particular letter, you could use a 1D char array, instead of this 2D array. But same idea - use the input from the user, to associate with the index of the array.

    So now you have the "manipulate the numbers" part to work out.

    If this doesn't help, show an example of what you want. The description left me without a full understanding of what you wanted to do.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Why are you setting n to value 5? What do you need variable a for? You can use i all time instead of a. How many numbers should user input and in what range?

    And i agree with adak, I don't understand your problem fully.
    Last edited by yahzee; 01-20-2013 at 01:38 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    "manipulate the numbers and display messages depending on what numbers are present" is a bit too vague to understand what you need to do.
    Please give more detailed requirements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Sorry guys, I appreciate you trying to help me. I am trying to make a poker scoring system. So at the moment I am working on how to get straights, then from there I can combine it all together.

    So I have got the code to read the input " 4s 5h 6s 7d 8s" <- an example. So what I was going to do was take the second value and subtract the first from it, the difference would have to be for them to be next to eachother, then do the third and second etc etc etc. Then wanted to display a message saying 'straight'.

    But I am unsure how to use the input, because with my current code, it takes the user input and assigns it to a value in the array1[c]. What I am struggling with is how to access the data in the array, so how to say
    number2 - number1 = difference,
    number3 - number2 = differnece2
    number4 -number3 = difference3
    number - number4 = diffenrece4
    difference + difference1 + difference2 + difference3 + difference4 == 5
    printf straight

    That is not written in code, just an example of what I would like to say. But the numbers would need to be obtained from the array.

    Here is my whole code to help you out
    Code:
     include<stdio.h>int main(void)
    {
     
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        int message[13][100];
        char array[127];
        char array1[127];
        n = 5;
        for (c = 0; c < n; c++) {
            scanf(" %c %c ", &array1[c], &array[c]);
        }
        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] == 4) {
                printf("Four of a kind.\n");
            }else if (count2[a] == 2 && 3 ){
                printf("Full house. \n");   
            }else if (count2[a] == 3) {
                printf("Three of a kind.\n");
            }else if (count2[a] == 2) {
                printf("Pair.\n");
            }else if (count2[a] ==2 && 2) {
                printf("Two Pair.\n");
            }
           
        }
        return 0;
    }
    Thank you very much for all your help!!

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    number2 - number1 = difference,
    number3 - number2 = differnece2
    number4 -number3 = difference3
    number - number4 = diffenrece4
    difference + difference1 + difference2 + difference3 + difference4 == 5
    printf straight
    In order to calculate difference try to finish this by yourself:

    Code:
        int array[5];
        int i;
        int difference=0;
        
        for(i=0;i<5-1;i++) {
            array[i]    //This is 'i' element of array
            array[i+1]  //This element of array is after 'i'
        }
    Note that lines in for loop are there just for orientation. And you should know why is there 'i<5-1' instead of 'i<5'. Also difference should be 4, not 5.

    And did you consider what if input is not a number. What if user have straight like this: 10,J,Q,K,A.
    Last edited by yahzee; 01-21-2013 at 08:05 AM.

  7. #7
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I was going to use values of
    10=ten
    11=jack
    12=queen
    etc

    I have tried to write code for it, and so far I have wrote this
    Code:
     #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        int message[13][100];
        char array[127];
        char array1[127];
        int difference1;
        int difference2;
        int difference3;
        int difference4;
        int difference5;
        int number;
        
        n = 5;
        for (c = 0; c < n; c++){
            scanf(" %c %c ", &array1[c], &array[c]);
        }
        for (i = 0; i < n; i++) {
            count1[array[i]]++;    //if array[i] is N, then count1[N]++//
        }
        array1[c] = difference1;
        array1[c] = difference2;
        array1[c] = difference3;
        array1[c] = difference4;
        array1[c] = difference5;
        
        number = difference2 - difference1;
        printf("%d ", number);
    My input is 2c 3d 7s 9h j9
    So theoretically difference2 - difference 1 should equal 1, but instead I am getting -4197144. Is this the right method in how I am solving the problem and any ideas why I am receiving the value I am. Thanks

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Your scanf function is not properly implemented, because it doesn't stop on fifth input. Try this:

    Code:
        for (c = 0; c < n; c++){
            scanf("%c%c", &array1[c], &array[c]);
            getchar();
        }
    Note that getchar() eats blank spaces and new line.

    You didn't initialize difference to any value, so you don't know it's value therefore result is unpredictive. Look at this examples:

    Code:
        int difference;
        printf("%d", difference);
    Result of this was 4198625, but we can't know for sure.

    Code:
        int difference=5;
        printf("%d", difference);
    Now the result was 5. So you should assign values to your variables. This was the reason why you were getting that weird number.

    And why do you have so many variables for difference? Why don't you try with my previous advice? If it's not understandable I will explain it to you.

  9. #9
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    sorry I think I haven't explained what I want to do properly, I would like the difference to be calculate from the user input ( this will change every time). But what I am having trouble is using the arrays. I think now I have made it stop after 5, but I am still unsure how to get the first number, then second number from within the array, or would I simply be allowed to use the input again?
    Thank you very much

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    sorry I think I haven't explained what I want to do properly, I would like the difference to be calculate from the user input ( this will change every time). But what I am having trouble is using the arrays. I think now I have made it stop after 5, but I am still unsure how to get the first number, then second number from within the array, or would I simply be allowed to use the input again?
    Thank you very much
    You explained well. But it seems that you don't unerstand me.

    1. You have arrays with 127 elements, and you store only five element, so initialize your array on 5 elements
    2. Scan for user input
    3. Use for loop to iterate through array in order to find difference as I previously suggested:

    Code:
    for(i=0;i<5-1;i++) {
        array[i]    
        array[i+1]  
    }
    array[i] is first element when i=0, array[i+1] is second element when i=0, when i=1 array[i] is second and array[i+1] is third element. And so on.
    If you don't understand what you should do with this, ask.

  11. #11
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Ok thank you very much, I had trouble with an earlier version of the code and found that putting 127 elements in the array seemed to solve the problem, but have changed it to five now and its seems to be working fine.
    I partly understand what you are saying but not totally would you mind explaining a bit further for me, really appreciate the time you are spending with me.

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    I partly understand what you are saying but not totally would you mind explaining a bit further for me, really appreciate the time you are spending with me.
    When you want to calculate difference between all elements, you can use this code:

    Code:
        int i;
        int difference=0;
        
        for(i=0;i<n-1;i++) {
            difference+=(array[i+1]-array[i]);
        }
    But this code won't work for you. Do you know why? And do you understand this code above?

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    I had trouble with an earlier version of the code and found that putting 127 elements in the array seemed to solve the problem, but have changed it to five now and its seems to be working fine.
    You propably had problems with that because your scanf function wasn't properly implemented.

  14. #14
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I understand how that code work, but do not know why that won't work for me. Is it because I have two arrays?

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by SDH View Post
    I understand how that code work, but do not know why that won't work for me. Is it because I have two arrays?
    There are multiple reasons.

    What if user inputs: 5h 4s 3d 2c 6h?
    What if user inputs: 5h 6d 7c 7h 9s?

    Try printing result for this inputs.

Page 1 of 2 12 LastLast
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