| | |
Returning
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 124
Reputation:
Solved Threads: 1
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
is it right? if not can show one give me a soultion
this is what I got
C++ Syntax (Toggle Plain Text)
void MPG ( char sizeoftank, char number of milest, char gasleftintank) { return sizeoftank * numberofmilest }
is it right? if not can show one give me a soultion
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 3
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.
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.
•
•
Join Date: Jan 2008
Posts: 124
Reputation:
Solved Threads: 1
how about this then
C++ Syntax (Toggle Plain Text)
double MPG ( double sizeoftank, double number of milest, double gasleftintank) { numberofmilest = numberofmilest/sizeoftank; return numberofmilest; }
Last edited by Jboy05; Feb 28th, 2008 at 7:51 pm.
•
•
•
•
how about this then
C++ Syntax (Toggle Plain Text)
double MPG ( double sizeoftank, double number of milest, double gasleftintank) { numberofmilest = numberofmilest/sizeoftank; return numberofmilest; }
Hope this helps.
•
•
•
•
how about this then
C++ Syntax (Toggle Plain Text)
double MPG ( double sizeoftank, double number of milest, double gasleftintank) { numberofmilest = numberofmilest/sizeoftank; return numberofmilest; }
•
•
•
•
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.
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.
•
•
•
•
ok how does this look then
double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}
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?
![]() |
Similar Threads
- Searches only returning results for certain strings (PHP)
- Returning Empty Datatable (ASP.NET)
- Table query returning error -- help! (MS SQL)
- Regarding Function Returning a Value. (C)
- Exact meaning of functions returning reference (C++)
- returning by reference (C)
- returning an array? (C++)
- Returning a correct python list (Python)
Other Threads in the C++ Forum
- Previous Thread: Why wouldn't this work?
- Next Thread: Combinations of Inheritance and Composition
Views: 1987 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





