In: Computer Science
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] =
{
2.1, 2.2, 2.3, 2.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 Value[][] array elements (0.5 points).
(2) The median of all the Value[][] array elements (0.5 points)
(3) The local average values of all the array elements (1.0 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]+Value[i][j])/5.
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.
Parts (2) only please.
EXPLANATION:
(1)-> Mean is calculated as the sum of all items / Number of items.for that we need to add all elements in the array and divide it by its size
(2)-> Median is the middle term in a series since it is not in a sorted format first we need to sirt it then we need to find the middle term. if num of columns is odd then we need we can directly print the middle term, if it is even then we need to add the two terms and average it
(3) -> for this we need to add the local position values and store it into another array, In case if the addition and substractions of the index out of range we need to initialize them to the last and first element.
Solution:
#include<iostream>
using namespace std;
#define NUM_ROW 4
#define NUM_COLUMN 4
float Value[NUM_ROW][NUM_COLUMN] =
{
2.1, 2.2, 2.3, 2.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
};
int main(){
float mean=0,median=0;
int i,j,count=0;
float
Average[NUM_ROW][NUM_COLUMN],dArray[NUM_ROW][NUM_COLUMN];
//Mean
for(i=0;i<NUM_ROW;i++){
for(j=0;j<NUM_COLUMN;j++){
//Copying the
values to the duplicate array since we need to duplicate array for
average calculation we will sort the array
//for
calculating the median
dArray[i][j]=Value[i][j];
mean+=Value[i][j];
count++;
}
}
mean=mean/count;
cout << "Mean Value is
"<<mean<<"\n";
//for finding median we need to set the data in
ascending or descending order for that we need to sort the
array
for(int irow=0;irow<NUM_ROW;irow++)
{
for(int icol=0;icol<NUM_COLUMN;icol++)
{
for(int jrow=0;jrow<NUM_ROW;jrow++)
{
for(int jcol=0;jcol<NUM_COLUMN;jcol++)
{
if(Value[irow][jrow]<Value[icol][jcol])
{
float tem=Value[irow][jrow];
Value[irow][jrow]=Value[icol][jcol];
Value[icol][jcol]=tem;
}
}
}
}
}
if (NUM_COLUMN % 2 != 0) {
//If num of rows are odd we can directly print odd
value
cout << Value[NUM_COLUMN/2][NUM_COLUMN/2];
}
else if (NUM_COLUMN%2 == 0) {
//if num of rows are even we need to do average
of two values;
cout<<" Median is "<<
(Value[(NUM_COLUMN-2)/2][NUM_COLUMN-1]+Value[NUM_COLUMN/2][0])/2.0<<"\n";
}
//Local Average
for(int irow=0;irow<NUM_ROW;irow++)
{
float avg=0;
for(int icol=0;icol<NUM_COLUMN;icol++)
{
//Calculating the average condition when addition is greater than
num of rows and less than 0
if(irow+1>=NUM_ROW){
avg+=dArray[(irow+1)%NUM_ROW][icol];
}else{
avg+=dArray[irow+1][icol];
}
if(icol+1>=NUM_COLUMN){
avg+=dArray[irow][(icol+1)%NUM_COLUMN];
}else{
avg+=dArray[irow][icol+1];
}
if(irow-1<0){
avg+=dArray[NUM_ROW-1][icol];
}else{
avg+=dArray[irow-1][icol];
}
if(icol-1<0){
avg+=dArray[irow][NUM_COLUMN-1];
}else{
avg+=dArray[irow][icol-1];
}
avg+=dArray[irow][icol];
avg=avg/5;
Average[irow][icol]=avg;
}
}
for(int irow=0;irow<NUM_ROW;irow++)
{
cout <<irow <<" -> ";
for(int icol=0;icol<NUM_COLUMN;icol++)
{
//Printing the array
cout<< Average[irow][icol]<<" ";
}
cout<<"\n";
}
}
Code Images:
OUTPUT: