Code:
#include <stdio.h>
#include <math.h>
#define SENTINEL -1
#define SIZE 100 //my edit: removed equal sign
int fillArray (double*,int);
double x[SIZE];
int main(void)
{
int n;
int maxsize = 20; //sample size
n = fillArray (x, maxsize);
printf("%d\n",n); // my edit: print n
for (int i = 0; i < SIZE; i++)
{
printf("%g\n",*(x+i)); // my edit: print elements of x
}
getchar();
getchar();
return 0;
}
// Function to read a collection of type float data from
// the keyboard and store this data in the array x.
// Returns number of elements read if function completes as // intended.
// Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file // is empty. Returns 0 if too many items in file (array // is filled while data remains to be processed)
int fillArray
(double *x, // INOUT: array to be filled
int arraysize) // IN: size of array
{
double item; // Temp cell for each item read
int count = 0; // Count of number of items read
if (arraysize <= 0) return (-2);
// Execute the "priming" or initial read
printf("Enter the first data item: ");
scanf ("%lg", &item); //my edit: changed %f to %lg
// Repeat until no more data.
while (item !=SENTINEL){
x[count] = item;
count++;
if (count >= arraysize) return (0); // array full
// Execute the "update" or next read
printf("Enter the next data item: ");
scanf ("%lg", &item); //my edit: changed %f to %lg
}
if (count == 0) return (-1); // file empty
else return (count);
} // end fillArray
the code above compiles. fillarray starts filling x at x[1] though.
edited: