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

1. ## 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.

2. 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. Originally Posted by algorism
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. 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. Originally Posted by algorism
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. Originally Posted by ZeroesAndOnes
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. Originally Posted by algorism
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.