How do i calculate the median of a vector

So i dont quite understand how to calculate the median of a vector can someone show me an example this is my code so far im having to use a struct for this program


#include<iostream>
#include<vector>
#include <algorithm>

using namespace std;

struct Vec{

int numb;
int max;
double sum;
int median;
};

int main()
{


Vec Max;
Vec Mode;
Vec Sum;
Vec Numb;
Vec Median;




vector<int>record;
Max.max=0;
Median.median=0;
Sum.sum=0;
Numb.numb=0;
cout<<"Please input some numbers\n";
while(cin>>Numb.numb)

{
record.push_back(Numb.numb);
cout<<"Please input some numbers\n";



for(int i=0; i<record.size();++i){

if(record[i]>Max.max)
Max.max=record[i];


// attempt in calculating the median(failed) Median.median=(record[i]/2);


Sum.sum+=record[i];

}

}

cout<<"Mean "<<Sum.sum/record.size()<<"\n";
sort(record.begin(),record.end());
cout<<"Max value: "<<Max.max<<'\n';
cout<<"Min "<<*min(record.begin(),record.end())<<"\n";
cout<<"Median "<<Median.median<<"\n";

}
The median is the (min + max) / 2, not some arbitrary record divided by two.
First, Zhuge has described the mean, not the median.

Second, http://www.cplusplus.com/articles/z13hAqkS/

Third, I suggest you organize your program more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include<vector>
#include <algorithm>

using namespace std;

struct Vec{
	int max;
	int min;
	int sum ;
	double mean ;
	double median;
};

int main()
{
	// initialize our stats:
	Vec stats = { 0, 0, 0, 0.0, 0.0} ;


	vector<int>record;
	cout<<"Please input some numbers\n";	
	
	int input ;
	while(cin>>input)
	{
		record.push_back(input);
		// cout<<"Please input some numbers\n";
	}

	// compute stats for the filled vector here, not in 
	// the while loop where it is stil being filled.
	// populate our stats struct with the computed
	// values.

	// hint for calcuating the median:
	if (record.size() %2 )
	{
		// record has an odd number of elements
	}else{
		// record has an even number of elements
	}

	
	cout << "Mean: " << stats.mean ;
	cout << "Max: " << stats.max ;
	cout << "Min: " << stats.min ;
	cout << "Median: " << stats.median ;
	cout << "Sum: " << stats.sum ;
}  


Notice that you only need one struct to hold all of the stats.
Last edited on
The median can only be calculated by sorting the values and picking the mid one. It can't be done via statistics gathering. Also the "Vec" name is pretty inappropriate. It's more like a 1d statistics class.
I was just initializing a struct so ill change Vec and how world you call for the middle element of the vector?
this is what i have so far and what zhuge said is working to find the median and what cire said
avarage/ mean is sum/total elements


#include<iostream>
#include<vector>
#include <algorithm>

using namespace std;

struct Stats{
int min;//initializing
int numb;//initializing
int max;//initializing
double sum;//initializing
int median;//initializing
vector<int>record;//initializing
};

int main()
{


Stats Max;//intializing Max to be able to call to Stats
Stats Sum; //intializing Sum to be able to call to Stats
Stats Numb;//intializing Numb to be able to call to Stats
Stats Median;//intializing Median to be able to call to Stats
Stats Record;//intializing Record to be able to call to Stats
Stats Min;//intializing Min to be able to call to Stats


int&Mx=Max.max;//reference to Max.max
int&Md=Median.median;//reference to Median.median
double&S=Sum.sum;//reference to Sum.sum
Max.max=0;//using int max from struct Vec
Median.median=0;//using int median from struct Vec
Sum.sum=0;//using double sum from struct Vec
Numb.numb=0;//using int numb from struct Vec
cout<<"Please input some numbers\n";//prompt user to start program


while(cin>>Numb.numb)

{
Record.record.push_back(Numb.numb);//stores inputs in the vector record
cout<<"Please input some numbers(terminate by entering ||)\n";



for(int i=0; i<Record.record.size();++i){

if(Record.record[i]>Max.max)
Max.max=Record.record[i];// gets the max value of the inputs

Sum.sum+=Record.record[i];//gets the sum of inputs and later used to get the mean

}

}

Min.min=*min(Record.record.begin(),Record.record.end());//using int min from struct Vec
int&Mn=Min.min;//referecing the min value and outputs it
Median.median=(Min.min+Max.max)/2;// gets the median of the inputs


sort(Record.record.begin(),Record.record.end());//sorts inputs from smallest to largest
cout<<"Mean value using struct: "<<Sum.sum/Record.record.size()<<"\n";// calculates and ouputs the mean
cout<<"Mean value using reference argument: "<<S/Record.record.size()<<"\n";//referecing the mean value and ouputs it
cout<<"Max value using reference argument: "<<Mx<<'\n';//referecing the max value
cout<<"Max value using struct: "<<Max.max<<'\n';//outputs max value
cout<<"Min value using struct: "<<Min.min<<"\n";//calculates and ouputs min value
cout<<"Min value using reference argument: "<<Mn<<"\n";//referencing min value and ouputs it
cout<<"Median value using struct: "<<Median.median<<"\n";//ouputs median value
cout<<"Median value using reference argument: "<<Md<<"\n";//referencing median value and ouputs it

return 0;
}
Last edited on
Topic archived. No new replies allowed.