Output a table based on two dimensional array and control the field width - C++ Data Type

C++ examples for Data Type:Array

Description

Output a table based on two dimensional array and control the field width

Demo Code

#include <iostream>
using namespace std;
#include <iomanip>
void main()/*from   w  w w .j  av  a2 s  .c  o 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;
}

Result


Related Tutorials