# Help with EOF with Arrays

• 03-11-2011
cpsestudent
Help with EOF with Arrays
I'm working on an assignment that I can't figure out. The instructions were to create a program that reads in an input file of integers, and prints out the number of times any number from 0-9 appears. I can only use THREE variables (an array counts as one). The problem I have is that the program is supposed to detect the EOF and stop scanning there. My program isn't doing this.

A sample run would be if the input file had: 1 2 4 3 5 8 9 9 9 9 5 3 2 38 39 92 0 3
The output would be:
1 0's
1 1's
2 2's
3 3's
1 4's
2 5's
0 6's
0 7's
1 8's
4 9's

It ignores any number that is not between 0 and 9. Here is my program so far. Everything seems to work except I am getting a segmentation fault when running it... PLEASE HELP! THANK YOU!

Code:

```int main() { int vals[10]; int numCount[10]; int i; while (scanf("%d", &vals[i]) !=EOF) {   vals[i] = 0;   i++; } while (scanf("%d", &vals[i]) !=EOF) {       i++;       if (vals[i] == 0) {         numCount[0] = numCount[0] + 1;       }       if (vals[i] == 1) {         numCount[1] = numCount[1] + 1;       }       if (vals[i] == 2) {         numCount[2] = numCount[2] + 1;       }       if (vals[i] == 3) {         numCount[3] = numCount[3] + 1;       }       if (vals[i] == 4) {         numCount[4] = numCount[4] + 1;       }       if (vals[i] == 5) {         numCount[5] = numCount[5] + 1;       }       if (vals[i] == 6) {         numCount[6] = numCount[6] + 1;       }       if (vals[i] == 7) {         numCount[7] = numCount[7] + 1;       }       if (vals[i] == 8) {         numCount[8] = numCount[8] + 1;       }       if (vals[i] == 9) {         numCount[9] = numCount[9] + 1;       } } for (i = 0; i < 10; i++) {         printf("%d %d's\n", numCount[i], i); } }```
• 03-11-2011
Salem
Several points.

1. Why are you reading input, when you're just clearing your array?

2. You keep incrementing i for no apparent reason, except that at some point, &vals[i] will be out of bounds of your array.

3. You don't need to store all the values. You just take the current value, check it for range 0 to 9, and increment that counter directly.
• 03-11-2011
cpsestudent
This is what I came up with after your points, but I'm still getting a segmentation fault when I run the program....
Code:

```int main() { int vals[10]; int numCount[10]; int i; while (scanf("%d", &vals[i]) !=EOF) {       if (vals[i] == 0) {         numCount[0] = numCount[0] + 1;       }       if (vals[i] == 1) {         numCount[1] = numCount[1] + 1;       }       if (vals[i] == 2) {         numCount[2] = numCount[2] + 1;       }       if (vals[i] == 3) {         numCount[3] = numCount[3] + 1;       }       if (vals[i] == 4) {         numCount[4] = numCount[4] + 1;       }       if (vals[i] == 5) {         numCount[5] = numCount[5] + 1;       }       if (vals[i] == 6) {         numCount[6] = numCount[6] + 1;       }       if (vals[i] == 7) {         numCount[7] = numCount[7] + 1;       }       if (vals[i] == 8) {         numCount[8] = numCount[8] + 1;       }       if (vals[i] == 9) {         numCount[9] = numCount[9] + 1;       } } for (i = 0; i < 10; i++) {         printf("%d %d's\n", numCount[i], i); } }```
• 03-11-2011
Mr.777
1. Initialize i with 0 and increment at the end of while loop...
Simple :-)
• 03-11-2011
quzah
Your while should probably be a for, and if you think hard enough about it, you should be able to see how to turn 10 if statements into a single one ... or none at all.

Quzah.
• 03-11-2011
cpsestudent
Thanks for all the help, but still no luck. When I tried both of those suggestions, the program compiled and ran but for the 8 and 9 values, I am getting random garbage numbers. BUT, the rest of the numbers are adding correctly. Also, how big should I make my vals array? Is 100 sufficient?

for THIS input: 1 0 9 8 9 7 9 8 9 4 0
i'm getting THIS output:
2 0's
1 1's
0 2's
0 3's
1 4's
0 5's
0 6's
1 7's
8341510 8's
8508163 9's

Code:

```int main() { int vals[100]; int numCount[10]; int i = 0; while(scanf("%d", &vals[i]) !=EOF) {       if (vals[i] == 0) {         numCount[0] = numCount[0] + 1;       }       if (vals[i] == 1) {         numCount[1] = numCount[1] + 1;       }     // ... continued until 9 ... //       if (vals[i] == 9) {         numCount[9] = numCount[9] + 1;       } i++; } for (i = 0; i < 10; i++) {   printf("%d %d's\n", numCount[i], i); } }```
• 03-11-2011
Mr.777
Depends upon the needs.......
Or you can input value from the user for array size and create a dynamic array.
You should get garbage as array has garbage in it. (Not initialized).
• 03-11-2011
quzah
If you only have ten unique values (0 through 9) then you only need an array big enough to hold ten items. Try initializing your array before you start using it:
Code:

`int array[ SIZE ] = {0};`
That will set it all to zero. (Note: this is not because it sets the whole thing to whatever you specify first, but rather, that it sets every one you don't specify to zero.)

Quzah.