Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given that Sale[NUM_ROW][NUM_COLUMN] is a two dimensional array of float- point

ID: 3753869 • Letter: G

Question

Given that Sale[NUM_ROW][NUM_COLUMN] is a two dimensional array of float-

point type and the two constants are defined as follows:

#define NUM_ROW 4

#define NUM_COLUMN 4

float Value[NUM_ROW][NUM_COLUMN] =

{

5.1, 5.2, 5.3, 5.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,

2.1, 2.2, 2.3, 2.4

};

Write a C++ main function that computes and prints out the following information about

this 2d array:

(1) The mean of all the array elements (1.25 points).

(2) The local average values of all the array elements (1.25 points). For instance, the

local

average

value

at

position

i,

j

=

(Value[i-

1][j]+Value[i+1][j]+Value[i][j+1]+Value[i][j-1])/4. If (i+1) > NUM_ROW-1 or

(i-1) < 0, use modulus operator to wrap around to the other end of the array index.

Create another array: Average[NUM_ROW][NUM_COLUMN], and use this

array to store the values of local average at each position of this 2d array. At the

end, you need print out the values of all the array elements of Average[][] on

computer screen. Prepare a screenshot to show the values of these array elements.

Below is an image for showing how the ‘wrap around’ works.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

#include <iostream>
#include <iomanip>
#define NUM_ROW 4
#define NUM_COLUMN 4
using namespace std;
float Value[NUM_ROW][NUM_COLUMN] =
{
5.1, 5.2, 5.3, 5.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,
2.1, 2.2, 2.3, 2.4
};
void printArray(float arr[][NUM_COLUMN], int rows, int cols);
float meanAll(float arr[][NUM_COLUMN], int rows, int cols);
float localAvg(int i, int j, float arr[][NUM_COLUMN], int rows, int cols);
int main(){
float localAvgArr[NUM_ROW][NUM_COLUMN];
cout << fixed << setprecision(2); //show 2 decimal places
cout << "Actual array is " << endl;
printArray(Value, NUM_ROW, NUM_COLUMN);
cout << "Mean of all elements is " << meanAll(Value, NUM_ROW, NUM_COLUMN) << endl;
for(int i = 0; i < NUM_ROW; i++)
for(int j = 0; j < NUM_COLUMN; j++)
localAvgArr[i][j] = localAvg(i, j, Value, NUM_ROW, NUM_COLUMN);
cout << "The array of local averages is " << endl;
printArray(localAvgArr, NUM_ROW, NUM_COLUMN);
}
void printArray(float arr[][NUM_COLUMN], int rows, int cols){
for(int i = 0; i < rows;i++){
cout << endl;
for(int j = 0; j < cols; j++)
cout << setw(6) << arr[i][j];
}
cout << endl;
}
float meanAll(float arr[][NUM_COLUMN], int rows, int cols){
float total = 0;
for(int i = 0; i < rows;i++){
for(int j = 0; j < cols; j++)
total += arr[i][j];
}
return total / (rows * cols);
}
float localAvg(int i, int j, float arr[][NUM_COLUMN], int rows, int cols){
float total = 0;
if(i-1 < 0) //previous row not available, wrap around to bottom row
total += Value[NUM_ROW - 1][j];
else
total += Value[i - 1][j];
if(i+1 == NUM_ROW) //next row not available, wrap around to top row
total += Value[0][j];
else
total += Value[i + 1][j];
if(j-1 < 0) //previous column not available, wrap around to last column
total += Value[i][NUM_COLUMN - 1];
else
total += Value[i][j - 1];
if(j+1 == NUM_COLUMN) //next column not available, wrap around to first column
total += Value[i][0];
else
total += Value[i][j + 1];
total /= 4;
return total;
}

output
----
Actual array is
5.10 5.20 5.30 5.40
1.50 1.60 1.70 1.80
1.90 2.00 2.10 2.20
2.10 2.20 2.30 2.40
Mean of all elements is 2.80
The array of local averages is
3.55 3.55 3.65 3.65
2.60 2.60 2.70 2.70
1.95 1.95 2.05 2.05
2.90 2.90 3.00 3.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote