You must have a classmate who posted here:
Bar graph troubles
Since he never got around to stating fully what the data was, I can't say if his math was correct, or not.
In any case, I would change your FILE *ifp to the more conventional *fp. if(p) is a common syntax in C, and having a file pointer that starts with an i is just confusing. Must have been in the prof's notes, because he had the same odd name for it.
Drawing the graph is no problem. Fill the array with your calculated data, and it's the logic shown in that thread, is (maybe), good to go. I don't want to say that it's been tested, because it has not been, AFAIK.
Code:
Date from milesPG[]:
10.033465
32.441399
28.829947
===================
45|
40|
35|
30| *****
25| ***** *****
20| ***** *****
15| ***** *****
10| ***** ***** *****
5| ***** ***** *****
0| ***** ***** *****
-----------------------------------
00-05 05-10 10-15 15-20 20-25 25-30
Press any key to continue . . .
He limited his scale to 45, but there's no reason not to extend it higher, in your version.
Looking at your code:
Code:
#include <stdio.h>
int main() {
FILE *ifp;
int file, rad, mins;
double revs, gas;
printf("What file stores the car data?\n");
scanf("%d", &file);
//Open file
ifp = fopen(file, "r");
fscanf(ifp, "%d%d", &mins, &rad);
}
I would: delete "int file", that should be your char array for the filename.
add: char file[40]; double alltheBloodyGasUsedSoFar; int interval;
Code:
while("endless loop here") {
n=fscanf(fp, "%f", "%f", interval, gas);
if(n>1) {
alltheBloodyGasUsedSoFar += gas;
//do your other calculations here
}
else //end of file has been reached, n=EOF(usually -1)
break;
}
fclose(fp); just above the last getchar() of pause line.
getchar() or system("pause"); to just before the,
return 0; at the very end of main.
check if the file was opened OK or not (is fp==NULL). If so, print an error message and return 1, ending the program.
change: scanf("%d", &file); to scanf("%s", file); //(no ampersand)
ifp to fp as mentioned earlier
That should get you going nicely.
Be careful that this is your own work - simple copying with a tweak here or there would be plagerism.