In: Computer Science
|
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. |
|
|
be the subscript of a row in the array. The function should return the total of the values in the specified row.
|
|
▪ getLowestInRow . This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the lowest value in the specified row of the array. Demonstrate each of the functions in this program. |
Sample run:
DocViewer
Page
of 3Zoom
Pages
#include <iostream>
using namespace std;
// Constant for the number of columns
const int COLS = 5;
// Function prototypes
int getTotal(int [][COLS], int);
double getAverage(int [][COLS], int);
int getRowTotal(int [][COLS], int);
int getColumnTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int);
int getLowestInRow(int [][COLS], int);
int main()
{
const int ROWS = 4; // Constant for the number of rows
// Array with test data
int testArray[ROWS][COLS] =
{ { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 } };
// Display the total of the array elements.
cout << "The total of the array elements is "
<< getTotal(testArray, ROWS)
<< endl;
// Display the average of the array elements.
cout << "The average value of an element is "
<< getAverage(testArray, ROWS)
<< endl;
// Display the total of row 0.
cout << "The total of row 0 is "
<< getRowTotal(testArray, 0)
<< endl;
// Display the total of column 2.
cout << "The total of col 2 is "
<< getColumnTotal(testArray, 2, ROWS)
<< endl;
// Display the highest value in row 2.
cout << "The highest value in row 2 is "
<< getHighestInRow(testArray, 2)
<< endl;
// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2)
<< endl;
system("PAUSE");
return 0;
}
// ********************************************************
// The getTotal function returns the total of all *
// the elements in the array. *
// ********************************************************
int getTotal(int array[][COLS], int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getAverage function returns the averave value *
// of the elements in the array. *
// ********************************************************
double getAverage(int array[][COLS], int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getRowTotal function returns the total of the *
// the elements in the specified row of the array. *
// ********************************************************
int getRowTotal(int array[][COLS], int rowToTotal)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getColTotal function returns the total of the *
// the elements in the specified column of the array. *
// ********************************************************
int getColumnTotal(int array[][COLS], int colToTotal, int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getHighestInRow function returns the highest *
// value in the specified row. *
// ********************************************************
int getHighestInRow(int array[][COLS], int rowToSearch)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getLowestInRow function returns the lowest *
// value in the specified row. *
// ********************************************************
int getLowestInRow(int array[][COLS], int rowToSearch)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
Annotations
#include <iostream>
using namespace std;
// Constant for the number of columns
const int COLS = 5;
// Function prototypes
int getTotal(int [][COLS], int);
double getAverage(int [][COLS], int);
int getRowTotal(int [][COLS], int);
int getColumnTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int);
int getLowestInRow(int [][COLS], int);
int main() {
const int ROWS = 4; // Constant for the number of rows
// Array with test data
int testArray[ROWS][COLS] =
{{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
// Display the total of the array elements.
cout << "The total of the array elements is "
<< getTotal(testArray, ROWS)
<< endl;
// Display the average of the array elements.
cout << "The average value of an element is "
<< getAverage(testArray, ROWS)
<< endl;
// Display the total of row 0.
cout << "The total of row 0 is "
<< getRowTotal(testArray, 0)
<< endl;
// Display the total of column 2.
cout << "The total of col 2 is "
<< getColumnTotal(testArray, 2, ROWS)
<< endl;
// Display the highest value in row 2.
cout << "The highest value in row 2 is "
<< getHighestInRow(testArray, 2)
<< endl;
// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2)
<< endl;
system("PAUSE");
return 0;
}
// ********************************************************
// The getTotal function returns the total of all *
// the elements in the array. *
// ********************************************************
int getTotal(int array[][COLS], int rows) {
int total = 0, count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < COLS; ++j) {
total += array[i][j];
}
}
return total;
}
// ********************************************************
// The getAverage function returns the averave value *
// of the elements in the array. *
// ********************************************************
double getAverage(int array[][COLS], int rows) {
double total = 0, count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < COLS; ++j) {
total += array[i][j];
count += 1;
}
}
return total / count;
}
// ********************************************************
// The getRowTotal function returns the total of the *
// the elements in the specified row of the array. *
// ********************************************************
int getRowTotal(int array[][COLS], int rowToTotal) {
int total = 0;
for (int i = 0; i < COLS; ++i) {
total += array[rowToTotal][i];
}
return total;
}
// ********************************************************
// The getColTotal function returns the total of the *
// the elements in the specified column of the array. *
// ********************************************************
int getColumnTotal(int array[][COLS], int colToTotal, int rows) {
int total = 0;
for (int i = 0; i < rows; ++i) {
total += array[i][colToTotal];
}
return total;
}
// ********************************************************
// The getHighestInRow function returns the highest *
// value in the specified row. *
// ********************************************************
int getHighestInRow(int array[][COLS], int rowToSearch) {
int highestIndex = 0;
for (int i = 0; i < COLS; ++i) {
if (array[rowToSearch][i] > array[rowToSearch][highestIndex]) {
highestIndex = i;
}
}
return array[rowToSearch][highestIndex];
}
// ********************************************************
// The getLowestInRow function returns the lowest *
// value in the specified row. *
// ********************************************************
int getLowestInRow(int array[][COLS], int rowToSearch) {
int lowestIndex = 0;
for (int i = 0; i < COLS; ++i) {
if (array[rowToSearch][i] < array[rowToSearch][lowestIndex]) {
lowestIndex = i;
}
}
return array[rowToSearch][lowestIndex];
}