/* 2D Array Operations - This program creates a two-dimensional array
initialized with test data. The program has the following functions:
* getTotal - This function accepts a two-dimensional array as its
argument and return the total of all the values in the array.
* getAverage - This function accepts a two-dimensional array as
its argument and terturn the average of all the values in the
array.
* getRowTotal - This function accepts a two-dimensional array as
its first argument and an integer as its second argument. The
second argument is the first subscript of a row in the array.
The function returns the total of the values in the specified
row.
* getColumnTotal - This function accepts a two-dimensional array
as its first argument and an integer as as its second argument.
The second argument is the subscript of a column in the array.
The function returns the total of the values in the specified
column.
* getHighestInRow - This function accepts a two-dimensional array
as its first argument and an integer as its second argument. The
second argument is the first subscript of a row in the array. The
function returns the highest value in the specified row in the
array.
* getLowestInRow - This function accepts a two-dimensional array
as its first argument and an integer as its second argument. The
second argument is the first subscript of a row in the array. The
function returns the lowest value in the specified row of the array.
This program demonstrates each of the functions in this program. */
#include "Utility.h"
/* Global Constants: Number of rows, Number of Columns */
const int NUM_ROWS = 3,
NUM_COLS = 5;
/* Function prototypes: Switch operations, Validate input rows, Validate input
columns, Get total, Get average, Get row total, Get
column total, Get highest in row, Get lowest in row */
void switchOps(double [][NUM_COLS]);
bool valInputRows(int);
bool valInputCols(int);
double getTotal(double [][NUM_COLS]);
double getAverage(double [][NUM_COLS]);
double getRowTotal(double [][NUM_COLS], int);
double getColumnTotal(double [][NUM_COLS], int);
double getHighestInRow(double [][NUM_COLS], int);
double getLowestInRow(double [][NUM_COLS], int);
int main()
{
double magicApples[NUM_ROWS][NUM_COLS] = { { 17, 35, 98, 12, 44 },
{ 29, 19, 44, 5, 67 },
{ 15, 22, 18, 31, 97 } };
switchOps(magicApples);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: switchOps
This function accepts magicApples[][] as its argument.
It provides a menu from which the user can select. When a
valid item is selected, the appropriated functions are
called, and the returned result is displayed.
********************************************************** */
void switchOps(double magicApples[][NUM_COLS])
{
/* Constants: Get total, Get average, Get row total, Get column
total, Get highest number in row, Get lowest number
in row */
const int GET_TOTAL = 1,
GET_AVERAGE = 2,
GET_ROW_TOTAL = 3,
GET_COL_TOTAL = 4,
GET_HIGHEST_IN_ROW = 5,
GET_LOWEST_IN_ROW = 6,
QUIT = 7;
/* Variables: Pick array operation, Pick row, Pick column,
Is valid (input validation), Get total, Get
average, Get row total, Get column total,
Get highest in row, Get lowest in row */
int pickOp = 0,
pickRow = 0,
pickCol = 0;
bool isValid = false;
static double gTotal = 0.0,
gAverage = 0.0,
gRowTotal = 0.0,
gColTotal = 0.0,
gHighRow = 0.0,
gLowRow = 0.0;
do
{
cout << "\n\t\tMAGIC ACADEMY - TEST CENTER\n\n"
<< "\t\t1. TOTAL SUM\n"
<< "\t\t2. AVERAGE SUM\n"
<< "\t\t3. TOTAL SUM ROWS\n"
<< "\t\t4. TOTAL SUM COLUMNS\n"
<< "\t\t5. HIGHEST NUMBER IN ROW\n"
<< "\t\t6. LOWEST NUMBER IN ROW\n"
<< "\t\t7. QUIT.\n\n"
<< "\t\tCHOOSE: ";
cin >> pickOp;
while (pickOp < GET_TOTAL || pickOp > QUIT)
{
cout << "\n\t\tInvalid selection!\n"
<< "\t\tCHOOSE: ";
cin >> pickOp;
}
/* Setup numeric output formatting */
cout << showpoint << fixed << setprecision(2);
switch (pickOp)
{
case 1:
{
gTotal = getTotal(magicApples);
cout << "\n\t\tThe total amount of magic apples found was: "
<< gTotal << "\n\n";
}
break;
case 2:
{
gAverage = getAverage(magicApples);
cout << "\n\t\tThe average amount of magic apples found was: "
<< gAverage << "\n\n";
}
break;
case 3:
{
cout << "\n\t\tPlease enter row # (1 through 3): ";
cin >> pickRow;
/* Validate Input */
while (isValid = valInputRows(pickRow) == false)
{
cin >> pickRow;
}
gRowTotal = getRowTotal(magicApples, pickRow);
cout << "\t\tThe sum total of all magic apples found in row #"
<< pickRow << " was: " << gRowTotal << "\n\n";
}
break;
case 4:
{
cout << "\n\t\tPlease enter a column # (1 through 5): ";
cin >> pickCol;
/* Validate Input */
while (isValid = valInputCols(pickCol) == false)
{
cin >> pickCol;
}
gColTotal = getColumnTotal(magicApples, pickCol);
cout << "\n\t\tThe sum total of all magic apples found in column #"
<< pickCol << " is: " << gColTotal << "\n\n";
}
break;
case 5:
{
cout << "\n\t\tPlease enter a row number (1 through 3): ";
cin >> pickRow;
/* Validate Input */
while (isValid = valInputRows(pickRow) == false)
{
cin >> pickRow;
}
gHighRow = getHighestInRow(magicApples, pickRow);
cout << "\n\t\tThe highest amount of magic apples found in row #"
<< pickRow << " is: " << gHighRow << "\n\n";
}
break;
case 6:
{
cout << "\n\t\tPlease enter a row number (1 through 3): ";
cin >> pickRow;
/* Validate Input */
while (isValid = valInputRows(pickRow) == false)
{
cin >> pickRow;
}
gLowRow = getLowestInRow(magicApples, pickRow);
cout << "\t\tThe lowest amount of magic apples found in row #"
<< pickRow << " is: " << gLowRow << "\n\n";
}
break;
case 7:
cout << "\n\t\tCLOSING: Test Center ...";
break;
}
} while (pickOp != QUIT);
}
/* **********************************************************
Definition: valInputRows
This function validates the input for rows.
********************************************************** */
bool valInputRows(int pickRow)
{
bool isValid = false;
pickRow <= 0 || pickRow > NUM_ROWS ?
cout << "\t\tPlease enter row # (1 through 3): ", isValid = false :
isValid = true;
return isValid;
}
/* **********************************************************
Definition: valInputCols
This function validates the input for columns.
********************************************************** */
bool valInputCols(int pickCol)
{
bool isValid = false;
pickCol <= 0 || pickCol > NUM_COLS ?
cout << "\t\tPlease enter col # (1 through 5): ", isValid = false :
isValid = true;
return isValid;
}
/* **********************************************************
Definition: getTotal
This function accepts magicApples[][] as its argument and
returns the total of all the values in the array.
Expected output: 553
********************************************************** */
double getTotal(double magicApples[][NUM_COLS])
{
/* Variables: Index x, Index y */
int idxX = 0,
idxY = 0;
/* Variable: Total sum */
double total = 0.0;
for (idxX = 0; idxX < NUM_ROWS; ++idxX)
{
for (idxY = 0; idxY < NUM_COLS; ++idxY)
{
total += magicApples[idxX][idxY];
}
}
return total;
}
/* **********************************************************
Definition: getAverage
This function accepts magicApples[][] as its argument and
returns the average of all the values in the array.
Expected output: 36.86 (rounded to 37 with ceil function).
********************************************************** */
double getAverage(double magicApples[][NUM_COLS])
{
/* Variables: Index x, Index y */
int idxX = 0,
idxY = 0;
/* Variables: Total sum, Average sum */
double total = 0,
average = 0;
for (idxX = 0; idxX < NUM_ROWS; ++idxX)
{
for (idxY = 0; idxY < NUM_COLS; ++idxY)
{
total += magicApples[idxX][idxY];
}
}
average = total / (NUM_COLS * NUM_ROWS);
return ceil(average);
}
/* **********************************************************
Definition: getRowTotal
This function accepts magicApples[][] as its first, and
pickRow, an integer, as its second argument. The second
argument is the first subscript of a row in the array.
The function returns the total of the values in the
specified row.
Expected output top -> bottom: 206, 164, 183
********************************************************** */
double getRowTotal(double magicApples[][NUM_COLS], int pickRow)
{
/* Variable: Index y */
int idxY = 0;
/* Variable: Row total */
double rowTotal = 0.0;
/* The ternary operator determines the row total based
on user input. - 1 catches the 1-off problem. */
for (idxY = 0; idxY < NUM_COLS; ++idxY)
{
rowTotal += magicApples[pickRow - 1][idxY];
}
return rowTotal;
}
/* **********************************************************
Definition: getColumnTotal
This function accepts magicApples[][] as its first, and
pickCol, an integer, as as its second argument. The second
argument is the subscript of a column in the array. The
function returns the total of the values in the specified
column.
Expected output left -> right: 61, 76, 160, 48, 208
********************************************************** */
double getColumnTotal(double magicApples[][NUM_COLS], int pickCol)
{
/* Variable: Index x */
int idxX = 0;
/* Variable: Column total */
double colTotal = 0.0;
/* The ternary operator determines the column total based
on user input. - 1 catches the 1-off problem. */
for (idxX = 0; idxX < NUM_ROWS; ++idxX)
{
colTotal += magicApples[idxX][pickCol - 1];
}
return colTotal;
}
/* **********************************************************
Definition: getHighestInRow
This function accepts magicApples[][] as its first, and
pickRow, an integer, as its second argument. The second
argument is the first subscript of a row in the array. The
function returns the highest value in the specified row in
the array.
Expected output top -> bottom: 98, 67, 97
********************************************************** */
double getHighestInRow(double magicApples[][NUM_COLS], int pickRow)
{
/* Variable: Index y */
int idxY = 0;
/* Variable: Highest number in row */
double rowHighest = 0.0;
/* The ternary operator determines the highest value in the row
specified by user input. - 1 catches the 1-off problem. */
for (idxY = 0; idxY < NUM_COLS; ++idxY)
{
rowHighest < magicApples[pickRow - 1][idxY] ?
rowHighest = magicApples[pickRow - 1][idxY] :
rowHighest;
}
return rowHighest;
}
/* **********************************************************
Definition: getLowestInRow
This function accepts magicApples[][] as its first, and
pickRow, an integer, as its second argument. The second
argument is the first subscript of a row in the array. The
function returns the lowest value in the specified row in
the array.
Expected output top -> bottom: 12, 5, 15
********************************************************** */
double getLowestInRow(double magicApples[][NUM_COLS], int pickRow)
{
/* Variable: Index y */
int idxY = 0;
/* Variable: Row lowest (initialized to magicApples[pickRow -1]
column subscript 0) */
double rowLowest = magicApples[pickRow - 1][0];
/* The ternary operator determines the lowest value in the row
specified by user input. - 1 catches the 1-off problem. */
for (idxY = 0; idxY < NUM_COLS; ++idxY)
{
rowLowest = magicApples[pickRow - 1][idxY] < rowLowest ?
rowLowest = magicApples[pickRow - 1][idxY] :
rowLowest;
}
return rowLowest;
}
Thursday, March 2, 2017
Programming Challenge 7.19 - 2D Array Operations
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment