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

HELP C++ You are given an array x of int elements along with an int variable n t

ID: 3574909 • Letter: H

Question

HELP C++

You are given an array  x of int elements along with an int variable  n that contains the number of elements in the array . There are no duplicates in this array . You are also given an int variable  m that has been declared . Assign to m the median value of the array .

NOTE: The median of a set of numbers is the number in the set such that there are as many numbers above that value as there are below. If the set has an even number of members, the median is the average of the pair of numbers such that there are as many numbers above the pair as there are below.

EXAMPLE 1: Given 5 8 1 9 7 the median is 7 because there are two numbers below (1 5) and two numbers above (8 9).

EXAMPLE 2: Given 3 7 1 4 6 9 the median is the average of 4 and 6 because there are two numbers above 4 and 6 (7 9) and two numbers below (3 1).

Explanation / Answer

To find the median of the array firstly we need to sort the array. We can sort the array in the main method itself but to make the code readable and user friendly, we will be doing that in a different method.

Comments are inserted in the program for explanation. The code is tested for above example.

Please find below the code:

#include <iostream>

using namespace std;

int median(int x, int n);

int sort(int x, int n);

int main()

{

   int x[100];
//Initially number of elemets is taken as 100. We will fill only the required number of elements.
   int n,m;
  
  
   cout << "Enter the number of elements: ";
  
   cin >> n; //number of elements in array

  
  
   //fill in the value of array
  
   for(int i=0; i<n; i++)
  
   {
  
       cout << " Fill in the "<< i+1 << " number. :";
  
       cin >> x[i];
  
   }

  
  
   m = sort(x, n);
//Assign median value to variable m
   return 0;

}

int median(int x, int n)

{
  
//CALCULATE THE MEDIAN (middle number)
  
   if (n % 2 != 0)
  
   { // is the number of elements odd?
  
       int temp = ((n+1)/2)-1;

       int m = x[temp];
       /* If you want to display the value use this:
       cout << "The median is " << m << endl;
  
       */
   }
  
   else
  
   { // then it's even!
  
       int a = x[(n/2)-1];
  
       int b = x[n/2];
  
       int m = (a+b)/2; // Median for even number of array elemnts will be average of middle numbers.
       /* If you want to display the value use this:
       cout << " The median is : " <<
m;
       cout << "This is average of "<< a << " and " << b;

       */
   }

return m;
}

void sort(int x[], int n)

{

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

   for(int y=0; y<n-1; y++)
   {

       if(x[y] > x[y+1])
       {

           int temp = x[y+1];

x[y+1] = x[y];
      
x[y] = temp;

   }

}
}

int m = median(x, n); //We will claculate the median from this sorted array
return m;
   //Return the median value.
}