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!

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!

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.

Okay, so how can I get this point of reference for the above array? Sorry that's what I'm tryin get to.

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?

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

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 }

https://www.mathsisfun.com/median.html

{ 0, 1, 1, 1, 1, 2, 2, 2, 2, 2,

Last edited on

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.

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.

If the length of array is even, we have two median:

1.

2.

If the length of array is odd, we have only one median:

1.

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

Note that

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.
Note that count + 1 is real length of the array tamalesConsumed in your code. |

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.

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.

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.

0.5*(a[(n-1)/2]+a[n/2])

whether the length n is even or odd. Integer division works in your favour.

Its okay ywxk I made the same mistake thinking I'd have to use two numbers. I appreciate the help so far!

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.

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?

Registered users can post here. Sign in or register to post.