I am writing a program that scans data from a file into an array. It then prints out the contents of the file and finds the average.
It compiles and builds without error, but for some reason Microsoft Visual C++ cannot locate my file. The file is exactly where i said it is in the program, and I have switched computers to make sure, and it still will not read my file.
Is there something wrong in my code that could be keeping it from finding my file?
Code:
#include <stdio.h>
#define MAXSIZE 100
double arrayAverage(double dArray[MAXSIZE], double delements);
int main (void)
{
int i;
double delements, dArray[MAXSIZE], dAverage;
FILE *pIn;
pIn = fopen("H:\\CS 1003\\practice\\numbers.txt", "r");
if (pIn == NULL);
{
printf("Unable to open file.\n");
return(-1);
}
i=0;
fscanf(pIn, "%lf", &dArray[i]);
while (dArray[i] != EOF)
{
delements = i+1;
i++;
fscanf(pIn, "%lf", &dArray[i]);
}
printf("Original series of numbers is as follows:\n");
for (i=0; i<delements; i++)
{
printf("%f\n", dArray[i]);
}
dAverage = arrayAverage(dArray, delements);
printf("\nArray average is %f.", dAverage);
fclose(pIn);
return (0);
}
double arrayAverage(double dArray[MAXSIZE], double delements)
{
double dAverage, dSum;
int i;
dSum=0;
i=0;
while (i<delements)
{
dSum+=dArray[i];
i++;
}
dAverage = dSum/delements;
return (dAverage);
}