Code:
/* Definition libraries. */
#include <stdio.h>
#include <stdlib.h>
/* Defines constants. */
#define PI 3.14159
#define INCHES_IN_FEET 12
#define FEET_IN_MILE 5280
/* Initializes functions. */
double findfueleff(double radius, double numrevs, double gas);
void printGraph(double array[], int length);
int main() {
/* Defines variables. */
FILE *ifp;
int i, j;
int min_in_trip, radius;
double array[6], temp, temp2, numrevs, gas;
char filename[20];
/* Checks and opens input file. */
do {
printf("Enter the name of the file storing tire gas and tire rotation data.\n");
scanf("%s", &filename);
ifp = fopen(filename, "r");
if(ifp == NULL)
printf("File not found.\n");
}
while(ifp == NULL);
printf("\n");
/* Scans in length of trip and tire radius and determines array length. */
fscanf(ifp, "%d%d", &min_in_trip, &radius);
/* Scans in values from file. */
for(i=0; i<min_in_trip/5; i++) {
for(j=0; j<300; j++) {
fscanf(ifp, "%lf%lf", &temp, &temp2);
numrevs += temp;
gas += temp2;
}
if(j == 300)
array[i] = findfueleff(radius, numrevs, gas);
}
/* Prints out bar graph to display data. */
printGraph(array, min_in_trip/5);
/* Closes input file. */
close(ifp);
system("PAUSE");
return (0);
}
/* Checks values that calculate fuel efficiency and returns the respective value. */
double findfueleff(double radius, double numrevs, double gas) {
if(numrevs == 0 && gas > 0)
return 100;
else if(numrevs == 0 && gas == 0) {
return 0;
}
else
return ((2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE)*numrevs)/gas;
}
/* Creates bar graph. */
void printGraph(double array[], int length) {
int i, j;
for(i=95; i>=0; i=i-5) {
printf("%-4d", i);
for(j=0; j<length; j++) {
if(array[j]>=i)
printf("***** ");
else
printf(" ");
}
printf("\n");
}
printf(" ");
for(i=0; i<=34; i++) {
printf("-");
}
printf("\n");
printf(" ");
printf("00-05 05-10 10-15 15-20 20-25 25-30\n");
printf("\n");
}
Still not working with the above attached text file, so I decide to post the entire program instead.