Tuesday, 8 March 2016

Write a program that includes a call and the definition of a function that takes in an integer array and its size. This function should find the median and return it

Leave a Comment
Questions:

Write a program that includes a call and the definition of a function that takes in an integer array and its size. This function should find the median and return it

Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

The median is the middle value, so I'll have to rewrite the list in order:

13, 13, 13, 13, 14, 14, 16, 18, 21

There are nine numbers in the list, so the middle one will be the(9 + 1) ÷ 2 = 10 ÷ 2 = 5th number :

13, 13, 13, 13, 14, 14, 16, 18, 21


So the median is 14.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;

int findMedian(int[], int);
void main() {
       int arr[9];
       for (int i = 0; i < 9; i++) {
              cout << "Enter Value of " << i + 1 << " : ";
              cin >> arr[i];
       }

       //According to Formula
       int median = findMedian(arr, 9);
       cout << "The Median is == " << median<<endl;
}


int findMedian(int arr[], int size) {
       return arr[(size + 1) / 2];

}

Output:


If You Enjoyed This, Take 5 Seconds To Share It