LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 10-22-2003, 08:02 PM   #1
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Rep: Reputation: 30
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
 
Old 10-22-2003, 08:42 PM   #2
jemenake
LQ Newbie
 
Registered: Oct 2003
Posts: 15

Rep: Reputation: 0
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
 
Old 10-22-2003, 08:48 PM   #3
jemenake
LQ Newbie
 
Registered: Oct 2003
Posts: 15

Rep: Reputation: 0
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
 
Old 10-22-2003, 09:13 PM   #4
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
Lemme try that, fellas. I will let you know! Thanks
 
Old 10-22-2003, 09:16 PM   #5
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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!
 
Old 10-22-2003, 09:33 PM   #6
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
Nevermind!

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

Chris
 
Old 10-22-2003, 09:36 PM   #7
jemenake
LQ Newbie
 
Registered: Oct 2003
Posts: 15

Rep: Reputation: 0
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
 
Old 10-22-2003, 09:46 PM   #8
jemenake
LQ Newbie
 
Registered: Oct 2003
Posts: 15

Rep: Reputation: 0
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!
 
Old 10-22-2003, 09:50 PM   #9
jemenake
LQ Newbie
 
Registered: Oct 2003
Posts: 15

Rep: Reputation: 0
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.
 
Old 10-23-2003, 05:22 AM   #10
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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
 
Old 10-23-2003, 09:11 AM   #11
SnoopDougEDoug
LQ Newbie
 
Registered: Oct 2003
Location: Seattle
Distribution: RH
Posts: 5

Rep: Reputation: 0
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
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a good Tribooting Tutorial I wrote cheetahman Linux - Newbie 7 09-25-2005 11:51 AM
What is the last program you wrote (or are currently writing)? lowpro2k3 Programming 2 08-15-2005 01:14 AM
font looks like its been wrote with a pen insurin Linux - Newbie 4 07-05-2005 09:50 AM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
Wrote a program: TEXTDRAW lea Programming 1 08-13-2002 09:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration