LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Can anyone help me with a C prog I wrote? (http://www.linuxquestions.org/questions/linux-software-2/can-anyone-help-me-with-a-c-prog-i-wrote-107354/)

WorldBuilder 10-22-2003 09:02 PM

Can anyone help me with a C prog I wrote?
 
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

Thank you very much!

Chris

jemenake 10-22-2003 09:42 PM

Let me guess, the average always comes out to be 1, huh?

You need to replace:
total_miles = ++num_miles;
total_gas = ++num_gallons;

with:
total_miles += num_miles;
total_gas += num_gallons;

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.

- Joe

jemenake 10-22-2003 09:48 PM

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.

- Joe

WorldBuilder 10-22-2003 10:13 PM

Lemme try that, fellas. I will let you know! Thanks

WorldBuilder 10-22-2003 10:16 PM

Quote:

You need to replace:
total_miles = ++num_miles;
total_gas = ++num_gallons;

with:
total_miles += num_miles;
total_gas += num_gallons;
Nope, sorry. That didn't do it, either. Now it's giving the last mileage value. Any other thoughts? Thanks, guys!

Chris

The lines are starting to run together... I'm getting sleeeeeeepy, lol. I have been staring at this too long!

WorldBuilder 10-22-2003 10:33 PM

Nevermind!

I figured it out... I wasn't declaring the final variables... Stupid. Thanks!

Chris

jemenake 10-22-2003 10:36 PM

Okay... well, let me pop this into a compiler and see.

In the meantime, fix this:

miles_per_gallon = ' '; //introduce miles per gallon for calculation

for (miles_per_gallon = ' '; miles_per_gallon == ' '; miles_per_gallon + 0)


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.

- Joe

jemenake 10-22-2003 10:46 PM

Okay.... it works for me.

Here's the program. I changed the variable initializations and I removed a loop and I changed the ++'s to +='s.

#include <stdio.h>
void main(void)
{

/*Variable Declaration*/

int counter;
float num_miles;
float num_gallons;
float miles_per_gallon;
float total_gas = 0;
float total_miles = 0;
float average;

/*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*/
/*------------------------------------*/


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


Your average miles per gallon: 4.00

Thanks for using the program.:-)

Drive carefully!

jemenake 10-22-2003 10:50 PM

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.

WorldBuilder 10-23-2003 06:22 AM

Thanks, bro.

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!

Chris

SnoopDougEDoug 10-23-2003 10:11 AM

How 2 Program 101
 
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.

doug


All times are GMT -5. The time now is 11:39 AM.