Help with EOF with Arrays

This is a discussion on Help with EOF with Arrays within the C Programming forums, part of the General Programming Boards category; I'm working on an assignment that I can't figure out. The instructions were to create a program that reads in ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    18

    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);
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    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);
    }
    }

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. Initialize i with 0 and increment at the end of while loop...
    Simple :-)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    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);
    }
    }

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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).

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  2. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 03:25 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 06:39 PM

Tags for this Thread


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