Program to find average of numbers stored in sequential access file
Answers
Answer:
Answer: Average of numbers stored in sequential access files is the total sum that comes in the ended which is divided by the group size. Explanation: The first number refers to the size of the group. Taking that number, the loop is made and through that number other numbers are accessible.
find average of numbers stored in sequential access file
#include <stdio.h>
#include <stdlib.h>
#define DATAFILE "prog15.dat"
int main() {
FILE* fp;
int n[50], i = 0;
float sum = 0;
if ((fp = fopen(DATAFILE, "r")) == NULL) {
printf("Unable to open %s...\n", DATAFILE);
exit(0);
}
puts("Reading numbers from num.dat");
while (!feof(fp)) {
fscanf(fp, "%d ", &n[i]);
printf("%d %d\n", i, n[i]);
sum += n[i];
i++;
}
fclose(fp);
// if no data is available in the file
if (i == 0)
printf("No data available in %s", DATAFILE);
float average = sum / i;
printf("The average is %.3f for %d numbers\n", average, i);
return 0;
}
#SPJ2