// This program demonstrates two-dimensional arrays. // AUTHOR: Jim Etheredge #include #include #include using namespace std; const int ROWS=3; const COLUMNS=4; // If the type isn't specified, C++ figures it out // Function prototypes // Notice missing name in first argument & the const qualifier void max(const float [][COLUMNS], const string names[]); void average(const float [][COLUMNS]); int main() { // Declare a two-dimensional array to hold sales data // Each row represents a salesperson. The array is initialized // with sales amounts. float sales[ROWS] [COLUMNS] = {{123.45, 987.65, 345.67, 999.87}, {111.11, 222.22, 333.33, 444.44}, {77.54, 234.99, 1.25, 777.77}}; // A one-dimensional array of names string names[ROWS] = {"Jones", "Smith", "Davis"}; max(sales, names); average(sales); return 0; } void max(const float sales[][COLUMNS], const string names[]) { int i, j, maxRow, maxColumn; float maxValue = 0.0; for (i = 0; i < ROWS; i++) for (j = 0; j < COLUMNS; j++) if (sales[i][j] > maxValue) { maxValue = sales[i][j]; maxRow = i; maxColumn = j; }; cout.setf(ios::left); cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << names[maxRow] << " has the maximum sales value of " << setprecision(2) << sales[maxRow][maxColumn] << endl; } void average(const float sales[][COLUMNS]) { int i, j, maxRow, maxColumn; float total = 0.0, average; // Note that i and j are swapped this time for (j = 0; j < COLUMNS; j++) for (i = 0; i < ROWS; i++) total += sales[i][j]; average = total / (ROWS + COLUMNS); cout << "The average sales value is " << setprecision(2)<< average << endl; }