I wanted to code a program to find the mean and median of a sorted array (so that I can do my maths homework faster) without using vectors. I wrote this program in HackerRank:
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[2500],x;
double sum, mean;
cin>>x;
//solving for mean
for(int i = 0; i <= x; i++) {
cin>>arr[i];
}
sort(arr, arr + x);
sum = 0.0;
for (int i = 0; i <= (x-1); i++)
{
sum += arr[i];
}
mean = sum/x;
cout<<fixed<<setprecision(1)<<mean<<endl;
//solving for median
if (x%2==0)
cout<<fixed<<setprecision(1)<<arr[x/2]<<endl;
else
cout<<fixed<<setprecision(1)<<((arr[(x-1)/2] + arr[x/2])/2.0)<<endl;
return 0;
}
The input was:
10
64630 11735 14216 99233 14470 4978 73429 38120 51135 67060
And the expected output was:
43900.6
44627.5
But my output is:
43900.6
51135
I am unable to figure out the issue so please help