Newbie question

Sep 11, 2020 at 5:04am
Hey all, new member.

I'm working on a program to create an average of a set of arrays as well as the Median. I've pretty much gotten the average down, now it's on to incorporate the Median.

#include <iostream>
#include <algorithm>
using namespace std;


int main()
{
double tamalesConsumed[21] = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 7 };
double sum = 0;
double count = 21;
double median = 0;
float average = 0;

for (int x = 0; x < 21; x++) {
sum += tamalesConsumed[x];
cout << sum << endl;
}
average = sum / count;
cout << "The average is: " << average << endl;


return 0;
}

so how can I add a Median to this? We haven't gone over Find Median functions that I've seen elsewhere. Is there another way? Thanks!

Sep 11, 2020 at 5:38am
The median is defined as the value such that equal numbers of samples are less than it and greater than it. Therefore in a sorted array of samples, the median would be the mid-point of the array.
Sep 11, 2020 at 6:26am
Okay, so how can I get this point of reference for the above array? Sorry that's what I'm tryin get to.
Sep 11, 2020 at 6:39am
Well, if you have doors 1 to 101, how do you find the door in the middle? What formula do you use to find the door number?
Sep 11, 2020 at 8:35am
If you have an array with 5 elements, the middle element is the 3rd. For 7 elements it would be the 4th etc etc etc

Sep 11, 2020 at 1:22pm
Right. So what's the middle element of tamalesConsumed?
Sep 11, 2020 at 1:59pm
To find the median see this:
https://www.mathsisfun.com/median.html

{ 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 7 }
Last edited on Sep 11, 2020 at 1:59pm
Sep 12, 2020 at 1:29pm
To find the median, you first have to sort the array. In this case, the array is already sorted, but you should check the assignment to see if you have to deal with other arrays that might not be sorted.

If the number of items is odd then the median is the "one in the middle." If the number of items is even then there is no "middle" item and the median is the average of the two closest to the middle. E.g., for the array { 12 18 19 24 35 50 }, the median is the average of 19 & 24.
Sep 12, 2020 at 2:24pm
If the length of array is even, we have two median:
1. length / 2
2. length / 2 + 1
If the length of array is odd, we have only one median:
1. (length + 1) / 2

In this case, the array is even, so we need to calculate two median.

Note that count + 1 is real length of the array tamalesConsumed in your code. The length of tamalesConsumed is even, so you will have two median.
Sep 12, 2020 at 3:03pm
Note that count + 1 is real length of the array tamalesConsumed in your code.
No, this is incorrect. The length of tamalesConsumed is 21.
Sep 12, 2020 at 3:08pm
No. If odd, the median is the value at the position of (length + 1) / 2

If even, the median is the (sum of the value at the position (length / 2) plus value at the position (length / 2 + 1)) divided by 2.
Sep 12, 2020 at 8:05pm
For a sorted array the median is always
0.5*(a[(n-1)/2]+a[n/2])
whether the length n is even or odd. Integer division works in your favour.
Sep 13, 2020 at 12:12am
helios, you're right, I made a stupid mistake.
There is only one median.
Sep 13, 2020 at 5:58pm
Its okay ywxk I made the same mistake thinking I'd have to use two numbers. I appreciate the help so far!
Sep 13, 2020 at 6:26pm
dhayden, the assignment states that he would want it to be used as if he changed the array in the .cpp file. I guess if there was another array I believe there's a sort array that can be used as well.
Sep 18, 2020 at 3:19am
Got it, thanks for the help all. I was looking to add median and I had the syntax of the array slightly wrong. That's all it takes, though, amirite?
Sep 18, 2020 at 5:10am
JPetrucci84 wrote:
That's all it takes, though, amirite?
No.
Topic archived. No new replies allowed.