The purpose of this program is to read a .data file and calculate the average of the numbers listed there. The program then gives a recommendation based on the average by way of nested if statements. The problem I am having is that for certain .data files the output average is way out of proportion to the actual average. This has happened when the program meets the "This value is the result of faulty data" condition
Here is the code:
insert
Code:
#include <stdio.h>
int main (void)
{
FILE *f1;
double a, b, c, d, e, f, g, h, i, j, k, l, m, avg;
/* opening the file */
f1 = fopen ("density1.data", "r");
/* read data */
fscanf (f1, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m);
/* calculate average */
avg = (a+b+c+d+e+f+g+h+i+j+k+l+m)/13;
fclose (f1);
/* First if condition for invalid data */
if (avg < 1.2 || avg > 2.2)
printf ("The data collected is faulty, there is no way that is reliable data! \n");
else
/* Second if condition for valid data */
if (1.2 < avg < 1.5)
printf (" The data collected suggests the site is usable with heavy \n compaction before building \n");
else
if ( 1.5 < avg < 2.2)
printf (" The data collected suggests the site is safe to build upon \n");
printf (" The average is %5.3lf \n", avg);
return (0);
}
Thank you