This is what I have thus far.

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

double miles( double miles, double gallons, double mpg);

int main ()
{
const int ARRAYSIZE = 10;      // fixed array with 10 items
double miles[ARRAYSIZE]={240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, 192.3};
double gallons[ARRAYSIZE]={10.3, 15.6, 8.7, 14.0, 16.3, 15.7, 14.9, 10.7, 8.3, 8.4};

system("pause");
return 0;
}

double miles( double miles, double gallons, double mpg)
{
double mpg;
mpg = miles/gallons;
return mpg;
}``````

I need each element of the mpg array to calculate as the corresponding element of the miles array divided by the equivalent element of the gallons array. For example:

Mpg[0]=miles[0]/gallons[0]

I've been like working on this now for about 4 days, I am very lost and could really use the help.

2
Contributors
1
2
Views
12 Years
Discussion Span
Last Post by Narue

First and foremost, you need to define an Mpg array. You're also reusing names too much, and that won't work. Your miles function does the operation, though it doesn't need either an mpg parameter or a local mpg. You can define it like this:

``````double get_mpg ( double miles, double gallons )
{
return miles / gallons;
}``````

Beyond that, it's a matter of looping over the arrays and assigning the result of miles to Mpg:

``````for ( int i = 0; i < ARRAYSIZE; i++ )
Mpg[i] = get_mpg ( miles[i], gallons[i] );``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.