Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 31st, 2006, 9:09 PM   #11
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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);

}
Fall Back Son is offline   Reply With Quote
Old Oct 31st, 2006, 9:28 PM   #12
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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
Fall Back Son is offline   Reply With Quote
Old Oct 31st, 2006, 9:31 PM   #13
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Quote:
Originally Posted by Fall Back Son View Post
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
Yeah, Dev-C++ threw an "invalid lvalue" compiler error.

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
Prm753 is offline   Reply With Quote
Old Oct 31st, 2006, 9:45 PM   #14
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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.

Fall Back Son is offline   Reply With Quote
Old Oct 31st, 2006, 9:53 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;

}
The good file:
Quote:
100 10.75
250 40
300 35.4
275 25
108 13
The bad file:
Quote:
100 10.75
250 a
300 35.4
275 25
108 13
The good file output:
Quote:
Trips:

100.00 miles, 10.75 gallons
250.00 miles, 40.00 gallons
300.00 miles, 35.40 gallons
275.00 miles, 25.00 gallons
108.00 miles, 13.00 gallons

The final skinny:

1033.00 miles total, 124.15 gallons total for 8.32 average mpg
The bad file output:
Quote:
Trips:

100.00 miles, 10.75 gallons
Error reading file
The nonexistent file output:
Quote:
Couldn't open file
__________________
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
DaWei is offline   Reply With Quote
Old Oct 31st, 2006, 10:06 PM   #16
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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);

}
Fall Back Son is offline   Reply With Quote
Old Nov 1st, 2006, 1:14 PM   #17
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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);

}
Fall Back Son is offline   Reply With Quote
Old Nov 1st, 2006, 1:49 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
umm... what does that mean? ^^ (i dont know what you are trying to tell me.)
It's merely example code, including testing the read operations for success, which only noobs and schlocks fail to do. Try putting in 'x' when your program is looking for a number (some user surely will) and you'll see what I mean.

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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC