C++ Function Parameter array as argument

Description

C++ Function Parameter array as argument

#include <iostream>
#include <iomanip>          //for setprecision, etc.
using namespace std;
const int DISTRICTS = 4;    //array dimensions
const int MONTHS = 3;
void display( double[DISTRICTS][MONTHS] );  //declaration
int main()/*from w  w w .  j  a  v  a2  s . com*/
{                        //initialize two-dimensional array

    double sales[DISTRICTS][MONTHS]
                = {  {  143.07,   23.50,   6.01 },
                {   32.00, 1383.32, 175.88 },
                {  932.34,   93.00,  44.30 },
                { 1283.29,  233.63,    .93 }  };
    display(sales);          //call function; array as argument
    cout << endl;
    return 0;
}
//function to display 2-d array passed as argument
void display( double funsales[DISTRICTS][MONTHS] )
{
    int d, m;
    cout << "\n\n";
    cout << "                        Month\n";
    cout << "                1         2         3";
    for(d=0; d<DISTRICTS; d++)
    {
       cout <<"\nDistrict " << d+1;
       for(m=0; m<MONTHS; m++)
          cout << setiosflags(ios::fixed) << setw(10) << setiosflags(ios::showpoint) << setprecision(2) << funsales[d][m];   //array element
    }
}



PreviousNext

Related