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>
usingnamespace 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.
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.
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