2

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • @RetiredNinja nope it still doesn't work :( – Cuckoo Beats Jun 04 '22 at 05:22
  • 3
    "without using vectors" is a weird condition. Why? Also see [this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – n. m. could be an AI Jun 04 '22 at 05:29
  • 1
    You ask for `x + 1` numbers as an input, but then apply `std::sort` to first `x` only. Why is that? – Evg Jun 04 '22 at 05:30
  • Seems like this program would invoke undefined behavior if the user enters 2500 or larger on the first line of stdin. – Jeremy Friesner Jun 04 '22 at 05:33
  • 2
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Jun 04 '22 at 05:35
  • 3
    a debugger can help you realize this quickly, you really need to learn how to use it if you want to learn programming [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714) – phuclv Jun 04 '22 at 05:44
  • @evg Sorry, forgot I said anything, you are right to average the two for even numbered inputs. – Goswin von Brederlow Jun 04 '22 at 05:45
  • The mean is the output expected, so only the median is wrong. Output the sorted array, output the calculated index/indices and the referred to value (by the index), then you see, which step had a problem. – Sebastian Jun 04 '22 at 06:55

3 Answers3

1
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;

if x is odd then (x-1)/2 and x/2 returns the same value, so (arr[(x-1)/2] + arr[x/2])/2.0 is just equivalent to arr[x/2]. You need to use

(arr[x/2] + arr[(x + 1)/2])/2.0

or

(arr[x/2] + arr[x/2 + 1])/2.0
phuclv
  • 37,963
  • 15
  • 156
  • 475
1

Two issues:

The odd/even check is reversed. If the number of items is even the middle two numbers are averaged and if it is odd the middle number is the median.

if (x%2==0)

should be if (x%2!=0) or if (x%2) You could also swap the contents of the if and else.

The calculation of the two middle indices is incorrect.

((arr[(x-1)/2] + arr[x/2])/2.0)

should be

((arr[x / 2 - 1] + arr[x / 2]) / 2.0)

With both of those issues corrected the output is the expected

43900.6
44627.5

online example of corrected code

Minor issues:

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

If x == 10 this will attempt to read 11 numbers. It should be i < x.

for (int i = 0; i <= (x-1); i++)

This is correct, but can be simplified to i < x.

Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • `if (x%2!=0)` often is shortened to `if (x%2)`, which can be read quite naturally as `!= 0` as it appears so often. – Sebastian Jun 04 '22 at 07:07
  • 1
    @Sebastian Good point, that is how I would normally write it too but didn't want to lose the `==` to `!=` change so I showed both. If I was rewriting it completely I'd just have `double median = arr[x / 2]; if (x % 2 == 0) { median += arr[x / 2 - 1]; median /= 2.0; }` and avoid the `else` and duplication. – Retired Ninja Jun 04 '22 at 07:21
-1

Change your if condition. x is the length of the array from 0. So if length of array is 12, x will essentially be 11.

First of all, the input loop should be

for(int i=0;i<x;i++) {
    cin>>arr[i];
}

and the median should be like

if (x%2 == 1)
    cout<<fixed<<setprecision(1)<<arr[x/2]<<endl;
else 
    cout<<fixed<<setprecision(1)<<((arr[(x-1)/2] + arr[x/2])/2.0)<<endl;

qwertyuiop
  • 39
  • 4