I've looked through the site to find a way to get the number of elements of an array with data in it. All I can find is people using sizeof, but that isn't an option for me due to the teachers limitations on what we can access. I need a way of doing it in a loop, preferably the same loop where I'm reading data from.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int readData();
int
main(void)
{
double data[SIZE];
int size;
readData(double data[], &size);
return 0;
}
int readData(double data[], int* size)
{
FILE* fp;
fp = fopen("data.txt", "r");
int i;
for (i = 0; i < SIZE; i++)
{
if( fscanf(fp, "%lf", &data[i]) < 1)
break;
if(data[i] < 0)
printf("Negative data at %d\n", i);
}
size = i;
printf("The array is: ");
for (i = 0; i < size; i++)
{
printf("%f ", data[i]);
}
fclose(fp);
printf("\n\n");
return;
}