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

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

Recommended Answers

All 23 Replies

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.

would you recommend using double instead? Are you saying void is not need?

>> 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.

how about this then

double MPG ( double sizeoftank, double number of milest, double gasleftintank)

{

    numberofmilest = numberofmilest/sizeoftank;
    return numberofmilest;

}

how about this then

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.

confused? :(

how about this then

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.

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.

ok how does this look then

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}

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?

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
double numberofmilest;

numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}

Done
I have not run the function in a program, its the freaking math of this thats pissing me off, this shit is frustrating

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
double numberofmilest;

numberofmilest=sizeoftank/gasleftintank;
return numberofmilest;
}

Done
I have not run the function in a program, its the freaking math of this thats pissing me off, this shit is frustrating

miles / gallons = mpg

double MPG ( double sizeoftank, double number of milest, double gasleftintank)
{
double numberofmilest;

sizeoftank=numberofmilest/gasleftintank;
return sizeoftank;

}

i switched the bolded statement around if miles/gallon will yeild mpg then I guess this is right...let me rephrase that I hope this is right....

Ok, I can see where you are confused and I apologise accordingly. I re-read your initial first post and I skipped the bits about the information to be input being 1. size of tank, 2. gallons left and 3. miles travelled. I assumed (lesson never assume) that gallons used was inputted directly. In that regard, yes, there are three (not two as I previously mentioned) arguments to be passed into the function. In the function simply deduct gallons left from size of tank and that equals gallons used. From there it's a straight out mpg calculation.

how would I go about using three agruments to yield the number of miles a car can travel with one gallon of gasoline

how would I go about using three agruments to yield the number of miles a car can travel with one gallon of gasoline

Ok, here's the formula:

miles travelled / (size of tank - gallons left) = mpg.

mpg is miles per gallon.

miles : 1000
size (gallons) : 60
left (gallons) : 10
1000 / (60-10) = 20 mpg.

double MPG ( double sizeoftank, double numberofmilest, double gasleftintank)
{


mpg=numberofmilest/(sizeoftank-gasleftintank);
return mpg;
}
double MPG ( double sizeoftank, double numberofmilest, double gasleftintank)
{


mpg=numberofmilest/(sizeoftank-gasleftintank);
return mpg;
}

Try it in a program........see what happens....

i got errors

i got errors

What were the errors.

#include <iostream>
#include <cstdlib>
using namespace std;

// Put you function prototype (declarations) here.

double MPG ( double sizeoftank, double numberofmilest, double gasleftintank)
{


MPG=numberofmilest/(sizeoftank-gasleftintank);
return MPG;
}

system("PAUSE");

}

You need to at least write a main() program, that is the only way you will be able to test your mpg function.

#include <iostream>
#include <cstdlib>
using namespace std;

// Put you function prototype (declarations) here.


int main ()
{   
    
   
}
double MPG ( double sizeoftank, double numberofmilest, double gasleftintank)
{


double MPG=numberofmilest/(sizeoftank-gasleftintank);
return MPG;
}

system("PAUSE");

You still need to call your function from main(). In main, simply initialise and assign three double variables and pass them into your function.

In main() something like this cout << mpg(d1, d2, d3) << " mpg.\n"; Note, d1, d2 & d3 are the three variables previously initialised and assigned.

We're not bothered about user input at this stage, the whole point is merely to test that your functions works. I can tell you that your function is correct but you need to test it for yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.