Question
In c++ If we define the median of a sequence as a number so that exactly as many elements come before it in the sequence
In c++
If we define the median of a sequence as a number so that exactly as many elements come before it in the sequence as come after it, fix the program in 4.6.3 so that it always prints out a median. Hint: A median need not be an element of the sequence.
4.6.3:
// compute mean and median temperatures
int main() {
vector temps; // temperatures
for (double temp; cin>>temp; ) // read into temp
temps.push_back(temp); // put temp into vector
// compute mean temperature:
double sum = 0;
for (int x : temps) sum += x;
cout << "Average temperature: " << sum/temps.size() << ' ';
// compute median temperature:
sort(temps); // sort temperatures
cout << "Median temperature: " << temps[temps.size()/2] << ' ';
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started