Need help with my homework

This is my assignment
Assignment #16 due 5/14
Gas comparison cost of various cars.
A company has five cars that give varying gas mileages of 10, 15, 20, 25 and 30 MPG. Assume that the cars were driven 10000, 15000, 20000, 25000 and 30000 miles per year respectively, and the cost of one gallon of gas is $2.75. Write a program to display a two-way table of the annual cost rounded to 2 decimal places. Display the MPG across as columns and the miles as rows and the appropriate headings as follows:
M P G
Miles 10 15 20 25 30
10000: $ 2750.00 $ 1833.33 $ 1375.00 $ 1100.00 $ 916.67
15000: $ 4125.00 $ 2750.00 $ 2062.50 $ 1650.00 $ 1375.00
20000: $ 5500.00 $ 3666.67 $ 2750.00 $ 2200.00 $ 1833.33
25000: $ 6875.00 $ 4583.33 $ 3437.50 $ 2750.00 $ 2291.67
30000: $ 8250.00 $ 5500.00 $ 4125.00 $ 3300.00 $ 2750.00
Press any key to continue . . .




This is what I did

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
const double gas=2.75;
double price;

for(int miles=10000; miles<=30000; miles+=5000)
{
for(int mpg=10; mpg<=30; mpg+=5)
price = miles*gas/mpg;
cout<< fixed << setprecision(2);
cout<< miles <<setw(10) << price << endl;
}


system("pause");
}


On my program I only made a nested for loop to test the calculations are correct.
But when I run the program, instead of listing the prices of 10MPG column, it listed the prices for the 30MPG.
How can I fix my program to show the table that my professor wanted?
You're missing the curly braces for your second for loop:
1
2
3
4
5
6
7
8
9
    for(int miles=10000; miles<=30000; miles+=5000)
    {
        for(int mpg=10; mpg<=30; mpg+=5)
        { //need this
            price = miles*gas/mpg;
            cout<< fixed << setprecision(2);
            cout<< miles <<setw(10) << price << endl;
        } //need this
    }
I added the brackets(curly braces) as you said but instead it displays the, outer row 5x and column

For example
10000
10000
10000
10000
10000
10000
15000
.
.
.
.

and the price in the next column with values that are all over the place.. :(
nested loops are kinda tough.
You want the endl to be printed in the outer loop, after each completion of the inner loop, not inside the inner loop.
Here's a solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
const double gas=2.75;
double price;

// Draw the MPG labels
cout << setw(8) << "MPG:" << left << setw(4) << " ";
	for(int i = 10; i <= 30; i+=5) cout << setw(4) << " " << setw(8) << i;
cout << endl;

// Draw the Miles axis label
cout << "Miles" << endl;

// Display the table
for(int miles=10000; miles<=30000; miles+=5000)
{
	cout << setw(12) << left << miles  << fixed;

		for(int mpg=10; mpg<=30; mpg+=5)
		{
		price = miles*gas/mpg;
		cout<<  "$ " << setprecision(2) << setw(10) << price;
		}

cout << endl;
}

cin.get();
return 0;
}
Last edited on
Thanks for the advice and solution!
Topic archived. No new replies allowed.