Write a program(c++ programming) that creates a two-dimensional array, reads dat
ID: 3695608 • Letter: W
Question
Write a program(c++ programming) that creates a two-dimensional array, reads data from file 2Dnumbers.txt, assigns the values to the array items, and process them. The program should have the following functions:
2Dnumbers.txt =
getTotal. This function should accept a two-dimensional array as its argument and return the total of all the values in the array.
getAverage. This function should accept a two-dimensional array as its argument and return the average of all the values in the array.
getRowAvg. 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 average of the values in the specified row.
getColumnAvg. 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 column in the array. The function should return the average of the values in the specified column.
getTargetInRow. This function should accept a two-dimensional array as its first argument and two integers as its second and third arguments. The second argument should be the subscript of a row in the array. The third argument should be the value of target in the row. The function should return the column subscript of the first occurrence of value in the specified row.
getTargetInCol. This function should accept a two-dimensional array as its first argument and two integers as its second and third arguments. The second argument should be the subscript of a column in the array. The third argument should be the value of target in the column. The function should return the row subscript of the first occurrence of value in the specified column.
getHighestInRow. 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 highest value in the specified row of the array.
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.
getHighestandLowestInCol. 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 column in the array. The function should return the highest value and also the lowest value in the specified column of the array. Use "pass by reference" to return the lowest and highest values of column.
reverseRow. 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 reverse the order of values in the specified row of the array.
Explanation / Answer
#include <iostream>
#include <limits>
using namespace std;
const int NUM_ROWS = 2;
const int NUM_COLS = 2;
int getTotal(int[][NUM_COLS]);
double getAverage(int);
int getRowTotal(int[][NUM_COLS],int);
int getColumnTotal(int[][NUM_COLS],int);
int getHighestInRow(int[][NUM_COLS],int);
int getLowestInRow(int[][NUM_COLS],int);
int main() {
int x,y,value,answer;
int num[NUM_ROWS][NUM_COLS]={10,20,30,40};
cout << "The data values are: ";
for(x = 0;x < NUM_ROWS;x++)
{
for(y = 0;y < NUM_COLS;y++)
{
if(y == NUM_COLS){
cout << num[x][y];
}
else
{
cout << num[x][y] << " ";
}
}
}
cout << " Choices: ";
cout << "1 Get the total of all the values. ";
cout << "2 Get the average of all the values. ";
cout << "3 Get the total value of all the values in the specified row. ";
cout << "4 Get the total value of all the values in the specified column. ";
cout << "5 Get the highest value in the specified row. ";
cout << "6 Get the lowest value in the specified row. ";
cout << "7 exit ";
cout << " Choose one from 1 to 7: ";
cin >> answer;
if(answer < 1 && answer > 7)
{
while(answer < 1 && answer > 7)
{
cout << "Sorry, what Option do you want? [1-7]: ";
cin >> answer;
}
}
switch(answer)
{
case 1: cout << "The total is " << getTotal(num) << endl;
break;
case 2: value = getTotal(num);
cout << "The average: " << getAverage(value) << endl;
break;
case 3: cout << "Enter Row Number: ";
cin >> x;
while(x < 0 || x > NUM_ROWS){
cout << "ERROR: Must be number 0 & " << NUM_ROWS << ": ";
cin >> x;
}
cout << "Row " << x << " total: " << getRowTotal(num, x) << endl;
break;
case 4: cout << "Enter the column number: ";
cin >> x;
while(x < 0 || x > NUM_COLS){
cout << "ERROR: Must be a number between 0 & " << NUM_COLS << ": ";
cin >> x;
}
cout << "Column " << x << " total: " << getColumnTotal(num, x) << endl;
break;
case 5: cout << "Enter the row number: ";
cin >> x;
while(x < 0 || x > NUM_ROWS){
cout << "ERROR: Must be a number between 0 & " << NUM_ROWS << ": ";
cin >> x;
}
cout << "Highest value in row " << x << " is: " << getHighestInRow(num, x) << endl;
break;
case 6: cout << "Enter the row number: ";
cin >> x;
while(x < 0 || x > NUM_ROWS){
cout << "ERROR: Must be a number between 0 & " << NUM_ROWS << ": ";
cin >> x;
}
cout << "Lowest value in row " << x << " is: " << getLowestInRow(num, x) << endl;
break;
case 7: cout << "Shutting Down...";
break;
default: cout << "ERROR: INVALID ACTION: Invalid option";
}
}
int getTotal(int row[][NUM_COLS])
{
int total = 0;
for(int c = 0;c < NUM_ROWS;c++)
{
for(int r = 0;r < NUM_COLS;r++)
{
total += row[c][r];
}
}
return total;
}
double getAverage(int total)
{
double average;
average = total/(NUM_ROWS*NUM_COLS);
return average;
}
int getRowTotal(int num[][NUM_COLS], int y)
{
int x, value = 0;
for(x = 0;x < NUM_COLS;x++)
{
value += num[y][x];
}
return value;
}
int getColumnTotal(int num[][NUM_COLS], int y){
int x, value = 0;
for(x = 0;x < NUM_ROWS;x++)
{
value += num[x][y];
}
return value;
}
int getHighestInRow(int num[][NUM_COLS], int y)
{
int x, value = 0;
value = num[y][0];
for(x = 0;x < NUM_ROWS;x++)
{
if(num[y][x] > value)
{
value = num[y][x];
}
}
return value;
}
int getLowestInRow(int num[][NUM_COLS], int y)
{
int x, value = numeric_limits<int>::max();
value = num[y][0];
for(x = 0;x < NUM_COLS;x++)
{
if(num[y][x] < value)
{
value = num[y][x];
}
}
return value;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.