# Answer to Question #12766 in C++ for Roger

Question #12766

float Median;

for (int j = 0; j < m; j++)

{

for (int i = 0; i < j; i++)

{

if (array[j] < array[i])

{

int temp = array[j][i];

array[j][i] = array[i][j];

array[i][j] = temp;

}

}

}

for (int j = 0; j < m; j++)

{

if (n % 2 == 0)

{

int i = 0;

Median = ((array[i][j]/2) + (array[i][j]/2)-1)/2;

cout << Median << endl;

}

if (n % 2 != 0)

{

int i = 0;

Median = array[i][j]/2;

cout << Median << "\n\n";

}

}

cout << "The Median of each row is displayed above.\n\n";

}

}

return 0;

}

I have been having lots of trouble trying to get the Median of each row in a 2D array. Note that m is the variable that represents the length of a row and n is the variable that represents the length of a column. The user had chose these variables earlier in the program. Also note that array[i][j] was what stored the lengths as well as the numbers the user wanted to put in the matrix. This is also earlier in the program. If I wanted to find the median of each row starting with row 1 and going to row m, how would I go about that?

for (int j = 0; j < m; j++)

{

for (int i = 0; i < j; i++)

{

if (array[j] < array[i])

{

int temp = array[j][i];

array[j][i] = array[i][j];

array[i][j] = temp;

}

}

}

for (int j = 0; j < m; j++)

{

if (n % 2 == 0)

{

int i = 0;

Median = ((array[i][j]/2) + (array[i][j]/2)-1)/2;

cout << Median << endl;

}

if (n % 2 != 0)

{

int i = 0;

Median = array[i][j]/2;

cout << Median << "\n\n";

}

}

cout << "The Median of each row is displayed above.\n\n";

}

}

return 0;

}

I have been having lots of trouble trying to get the Median of each row in a 2D array. Note that m is the variable that represents the length of a row and n is the variable that represents the length of a column. The user had chose these variables earlier in the program. Also note that array[i][j] was what stored the lengths as well as the numbers the user wanted to put in the matrix. This is also earlier in the program. If I wanted to find the median of each row starting with row 1 and going to row m, how would I go about that?

Expert's answer

float Median=0;

for (int i=0; i<n; i++){

for (int j=0; j<m;

j++){

Median=Median+array[i][j];

}

Median=Median/m;

cout<<Median<< "\n\n";

Median=0;

}

cout <<"The Median of each row is displayed

above.\n\n";

return 0;

}

for (int i=0; i<n; i++){

for (int j=0; j<m;

j++){

Median=Median+array[i][j];

}

Median=Median/m;

cout<<Median<< "\n\n";

Median=0;

}

cout <<"The Median of each row is displayed

above.\n\n";

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment