develop a program that uses a while loop to read a a file an
develop a program that uses a while loop to read a a file and calculate the statistics on the data while using c programming. The file needed to get the info from is boxes.txt:
the skeleton of the code is here:
and the code output:
Solution
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
#define FILENAME \"boxes.txt\"
#define INCH_TO_FEET_CUBE 5.78704e-4
FILE *fileptr;
fileptr=fopen(FILENAME,\"r\");
float volume=0;
int boxCount=0;
int min_boxNumber;
int max_boxNumber;
float min_volume=0;
float max_volume=0;
float total_volume=0;
float avg_volume=0;
int start=0;
while(!feof(fileptr))
{
fscanf(fileptr,\"%i %i %i\",&length,&height,&width);
boxcount++;
volume=length*height*width;
if(start==0)
{
min_volume=volume;
max_volume=min_volume;
start++;
}
if(volume>max_volume)
{
max_volume=volume;
max_boxNumber=boxCount;
}
else if(volume<min_volume)
{
min_volume=volume;
min_boxNumber=boxCount;
}
total_volume+=volume;
}
avg_volume=(total_volume/boxCount);
avg_volume=avg_volume*INCH_TO_FEET_CUBE;
max_volume=max_volume*INCH_TO_FEET_CUBE;
min_volume=min_volume*INCH_TO_FEET_CUBE;
total_volume=total_volume*INCH_TO_FEET_CUBE;;
printf(\"Maximum Volume box is %f, box number %d \",max_volume,max_boxNumber);
printf(\"Minimum Volume box is %f, box number %d\",min_volume,min_boxNumber);
printf(\"Total Volume of %d boxes is %f\",boxCount,total_volume);
printf(\"Average Volume of %d boxes is %f\",boxCount,avg_volume);
fclose(fileptr);
}

