C++ Array Output a table based on two dimensional array and control the field width
#include <iostream> using namespace std; #include <iomanip.h> void main()//from w w w . j a v a 2s . co m { float disks[2][4]; // Table of disk prices. int row, col; disks[0][0] = 2.39; // Row 1, column 1 disks[0][1] = 2.75; // Row 1, column 2 disks[0][2] = 3.29; // Row 1, column 3 disks[0][3] = 3.59; // Row 1, column 4 disks[1][0] = 1.75; // Row 2, column 1 disks[1][1] = 2.19; // Row 2, column 2 disks[1][2] = 2.69; // Row 2, column 3 disks[1][3] = 2.95; // Row 2, column 4 // Print the column titles. cout << "\tSingle-sided\tDouble-sided\tSingle-sided\t" << "Double-sided\n"; cout << "\tDouble-density\tDouble-density\tHigh-density" << "\tHigh-density\n"; // Print the prices for (row=0; row<2; row++) { if (row == 0) { cout << "3-1/2\"\t"; } // Need \" to print quotation. else { cout << "5-1/4\"\t"; } for (col=0; col<4; col++) // Print the current row. { cout << setprecision(2) << "$" << disks[row][col] << "\t\t"; } cout << "\n"; // Print a newline after each row. } return; }