![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
this is my new code. to give u an idea of whether ive been trying or not, the old code was ten times this long
#include <stdio.h>
int main ()
{
float miles;
float gallons;
float counter;
float lowest;
float highest;
float mpg;
lowest = 100;
highest = 0;
counter = 0;
scanf("%f", &miles);
while ( miles > -.5 )
{
scanf("%f", &gallons);
(miles/gallons) = mpg;
if (mpg < lowest)
{
lowest = mpg;
}
(miles/gallons) = mpg;
if (mpg > highest)
{
highest = mpg;
}
counter = counter + 1;
scanf("%f", &miles);
/* the above calculates number of trips */
}
return (0);
} |
|
|
|
|
|
#12 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
sweet. i figured out what was wrong. miles/gallons = pg doesnt work, it assigns a value to something that IS already a value. it has to be mpg = miles/gallons
|
|
|
|
|
|
#13 | |
|
Professional Programmer
|
Quote:
You might want to use indentation. You can also use counter++; instead of counter = counter + 1;. I am curious to know why your program only accepts values under -.5?
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
|
#14 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
no, it only accepts values OVER -.5
at the end of the data file used as my input, there is "-1", which serves to stop the while loops from running. . since it is a MPG analysis program, obviously all normal values will be positive. so the while loop will run until the end of the program, at which point the value "-1" will cause it to stop im estatic right now, it finally works. although I still havent figured out how to get the average of all the MPGs... but I'll figure it out. ![]() |
|
|
|
|
|
#15 | |||||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The code:
#include <stdio.h>
int ohGoodGosh (char *troubleInRiverCity)
{
fprintf (stderr, "%s\n", troubleInRiverCity);
return -1;
}
int main ()
{
FILE *trips;
char tripFile [] = "trips.txt";
double totMiles = 0.0;
double totGallons = 0.0;
double thisTrip;
double thisConsumption;
int numTrips = 0;
int status;
trips = fopen (tripFile, "r");
if (!trips) return ohGoodGosh ("Couldn't open file");
puts ("Trips:\n");
while (1)
{
status = fscanf (trips, "%lf %lf\n", &thisTrip, &thisConsumption);
if (status != 2)
{
/*
Failed to get complete record.
*/
if (feof (trips)) break; /* But that's okay, end of file */
/*
Two parts not found, or read error
*/
return ohGoodGosh ("Error reading file");
}
/*
Display this record
*/
printf ("%.2lf miles, %.2lf gallons\n", thisTrip, thisConsumption);
/*
Accumulate
*/
totMiles += thisTrip;
totGallons += thisConsumption;
/*
--------> Counter <--------
*/
numTrips++;
}
/*
Wrapup
*/
puts ("\nThe final skinny:\n");
printf ("%.2lf miles total, %.2lf gallons total for %.2lf average mpg\n\n", totMiles, totGallons, totMiles/totGallons);
return 0;
}Quote:
Quote:
Quote:
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|||||
|
|
|
|
|
#16 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
umm... what does that mean? ^^ (i dont know what you are trying to tell me.)
anyway, i have a question, and it is: why doesn't the "if" loop (condition is (20 <= trips <= 30)) correctly calculate the number of trips that were between 20-30 MPG? it compiles correctly, just doesnt get the correct answer. edit: nevermind, another stupid error. the condition was wrong... it has to be mpg in the condition, not trips. also, i need && ... what I had in the condition is mathematically true, but not for C . #include <stdio.h>
int main ()
{
float miles;
float gallons;
float counter;
float lowest;
float highest;
float mpg;
float trips;
trips = 0;
lowest = 100;
highest = 0;
counter = 0;
scanf("%f", &miles);
while ( miles > -.5 )
{
scanf("%f", &gallons);
mpg = (miles/gallons);
if ( mpg < lowest )
{
lowest = mpg;
}
mpg = (miles/gallons);
if (mpg > highest)
{
highest = mpg;
}
if ( 20 <= trips <= 30 )
{
trips = trips + 1 ;
}
counter = counter + 1;
scanf("%f", &miles);
/* the above calculates number of trips */
}
printf("Package truck MPG analysis :\n");
printf("%.3f trips were taken \n", counter);
printf("The best was %.3f MPG \n", highest);
printf("The worst was %.3f MPG \n", lowest);
printf("The number of trips between 20-30 MPG was %f \n", trips);
return (0);
} |
|
|
|
|
|
#17 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
If anyone is interested, here is the final code. thanks for all the help, everything was appreciated! Btw andro, the reason I couldn't do what you were telling me to do was because I didn't understand what you were saying. It makes sense to me now that I figured out the answer... but it was way over my head beforehand.
#include <stdio.h>
int main ()
/* File: proj5.c
**Author: Kyle Moser
**Date: Tuesday, October 31
**My web ID: 5727
**Section:
**email: kmo1@umbc.edu
**description: This program calculates the number of trips delivery trucks
took, as well as the highest MPG of any of those trips, the lowest MPG of
any of those trips, the average MPG, and the number of trips between 20-30
MPG. ***/
{
float miles;
float gallons;
float counter;
float lowest;
float highest;
float mpg;
float trips;
float average;
average = 0;
trips = 0;
lowest = 100;
highest = 0;
counter = 0;
scanf("%f", &miles);
/* Get number of trips */
while ( miles > -.5 )
{
scanf("%f", &gallons);
mpg = (miles/gallons);
/* get lowest MPG */
if ( mpg < lowest )
{
lowest = mpg;
}
mpg = (miles/gallons);
/* get highest MPG */
if (mpg > highest)
{
highest = mpg;
}
/* get number of trips between 20 and 30 MPG */
if (mpg >= 20 && mpg <= 30 )
{
trips = trips + 1 ;
}
counter = counter + 1;
/* get total MPG */
if (mpg > 0)
{
average = (average + mpg) ;
}
scanf("%f", &miles);
}
/* Get average MPG */
average = ( average / counter );
printf("Package truck MPG analysis :\n");
printf("%.3f trips were taken \n", counter);
printf("The best was %.3f MPG \n", highest);
printf("The worst was %.3f MPG \n", lowest);
printf("The average was %.3f MPG \n", average);
printf("The number of trips between 20-30 MPG was %f \n", trips);
return (0);
} |
|
|
|
|
|
#18 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Incidentally, the tab key is right under your left pinky if you move said pinky a little up and to the left.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|