Question

In: Computer Science

I need this in C++ please. I started it, but I need to add Get Average,...

I need this in C++ please. I started it, but I need to add Get Average, Get Row total, and Get highest in row to my source code. But I keep getting build errors. Can someone please help me with my code. I attached my code after the question. Thank you!

Write a program that create a two-dimensional array initialized with test data. The program should have the following functions:

getTotal - This function should accept 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 values in the array.

getRowTotal - 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 total of the values in the specified row.

getColumnTotal - 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 total of the values 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 in 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 in the array.

Demonstrate each of the function in this program.

my source code:

#include <iostream>
using namespace std;

const int ROWS = 4;
const int COLS = 5;

int getTotal(int [][COLS], int, int);
int getColumnTotal(int [][COLS], int, int);
int getLowestInRow(int [][COLS], int, int);


int main()

{
   int paArray[ROWS][COLS] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20} };

   cout << "The total equals " << getTotal(paArray, ROWS, COLS) << "\n";
   cout << "The total of column 3 is " << getColumnTotal(paArray, 3, ROWS) << "\n";
   cout << "The lowest in row 3 is " << getLowestInRow(paArray, 3, COLS) << "\n";

   system("pause");
   return 0;
         
}

int getTotal(int array[][COLS], int rows, int cols)
{
   int sum = 0;

   for (int row = 0; row < rows; row++)
   {
       for (int col = 0; col < cols; col++)
           sum += array[row][col];
   }

   return sum;
}

int getColumnTotal(int array[][COLS], int colToTotal, int rows)
{
   int sum = 0;
   for (int row = 0; row < rows; row++)
   {
       sum += array[row][colToTotal];
   }

   return sum;
}

int getLowestInRow(int array[][COLS], int rowToSearch, int cols)
{
   int lowest = array[rowToSearch][0];

   for (int col = 1; col < cols; col++)
   {
       if (array[rowToSearch][col] < lowest)
           lowest = array[rowToSearch][col];
   }

   return lowest;
}

Solutions

Expert Solution

C++ Program:

#include <iostream>
using namespace std;

const int ROWS = 4;
const int COLS = 5;

int getTotal(int [][COLS], int, int);
int getColumnTotal(int [][COLS], int, int);
int getLowestInRow(int [][COLS], int, int);

/* Adding function prototypes */
double getAverage(int [][COLS], int, int);
int getRowTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int, int);

int main()
{
int paArray[ROWS][COLS] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20} };

cout << "The total equals " << getTotal(paArray, ROWS, COLS) << "\n";
cout << "The average equals " << getAverage(paArray, ROWS, COLS) << "\n";
cout << "The total of column 3 is " << getColumnTotal(paArray, 3, ROWS) << "\n";
cout << "The total of row 2 is " << getRowTotal(paArray, 2, ROWS) << "\n";
cout << "The lowest in row 3 is " << getLowestInRow(paArray, 3, COLS) << "\n";
cout << "The highest in row 2 is " << getHighestInRow(paArray, 2, COLS) << "\n";

system("pause");
return 0;
}

int getTotal(int array[][COLS], int rows, int cols)
{
int sum = 0;

for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
sum += array[row][col];
}

return sum;
}

int getColumnTotal(int array[][COLS], int colToTotal, int rows)
{
int sum = 0;
for (int row = 0; row < rows; row++)
{
sum += array[row][colToTotal];
}

return sum;
}

int getLowestInRow(int array[][COLS], int rowToSearch, int cols)
{
int lowest = array[rowToSearch][0];

for (int col = 1; col < cols; col++)
{
if (array[rowToSearch][col] < lowest)
lowest = array[rowToSearch][col];
}

return lowest;
}

//Function that calculates average
double getAverage(int array[][COLS], int rows, int cols)
{
//Calculating total of array
int total = getTotal(array, rows, cols);

//Calculating average
double avg = total/(double)(rows * cols);

//REturning average
return avg;
}

//Function that calculates row total
int getRowTotal(int array[][COLS], int rowToTotal, int cols)
{
//Initially set sum to 0
int sum = 0;

//Iterating over array
for (int col = 0; col < cols; col++)
{
//Accumulating sum
sum += array[rowToTotal][col];
}

//Return sum
return sum;
}

//Function that gets highest value in a row
int getHighestInRow(int array[][COLS], int rowToSearch, int cols)
{
//Initially assume starting value is highest
int highest = array[rowToSearch][0];

//Iterating over each column
for (int col = 1; col < cols; col++)
{
//Comparing value
if (array[rowToSearch][col] > highest)
//Updating highest value
highest = array[rowToSearch][col];
}

//Returning highest value
return highest;
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

Please, I need to get this right. No one is yet to get it right, it...
Please, I need to get this right. No one is yet to get it right, it has been answered on Chegg but is wrong In a psychrometric process, Volume of a classroom = 300 m^3. and temperature in the room is T= 20 oC, P=100 kPa relative humidity = 50% if all the water vapor of air in the room is converted to liquid at the same temperature, what is the volume of the liquid water (mL). I believe the...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
if I need to get the sum of 6 numbers, here is a C programming solution...
if I need to get the sum of 6 numbers, here is a C programming solution int getSum(int number1, int number2, int number3, int number4, int number5, int number6); and the function is: int SUM; sum = number1+number2+number3+number4+number5+number6; return SUM; this function is called in main, and the main looks like: int totalSum; totalSum=getSum(1,2,3,4,5,6); while(1); so how can I write this C programming code in ARM assembly code, can you please write the comment of each line so I can...
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
Accounting - the principle of auditing I need to know how you get the answer, please....
Accounting - the principle of auditing I need to know how you get the answer, please. 1) explain when revenue was earned in each of the following independent situations: a. on a February 28, a farmer went to local seed supply store and ordered 600 Kg of seed and promises to pay. on March 2, the framer was informed that the seeds have arrived at the seed store. on the same day, the farmer went to the store, paid for...
I need implement the operation bellow. Please add comment on some lines to compare with my...
I need implement the operation bellow. Please add comment on some lines to compare with my code. The program will implement the following operations on the Matrix class: 1. Matrix addition 2. Matrix subtraction 3. Matrix multiplication 4. Matrix scalar multiplication 5. Matrix transpose Vector class will implement the following operations: 1. Vector length 2. Vector inner (dot product) 3. Vector angle Vector 3D Class The class Vector3D is derived from the Vector class, and it implements a vector in...
I need to write about    Apple Inc..    please can I get all the information about history...
I need to write about    Apple Inc..    please can I get all the information about history of apple inc., also I need information about bonds , investment, earning per share, stockholder's equity , I need to write about 5 pages , so need more information . all relates to apple inc.
( Posting the same question for third time. Can I please get answer in C++.( and...
( Posting the same question for third time. Can I please get answer in C++.( and not in C or Java) ..... Please Try and Provide me a complete programm Answer. not just one or two function) * Declare a single dimensional array of 65 characters. Convert each character into an integer and store it in a linked list. Manipulate the linked list by completing the following task: Create an additional linked list call greater_List, using the original linked list...
Hannah wants to start a Wellness Center. She will need $181096 to get started. It will...
Hannah wants to start a Wellness Center. She will need $181096 to get started. It will take time for the business to become profitable so she expects that she can repay $11570 each year for the first five years. Each year after that she will increase her payment by X so her 6th payment would be $11570+X, and the 7th would be $11570+2X. If the bank is charging 5.2%/year compounded annually, and the loan is a ten year loan what...
I need to get the MATLAB calculation for this but I can't. I past the exact...
I need to get the MATLAB calculation for this but I can't. I past the exact same thing into MATLAB R2016b Editor but it gives me error. Please write your answer and telling what is wrong with the code. What should I add or remove in this code to get the answer and calculations. Please explain step by step this is the first time ever that I am using MATLAB. Thanks I need to get the MATLAB calculation for this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT