Im writing a program that takes in an array inputted by the suer and outputs various things about them. Im writing several functions in them and im stuck on the median. I've found a way to do it but I need to know if there is a way to remove specific elemnts in the array to get it to work. In my code ive got to remove the minimum and maximum as that's how im getting to the median. Just look at the median function its close to the bottom.
#include <iostream>
#include <vector>
usingnamespace std;
void displayData(vector<double> d); // works
//Postcondition: displays array to the console
double minimum(vector<double> d); // works
//Postcondition: returns the minimum value in the array
double maximum(vector<double> d); // works
//Postcondition: returns the maximum value in the array
double mean(vector<double> d); // works
//Postcondition: returns mean of the value in the array
double sampleVariance(vector<double> d);
//
double median(vector<double> d);
//
double firstQuartile(vector<double> d);
//
double thirdQuartile(vector<double> d);
void swap(double &x, double &y);// works
// swaps x and y
void bubblesort (vector<double> &data); // works
//Postcondition: sorts the array from least to greatest
int main() {
//get list from user and store into a vector
vector<double> list; //empty vector of doubles
cout << "Please enter your data: (Enter -1 to quit)";
double temp;
do {
cin >> temp;
list.push_back(temp);
} while(temp != -1);
list.pop_back();
//check that I took in their data correctly
displayData(list);
bubblesort(list);
displayData(list);
cout << "Min: " << minimum(list) << '\n';
cout << "Max: " << maximum(list) << '\n';
cout << "Mean: " << mean(list) << '\n';
return 0;
}
void displayData(vector<double> d){
for(int i = 0 ; i < d.size(); i++) {
cout << d[i] << ' ';
}
cout << '\n';
}
double minimum(vector<double> d) {
double mn = d[0]; //set equal to first element of array
//traverse the entire array
for(int i = 0; i < d.size(); i++) {
if(d[i] < mn) mn = d[i];
}
return mn;
}
double maximum(vector<double> d) {
double mx = d[0];
for(int i = 0; i < d.size(); i++) {
if(d[i] > mx) mx = d[i];}
return mx;
}
void swap(double &x, double &y) {
double temp = x; x = y; y = temp;
}
void bubblesort (vector<double> &data) {
int n = data.size();
int disorder = n;
while (disorder) {
disorder = 0;
for (int i = 1; i < n; i++) {
if (data[i] < data[i-1]) {
swap(data[i], data[i-1]);
disorder++;
}
}
n--;
}
}
double mean(vector<double> d) {
double mean = 0;
for(int i = 0; i < d.size(); i++) {
mean += d[i];
}
return mean/d.size();
}
double median(vector<double> d) { // in this function
bubblesort(d);
int sum = 0;
for(int i = 0; i < d.size(); i++) {
do{
int mini = minimum(d);
int maxi = maximum(d);
d.pop_back(mini);
d.pop_back(maxi);
}while(d.size() <= 2);
if(d.size() == 1) return d[0];
if(d.size() != 1) {
sum += d[i];
int median = sum/2;
return median;
}
}
}
I don't understand the intention of median() function. It seems very over-complicated. No need for loops or pop_back or anything like that.
Once you have sorted the vector, all you need is to check whether there are an even or odd number of elements. If it's odd, the median is the middle value. If it's even, the median is the average of the two middle values.
Well how would i say that. If d.size()%2 == 0 return the middle? How would i tell it to return the middle? In my function i was trying to continually remove the outsides until I'm left with either 1 element- odd. or 2 - even. And if it was 1 id return the remaining value which is median and if it was 2 i would take the average of those and return it as the median
Ok, I kind of understand your approach now.
Yes, to test for odd or even, use d.size() % 2. If it's zero, size is even otherwise it's odd.
Let's say there are five elements. The middle one is the 3rd. You should be able to count that out. But to calculate the middle element just do 5/2, as an integer division, that gives 2 (2.5 with the fractional part discarded). And because array numbering starts from zero, d[2] would be the middle value - the median.
Or if there are 10 elements, the middle pair are the 5th and 6th elements. Again 10/2 gives 5. d[5] is the 6th. The other element is just one position lower, d[4].
I don't know whether that makes sense. Sometimes I write stuff which is clear to me but confuses other people :)
Well i can't even get mine to work and your way seems more efficient so ill see if i can do that. And so i use d.size%2 to test if the answer we get is odd or even. Cause i thought we did like 10%2 = 5. The remainder is 0 so 10 is even. I was saying that we find the position of the median using int median = d.size/2 and then test to see if the size is odd or even so if d.size%2 != 0 we know its odd therefore the size /2 will be the median and return it otherwise we have to return the average of the two middle values.
Perhaps the naming of your variables is the cause of confusion for myself.
int median = d.size/2
This seems like a poor choice of name to me, and may be why we are talking a bit at cross-purposes. Also, the function itself is named median(), so it's probably better not to reuse the same name inside the function.
Here result is actually the median value. You might put when size is odd
ok yeah ill remember that from now on. And yes this is basically what i was trying to do but yeah the variables were a little confusing and i didn't know how to relate it to the way the array counts from 0 up instead of 1 up. But wait shouldn't the data type be int not double?
I thought we wanted to truncate the decimal part of it though? But i got it working perfectly. Ill try it with decimals and if it doesn't work its cause my data types int. Thank you!