Returning

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 124
Reputation: Jboy05 is an unknown quantity at this point 
Solved Threads: 1
Jboy05 Jboy05 is offline Offline
Junior Poster

Returning

 
0
  #1
Feb 28th, 2008
I have to write a C++ function named MPG which returns the number of miles a car can travel with one gallon of gasoline. The function should have three parameters: the size of gas tank in gallons, number of miles traveled, and gasoline left in tank in gallons.

this is what I got

  1. void MPG ( char sizeoftank, char number of milest, char gasleftintank)
  2.  
  3. {
  4.  
  5. return sizeoftank * numberofmilest
  6.  
  7. }

is it right? if not can show one give me a soultion
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: danzona is an unknown quantity at this point 
Solved Threads: 3
danzona danzona is offline Offline
Newbie Poster

Re: Returning

 
0
  #2
Feb 28th, 2008
We have a policy of not doing people's homework for them.

However, here are a few pointers to get you on the right track:

1. Understand types. What is a void? What is a char? What is a double? Once you understand types, which one seems most appropriate for mathematical calculations?

2. You function is called MPG. What does it mean to have "void" before the function name? Is it correct?

3. Your algebra is incorrect. Generally speaking, if you are given three pieces of information to solve a problem and you only use two of them, your solution might be incorrect.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 124
Reputation: Jboy05 is an unknown quantity at this point 
Solved Threads: 1
Jboy05 Jboy05 is offline Offline
Junior Poster

Re: Returning

 
0
  #3
Feb 28th, 2008
would you recommend using double instead? Are you saying void is not need?
Last edited by Jboy05; Feb 28th, 2008 at 5:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 981
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Returning

 
0
  #4
Feb 28th, 2008
>> function named MPG which returns the number of miles a car can travel with one gallon of gasoline
MPG() simply cannot be void, because it has to return the requested value.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 124
Reputation: Jboy05 is an unknown quantity at this point 
Solved Threads: 1
Jboy05 Jboy05 is offline Offline
Junior Poster

Re: Returning

 
0
  #5
Feb 28th, 2008
how about this then

  1. double MPG ( double sizeoftank, double number of milest, double gasleftintank)
  2.  
  3. {
  4.  
  5. numberofmilest = numberofmilest/sizeoftank;
  6. return numberofmilest;
  7.  
  8. }
Last edited by Jboy05; Feb 28th, 2008 at 7:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Returning

 
0
  #6
Feb 28th, 2008
Originally Posted by Jboy05 View Post
how about this then
  1. double MPG ( double sizeoftank, double number of milest, double gasleftintank)
  2. {
  3. numberofmilest = numberofmilest/sizeoftank;
  4. return numberofmilest;
  5. }
Think of it this way. You have two known pieces of information (gallons used and miles travelled), that is all you know. The third piece is what is calculated and is the purpose of the function - to calculate 'mile per gallon' (return). All the function needs to know are the two known pieces of information (arguments).

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 124
Reputation: Jboy05 is an unknown quantity at this point 
Solved Threads: 1
Jboy05 Jboy05 is offline Offline
Junior Poster

Re: Returning

 
0
  #7
Feb 28th, 2008
confused?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Returning

 
0
  #8
Feb 28th, 2008
Originally Posted by Jboy05 View Post
how about this then
  1. double MPG ( double sizeoftank, double number of milest, double gasleftintank)
  2. {
  3. numberofmilest = numberofmilest/sizeoftank;
  4. return numberofmilest;
  5. }
Originally Posted by superjacent View Post
Think of it this way. You have two known pieces of information (gallons used and miles travelled), that is all you know. The third piece is what is calculated and is the purpose of the function - to calculate 'mile per gallon' (return). All the function needs to know are the two known pieces of information (arguments).

Hope this helps.
Originally Posted by Jboy05 View Post
confused?
Do you notice in your function that you are passing in three arguments. To work out miles per gallon, you only need two arguments (miles and gallons). Do the calculation in the function (using those arguments) and the return value of the function is the result of that calculation.

By the way, there's nothing wrong with reverting to a calculator to check/confirm results, even for simple problems. I do it all the time.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 124
Reputation: Jboy05 is an unknown quantity at this point 
Solved Threads: 1
Jboy05 Jboy05 is offline Offline
Junior Poster

Re: Returning

 
0
  #9
Feb 28th, 2008
ok how does this look then

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Returning

 
0
  #10
Feb 28th, 2008
Originally Posted by Jboy05 View Post
ok how does this look then

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}
It's the same function, you haven't made any changes.

By the way, in the context of your function the variable 'numberofmilest' first needs to be initialised as a double.

Have you tested this function in a program?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1987 | Replies: 23
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC