In: Computer Science
C++ ASSIGNMENT: Two-dimensional array
Problem
Write a program that create a two-dimensional array initialized with test data. The program should have the following functions:
Use the main method below to test the program.
int main()
{
// 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, COLS)
<< endl;
// Display the average of the array elements.
cout << "The average value of an element is "
<< getAverage(testArray, ROWS, COLS)
<< endl;
// Display the total of row 0.
cout << "The total of row 0 is "
<< getRowTotal(testArray, 0, COLS)
<< 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, COLS)
<< endl;
// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2, COLS)
<< endl;
return 0;
}
Output sample
The total of the array elements is 210
The average value of an element is 10.5
The total of row 0 is 15
The total of col 2 is 42
The highest value in row 2 is 15
The lowest value in row 2 is 11
Solution:
#include<iostream>
using namespace std;
#define ROWS 4
#define COLS 5
int getTotal(int testArray[4][5], int r, int c)
{
int i,j,sum=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum=sum+testArray[i][j];
return sum;
}
float getAverage(int testArray[4][5], int r, int c)
{
int i,j,sum=0;
float avg;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum=sum+testArray[i][j];
avg=(float)sum/(float)(r*c);
return avg;
}
int getRowTotal(int testArray[4][5], int row, int c)
{
int sum=0,i;
for(i=0;i<c;i++)
sum=sum+testArray[row][i];
return sum;
}
int getColumnTotal(int testArray[4][5], int col, int r)
{
int sum=0,i;
for(i=0;i<r;i++)
sum=sum+testArray[i][col];
return sum;
}
int getHighestInRow(int testArray[4][5], int row, int c)
{
int max=testArray[row][0],i;
for(i=0;i<c;i++)
if(testArray[row][i]>max)
max=testArray[row][i];
return max;
}
int getLowestInRow(int testArray[4][5], int row, int c)
{
int min=testArray[row][0],i;
for(i=0;i<c;i++)
if(testArray[row][i]<min)
min=testArray[row][i];
return min;
}
int main()
{
// 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, COLS)<< endl;
// Display the average of the array elements.
cout << "The average value of an element is "<<
getAverage(testArray, ROWS, COLS)<< endl;
// Display the total of row 0.
cout << "The total of row 0 is "<<
getRowTotal(testArray, 0, COLS)<< 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, COLS)<< endl;
// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "<<
getLowestInRow(testArray, 2, COLS)<< endl;
return 0;
}
Output: