Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello all, I don't know if this is the right forum (as it really doesn't have anything to do with linux software, per se), but I thought I'd start here.
I am writing a C prog for a class I take. I am trying to write a program that calculates miles per gallon for three tanks. Then I need to give an average for all 3 tanks. I have written it, but the last part, the part about averaging, is not working. Can anyone see why and let me know how to fix it? It's important. Thanks!
Here's the code:
Code:
#include <stdio.h>
void main(void)
{
/*Variable Declaration*/
int counter;
float num_miles = 0;
float num_gallons = 0;
float miles_per_gallon;
float total_gas;
float total_miles;
float average = 0;
/*Display welcome message and explain programs purpose and goal to user*/
printf ("Welcome to the Mileage Calculator.\n\n");
printf ("This program will find the average miles per gallon for 3 tanks of gas.\n");
printf ("It will also tell you the miles per gallon of each tank.\n\n");
printf ("Whenever you are ready, you may begin to enter your information.\n\n");
/*Begin Loop Statement & Calculator*/
/*-------------------------------*/
for ( counter = 1; counter <= 3; ++counter)
{
/* Prompt User to enter gas usage and milage information*/
/*-----------------------------------------------------*/
printf ("Please enter the number of miles driven: ", num_miles);
scanf ("%f", &num_miles);
fflush (stdin);
printf ("Please enter amount of gas used: ", num_gallons);
scanf ("%f", &num_gallons);
fflush (stdin);
/*Begin Calculation of miles per gallon*/
/*------------------------------------*/
miles_per_gallon = ' '; //introduce miles per gallon for calculation
for (miles_per_gallon = ' '; miles_per_gallon == ' '; miles_per_gallon + 0)
{
miles_per_gallon = num_miles / num_gallons; //Computation of miles per gallon for each tank
total_miles = ++num_miles; //Addition of milage entries
total_gas = ++num_gallons; //Addition of gas usage entries
/*Display milage per gallon for each tank*/
/*---------------------------------------*/
printf ("Your miles per gallon for this tank: %.2f\n\n\n", miles_per_gallon);
}//end inner loop
}//end of loop
/*Display average for all tanks*/
/*----------------------------*/
average = total_miles / total_gas; //Computation of average for all tanks
printf ("Your average miles per gallon: %.2f\n\n", average);
/*Display Program End-Goodbye Message*/
/*----------------------------------*/
printf ("Thanks for using the program.:-)\n\n");
printf ("Drive carefully!\n");
}//end
The first way is not *adding* anything to your "total" values. It's assigning the latest "num_" values to them. What's worse, you're performing a ++ on your "num_" values *before* you even do the assignment. This is going to even mess up your single-tank calculations.
I think "+=" is what you want. It leaves the "num_" values untouched and it adds those values to those that are already in your "total_" values.
Also, make sure you're initializing your totals to zero before you start. You might already be doing this, I didn't check.
Um... yeah, it looks like your totals are the only ones that you are *not* initializing to zero. That's not good.
Although just about every compiler you'll encounter will probably default uninitialized values to zero *for* you, my understanding is that this is not part of the C specification.
This means that, in accordance with Murphy's Law, someday you'll unknowingly come across a compiler that does NOT default your variables to zero and, of course, it will be the compiler for a program that's going to guide a rocket to the moon or manage air traffic or something that is going to cause lots of people to die and lots more to hate you when they find out that you didn't explicitly initialize your variables.
For starters, you don't need to assign a value to a variable when you assign the same value to it in your for() statement. So, you can remove the first line.
Next, you're assigning a char (' ') to a float. Don't do that.
Third, your iterative section (miles_per_gallon + 0) doesn't do anything. It doesn't assign anything to any variable and, basically, it doesn't change anything. So you can take that out.
Lastly, it looks like there's nothing that can get this loop to execute more or less than exactly *once*... nor does there seem to be any *reason* to. So, you can get rid of the whole looping part altogether and just unindent the inner code by one tab stop.
/*Display welcome message and explain programs purpose and goal to user*/
printf ("Welcome to the Mileage Calculator.\n\n");
printf ("This program will find the average miles per gallon for 3 tanks of gas.\n");
printf ("It will also tell you the miles per gallon of each tank.\n\n");
printf ("Whenever you are ready, you may begin to enter your information.\n\n");
for ( counter = 1; counter <= 3; ++counter)
{
/* Prompt User to enter gas usage and milage information*/
/*-----------------------------------------------------*/
printf ("Please enter the number of miles driven: ", num_miles);
scanf ("%f", &num_miles);
fflush (stdin);
printf ("Please enter amount of gas used: ", num_gallons);
scanf ("%f", &num_gallons);
fflush (stdin);
/*Begin Calculation of miles per gallon*/
/*------------------------------------*/
total_miles += num_miles; //Addition of milage entries
total_gas += num_gallons; //Addition of gas usage entries
/*Display milage per gallon for each tank*/
/*---------------------------------------*/
miles_per_gallon = num_miles / num_gallons; //Computation of miles per gallon for each tank
printf ("Your miles per gallon for this tank: %.2f\n\n\n", miles_per_gallon);
}//end of loop
/*Display average for all tanks*/
/*----------------------------*/
average = total_miles / total_gas; //Computation of average for all tanks
printf ("Your average miles per gallon: %.2f\n\n", average);
/*Display Program End-Goodbye Message*/
/*----------------------------------*/
printf ("Thanks for using the program.:-)\n\n");
printf ("Drive carefully!\n");
}//end
Here's what it does when I run it:
--------------------------------------------------------------
Welcome to the Mileage Calculator.
This program will find the average miles per gallon for 3 tanks of gas.
It will also tell you the miles per gallon of each tank.
Whenever you are ready, you may begin to enter your information.
Please enter the number of miles driven: 100
Please enter amount of gas used: 2
Your miles per gallon for this tank: 50.00
Please enter the number of miles driven: 200
Please enter amount of gas used: 4
Your miles per gallon for this tank: 50.00
Please enter the number of miles driven: 700
Please enter amount of gas used: 4
Your miles per gallon for this tank: 175.00
Your average miles per gallon: 100.00
Thanks for using the program.:-)
Drive carefully!
--------------------------------------------------------------
Welcome to the Mileage Calculator.
This program will find the average miles per gallon for 3 tanks of gas.
It will also tell you the miles per gallon of each tank.
Whenever you are ready, you may begin to enter your information.
Please enter the number of miles driven: 12
Please enter amount of gas used: 2
Your miles per gallon for this tank: 6.00
Please enter the number of miles driven: 6
Please enter amount of gas used: 2
Your miles per gallon for this tank: 3.00
Please enter the number of miles driven: 2
Please enter amount of gas used: 1
Your miles per gallon for this tank: 2.00
One last thing. It makes for a nicer program (and it would have helped you debug the program easier) if the program displayed what it thought the total miles and total gallons were before it displayed the average.
I see what you did. That's similar to what I did last night when I finally got it working. I really appreciate it, man! You pushed me in the right direction!
You probably won't read this as you have your assignment done and really are not interested in doing things properly, just quickly. So I am just writing this as a tome to my developing style.
Start simple. First hard code a small program with all of the variables explicitly declared with values. E.g., float miles = 100.0 ...
Get that toy program working, then add more complexity. Get each step working before you add the next layer of complexity. In the last step make your program interactive.
Don't forget to build in debugging support. I like to create a log class that I can use everywhere. They you can simply put in logging statements like:
Debug.Print("The user entered the following miles");
Debug.Print(miles);
Of course you have to overload Print to take a string, int, ...
Think of it as step-wise refinement. You can easily figure out what when wrong when you make small changes. Lord have mercy on your soul if you write more than a page of code before testing it out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.