225k views
4 votes
Driving costs (with pointers)

Complete the program below using pointers.

Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75 miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can be achieved by executing
cout << fixed << setprecision(2); once before all other cout statements. This line is included for you.

Ex: If the input is:

20.0 3.1599
the output is:

3.16 11.85 79.00
Note: Real per-mile cost would also include maintenance and depreciation.

#include
#include
using namespace std;

int main() {

/* Update the declarations below so that each variable points to a
* double on the heap.
*/

double *milesPerGallon;
double *dollarsPerGallon;
double *dollars20Miles;
double *dollars75Miles;
double *dollars500Miles;

/* Write the statements here to:
* 1) Read a value from the keyboard into the variable pointed to by
* milesPerGallon.
* 2) Read a value from the keyboard into the variable pointed to by
* dollarsPerGallon.
*/

/* Write the appropriate statements here to:
* 1) Assign the proper calculated value to the variable pointed to by
* dollars20Miles.
* 2) Assign the proper calculated value to the variable pointed to by
* dollars75Miles.
* 3) Assign the proper calculated value to the variable pointed to by
* dollars500Miles.
*/

cout << fixed << setprecision(2);

cout << *dollars20Miles << " " << *dollars75Miles << " " << *dollars500Miles << endl;

return 0;
}

1 Answer

4 votes

Answer:

Driving costs (with pointers)

Complete the program below using pointers.

Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75 miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can be achieved by executing

cout << fixed << setprecision(2); once before all other cout statements. This line is included for you.

Ex: If the input is:

20.0 3.1599

the output is:

3.16 11.85 79.00

Note: Real per-mile cost would also include maintenance and depreciation.

#include

#include

using namespace std;

int main() {

/* Update the declarations below so that each variable points to a

* double on the heap.

*/

double *milesPerGallon;

double *dollarsPerGallon;

double *dollars20Miles;

double *dollars75Miles;

double *dollars500Miles;

/* Write the statements here to:

* 1) Read a value from the keyboard into the variable pointed to by

* milesPerGallon.

* 2) Read a value from the keyboard into the variable pointed to by

* dollarsPerGallon.

*/

/* Write the appropriate statements here to:

* 1) Assign the proper calculated value to the variable pointed to by

* dollars20Miles.

* 2) Assign the proper calculated value to the variable pointed to by

* dollars75Miles.

* 3) Assign the proper calculated value to the variable pointed to by

* dollars500Miles.

*/

cout << fixed << setprecision(2);

cout << *dollars20Miles << " " << *dollars75Miles << " " << *dollars500Miles << endl;

return 0;

}

User Anderson Marques
by
6.9k points