Question

In: Computer Science

Given that Sale[NUM_ROW][NUM_COLUMN] is a two dimensional array of float- point type and the two constants...

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.

Solutions

Expert Solution

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:


Related Solutions

A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the...
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output:             Lab    Quiz      Exam       Final     Student 1          ## ## ## ## Student 2          ## ## ## ## Student 3          ## ## ## ## The...
For a given two-dimensional array in C as follows (Each int-type element occupies 4 bytes of...
For a given two-dimensional array in C as follows (Each int-type element occupies 4 bytes of memory) int A[8][16]; If the address of A[1][4] is 0x0FFA0040, what is the memory address of A[2][6]?
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
Ceate a two dimensional array of int type to hold scores (on a scale of 0...
Ceate a two dimensional array of int type to hold scores (on a scale of 0 to 100) for 10 students (row) for 3 courses (columns) and initialize the array with your preferred data. All the students get 10 bonus points for course #2 (index 1) . Use for a loop to add the bonus points. c programming language
You're given the type length = In of float | Cm of float. Write an OCaml...
You're given the type length = In of float | Cm of float. Write an OCaml function addlen(x,y) that adds two lengths. If x is in inches, the sum should be in inches; if x is in centimeters, the sum should be in centimeters. (Reminder: 2.54 cm per inch.)
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
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: 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...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT