Thread: Having trouble printing from an array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    Having trouble printing from an array

    I'm not sure what I'm doing wrong, but there seems to be some problems occuring before the print function. The only times it will print the values is if the array is full yet it still has some problems that I do not know how to fix.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSIZE 10
    
    int main()
    {
      int tablefill(int a[], int max);
      void print_table(int a[], int n);
      
      int table[MAXSIZE];              // table of values
      int n;                           // number of values in table
      
      n = tablefill(table, MAXSIZE);
      print_table(table, n);
      
      system("PAUSE");	
      return 0;
    }
    
    int tablefill(int a[], int max)
    {
      int cnt;               // counter variable
      int r;                 // read values
      int next;              // next array value
      
      cnt = 0;
      while ((r = scanf("%i", &next)) != EOF && cnt < max)
      {
         if(r == 0)           // invalid data
         {
            printf("Nonnumeric data has been entered.\n", cnt);
            while (getchar() != '\n')    // flush invalid characters
               ;
         }
         else
            a[cnt++] = next;
      }
         
      if (r == 1)            // full array
         printf("The array is full after reading %i values.\n", cnt);
    }
    
    void print_table(int a[], int n)
    {
       int i;
       
       for (i = 0; i < n; i++)
          printf("%i\n", a[i]);
    }
    The following happens when the array is full:
    Code:
    1 2 5 2 7 2 7 2 7 3 6
    The array is full after reading 10 values.
    1
    2
    5
    2
    7
    2
    7
    2
    7
    3
    2088810051
    2293672
    81
    2
    2293680
    4198887
    1
    4007080
    4008560
    4210688
    2293668
    -1
    2293672
    1
    6
    4008560
    0
    2147348480
    2293696
    4198968
    1
    9
    2293744
    2088857559
    34405412
    2088810051
    2147348480
    -2141935304
    2293704
    -1984787984
    -1
    2088999592
    2088857568
    Press any key to continue . . .

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You seem to be missing
    Code:
       return cnt;
    from
    Code:
    int tablefill(int a[], int max)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Wow, I feel kind of stupid now for missing that. Thanks a lot though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. array program trouble
    By chrisjenkins212 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2006, 01:16 AM
  4. trouble printing
    By dPmunky in forum C Programming
    Replies: 6
    Last Post: 11-18-2002, 02:43 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM