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