235k views
1 vote
Write a C++ program that has a declaration to store the following values in an array named prices: 17.61, 15.22, 19.53, 13.74, 10.25 and 12.55. Include the declaration in a program that displays the values in the array by using pointer notation as shown below via I/O manipulator functions. Provide a .cpp file and a sample run. please assist and show me how you would use iomanip to have the array with 2 rows and 3 columns using c++. Please show using pointer notation as well

User Tomazahlin
by
7.8k points

1 Answer

3 votes

Final answer:

A C++ program is given which uses an array named 'prices' to store specified values and displays them using pointer notation, formatting the output in a table with 2 rows and 3 columns, utilizing the 'iomanip' library for formatting.

Step-by-step explanation:

The student has asked for a C++ program that stores specific values in an array named prices and displays these values using pointer notation with an I/O manipulator to format the output as a table with 2 rows and 3 columns.

Here is the C++ program that fulfills the requirement:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
double prices[] = {17.61, 15.22, 19.53, 13.74, 10.25, 12.55};
double* ptr = prices;

cout << fixed << setprecision(2);

for (int i = 0; i < 6; ++i) {
if (i % 3 == 0 && i != 0) {
cout << endl;
}
cout << setw(8) << *(ptr + i) << ' ';
}
cout << endl;

return 0;
}

When the program is run, it will display the prices in the requested format. The use of setw from the iomanip library assists in setting the width for each value, ensuring the table format is maintained.

User LiangWang
by
7.8k points