I NEED TO THIS TO BE PROGRAMMED IN C++ ONLY. NO JAVA In statistics, when a set o
ID: 3933766 • Letter: I
Question
I NEED TO THIS TO BE PROGRAMMED IN C++ ONLY. NO JAVA
In statistics, when a set of values is stored in ascending or descending order, its median is the middle value. If the set contains an even number of values, the median is the average of the two middle values. Write a function that accepts as arguments the following:
a)An array of integers
b) An integer that indicates the number of elements in the array
The function should determine the median of the array. This value should be returned as a double.
Explanation / Answer
Median.cpp
#include <iostream>
using namespace std;
double getMedian(int a[], int size);
int main()
{
int a[] = {1,2,3,4,5,6,7};
int b[] = {1,2,3,4,5,6};
cout<<getMedian(a,7)<<endl;
cout<<getMedian(b,6)<<endl;
return 0;
}
double getMedian(int a[], int size)
{
if( size % 2 ==0){
return (a[size/2] + a[(size-1)/2])/(double)2;
}
else{
return a[size/2];
}
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main
4
3.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.