Thread: Program prints out extra numbers that are not entered in the console.

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Program prints out extra numbers that are not entered in the console.

    Load integers in the interval [ 10 , 99 ] . Loading should stop when you enter a number outside the given interval . Then print the number of times each number was loaded from the given interval , it should print out only those numbers that were loaded at least once

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define GG 99
    #define DG 10
    
    
    int main(void) {
        
        int number, i;
        int counter[GG - DG + 1] = { 0 };
        
        do {
            
            printf("Enter the number from interval [%d, %d]: ",    DG, GG);
            scanf("%d", &number);
            
        if (number >= DG && number <= GG) {
        
            counter[number]++;
            
            }
        
        }while (number >= DG && number <= GG);
    
    
    
    
        for (i = DG; i <= GG; ++i) {
            
            if (counter[i] > 0) {
                
                printf("\nNumber %d has been entered %d times\n",i, counter[i]);
                
                }
        }
    return 0;    
    }
    So if you enter for example 10,10,20,30,1000 it will write correctly that number 10 has been entered 2 times, number 20 1 time and number 30 1 time , but it will also print out message it for numbers 90,91,92,94,96 and 98.
    I really don't understand why does it load those numbers, for example if i = 90 , it is going to check if counter[90] > 0 and obviously counter[90] = 0 so if statement is = 0 and it isn't supposed to print anything , but still it does.
    Last edited by ZeroesAndOnes; 03-27-2016 at 12:58 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You're gonna kick yourself!
    What are the indices allowed for your counter array?
    They are 0 to 90.
    You need to subtract DG from your entered numbers to use them as an index.
    You need to add DG back to the index when you print it out.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by algorism View Post
    You're gonna kick yourself!
    What are the indices allowed for your counter array?
    They are 0 to 90.
    You need to subtract DG from your entered numbers to use them as an index.
    You need to add DG back to the index when you print it out.
    Since I am new into arrays could you please explain to me why is it so that I have to subtract DG from entered numbers to use them as an index,because If I enter number 20, 20-DG is 10 so it will be saved at index 10,shouldn't be logical that no.20 is saved in index 20.I thought my array needs only 89 places since I am entering numbers from 10 to 89

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Hmmm, okay. (I hope you kicked yourself anyway.)

    Sure, 10 to 99 is 90 numbers, so you only need 90 elements. But an array declared as int counter[90] must be indexed with 0 to 89. It's not going to magically know that when you say counter[92] it should store that in counter[82] (it can't store it in counter[92] because there is no counter[92]).

    You of course have the option of declaring counter as int counter[100] so that you can use your numbers directly as indices. This would become idiotic if DG was 1010 and GG was 1099, for example. I thought you were trying to minimize the wasted storage but with the particular values of DG and GG you're using, the wasted storage is not important.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by algorism View Post
    Hmmm, okay. (I hope you kicked yourself anyway.)

    Sure, 10 to 99 is 90 numbers, so you only need 90 elements. But an array declared as int counter[90] must be indexed with 0 to 89. It's not going to magically know that when you say counter[92] it should store that in counter[82] (it can't store it in counter[92] because there is no counter[92]).
    After this , I made facepalm ,it's an array of course ,I feel so ashamed right now for posting such a stupid post because I just realised how stupid my mistake was .

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by ZeroesAndOnes View Post
    After this , I made facepalm ,it's an array of course ,I feel so ashamed right now for posting such a stupid post because I just realised how stupid my mistake was .
    Now that you've given yourself the kick, realize that it could happen to anyone.

    When I'm really stuck on a bug it's almost always the case that I have some unwarranted assumption stuck in my head which causes me not to see it. Another pair of eyes is one of the most powerful debugging tools since the other person hasn't been staring at the code for an hour and so hasn't established any assumptions.

    To solve such a bug yourself you need to break out of the assumption, which can be difficult, but the first step is to realize that you probably have such an assumption.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by algorism View Post
    Now that you've given yourself the kick, realize that it could happen to anyone.

    When I'm really stuck on a bug it's almost always the case that I have some unwarranted assumption stuck in my head which causes me not to see it. Another pair of eyes is one of the most powerful debugging tools since the other person hasn't been staring at the code for an hour and so hasn't established any assumptions.

    To solve such a bug yourself you need to break out of the assumption, which can be difficult, but the first step is to realize that you probably have such an assumption.
    I couldn't agree more with you.I will take that advice from you, because that's so right , sometimes I do random things like walking or working out and in some moments I am like ' wait that was wrong with my algorithm'. It is like when you can't remember where you left your cellphone or keys , and you remember it later on doing some other random thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program prints random numbers not the sum
    By tlxxxsracer in forum C Programming
    Replies: 5
    Last Post: 11-08-2015, 10:08 PM
  2. Replies: 38
    Last Post: 02-19-2015, 10:45 PM
  3. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  4. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM