the program is suppose to calculate the mpg of 10 vehicles, and then average the total mpg for all 10 vehicles. i wrote the program, but it only calculates the vehicles mpg, but i doesn't know how to make it calculate the average off all vehicles.. can someone help plz..
HERE IS THE CODE
int main()
{
int gallons;
double distance;
double gallon;
cout << "Please input how many gallons of gas is in vehicle 1:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 1 traveld.::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 2::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 2 traveld.::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 3:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 3 traveld.::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 4:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 4 traveld.::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 5:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 5 traveld.:::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 6:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 6 traveld.:::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 7:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 7 traveld.:::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 8:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 8 traveld.:::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 9:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 9 traveld.:::";
cin >> distance;
cout << "Please input how many gallons of gas is in vehicle 10:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 10 traveld.:::";
cin >> distance;
well look what you are doing...
two variables 1 ) gallons , 2 ) distance.
each vehicle you are resetting that value and then you are only outputting the mpg not setting it to a variable.
what you should do is 1 ) create an array to store the gallons , distances, and mpgs. 2 ) create a vector to store the galons , distances, and mpgs.
ex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
int main()
{
std::vector<int> gallon( 10 , 0 ) , distance( 10 , 0 );
std::vector<double> mpg( 10 , 0 );
// int gallon[ 10 ] = { 0 } , distance[ 10 ] = { 0 };
//double mpg[ 10 ] = { 0.0 };
//or this ^ ^
for( auto i = 0; i < 10; ++i )
{
std::cout << "Input gallons for car " << i + 1 << std::cout;
std::cin >> gallon[ i ];
//ect
//ect
mpg[ i ] = distance[ i ] / gallon[ i ];
}
}
You have to assign them to a different variable and as I did I used a loop auto is the same thing as get the variable type for me automatically because I was feeling lazy. In reality that is the same as an int. So you don't have to retype everything over and over like you are currently doing.
how about this??? is it correct
int main()
{
int gallons;
double distance, total;
for(int i = 0; i < 10; i++)
{
cout << "Please input how many gallons of gas is in vehicle ::: ";
cin >> gallons;
cout << "Please input the distance in miles vehicle traveled.::: ";
cin >> distance;
cout << "Your vehicle " << i+1 << " MPG is:" << (distance/gallons) << endl;
total += distance/gallons;
}
return 0;
}
you still need to put your values into a container try a vector or array like I mentioned earlier. The for loop is correct though. Also you should indent.
try something like
cin >> gallon[ i ];
cin >> distance[ i ];
//do same thing for the distance/gallons in the output and total
//this is so you can access the element at that position
for (int i = 0; i < 10; i++)
{
you can use the for loop here to cleanup and not have the reiteration of all the separate cars.
cout<<" Your vehicle " <<i <<" MPG is: <<(distance/gallons) <<endl;
}
then you need to add up the distance/gallons calculations, so you can create a variable for it and then += that variable or do a for loop to tally it up.