-
Quick array fix
This code doesn't seem to be looping properly because it doesn't store the correct array values (in fact, the values are all set to 0 in the end). And I can't figure out why for the life of me..
Code:
for(i=0; i<min_in_trip/5; i++) {
for(j=0; j<300; j++)
fscanf(ifp, "%f%f", &temp, &temp2);
numrevs += temp;
gas += temp2;
if(j == 300)
array[i] = findfueleff(radius, numrevs, gas);
}
I really need this to work so that I can finish working on another program for homework that requires that the program that this code was derived from to work.
Thanks!
P.S. I have attached the .txt file that this program uses.
-
We like indentation here, but this is not Python, and indentation is no substitute for getting the braces right:
Code:
for(i=0; i<min_in_trip/5; i++) {
for(j=0; j<300; j++) {
fscanf(ifp, "%f%f", &temp, &temp2);
numrevs += temp;
gas += temp2;
}
if(j == 300)
array[i] = findfueleff(radius, numrevs, gas);
}
-
(1)
Code:
> for(j=0; j<300; j++)
> fscanf(ifp, "%f%f", &temp, &temp2);
> numrevs += temp;
> gas += temp2;
Assuming you want to sum all 300 items, you're missing some braces:
Code:
for(j=0; j<300; j++)
{
fscanf(ifp, "%f%f", &temp, &temp2);
numrevs += temp;
gas += temp2;
}
(2) Make sure temp and temp2 are declared as floats versus doubles, as %f is for reading floats. Use %lf to read doubles instead.
(3) You should probably check the return of fscanf() to be sure it actually read two floats:
Code:
if (fscanf(ifp, "%f%f", &temp, &temp2) == 2)
{
/* Sum data */
}
else
{
/* Data not read */
}
-
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.
-
It would probably be a good idea to initialize numrevs and gas.
-
double array[6], temp, temp2, numrevs, gas; ..?
..unless there is something else I need to do.
-
I never said you hadn't declared them, I said you hadn't initialized them. Print the value of numrevs before the for-loop and see what you see.
-
I just understood what you meant, and I set both variables equal to 0.
But I'm not sure if it is printing the correct information still..
-
In your code, when would this ever not be true:So what is the point in it?