//Before calculating the median, we must sort the 1/array. //1f the array is even... if (numbers.size() X 2 -- 0) //Get the mid index int mid - numbers.size() / 2; //Get the two numbers in the mid. (mid is the the right most middle number.) //Get mid anid mid- 1. double numl - numbers(mid]; double num2 - numbers(mid - 1]; T/Calculate the average of the two middle num return //the average.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question
double median()
//Before calculating the median, we must sort the
//array.
//If the array is even...
if (numbers.size() % 2 -- 0)
{
//Get the mid index
int mid - numbers.size()/ 2;
//Get the two numbers in the mid. (mid is the i
ndex of the right most middle number.)
//Get mid ar'd mid - 1
double numl - numbers [mid];
double num2 = numbers [mid - 1];
//Calculate the average of the two middle numbe
rs and return
//the average.
return average_mid;
else
//Determine the median index and return the
//number at the median.
int mid - numbers.size() /2;
return numbers [mid];
}
//Sorts the array in ascending order.
void BubbleSort)
for (int i - 0; i < numbers.size(); i++)
{
bool swapped false;
for (int j - 0; j< numbers.size() - i - 1; j++
{
if (numbers[j] > numbers[j + 1]
swap(numbers[), number
s[j + 11);
swapped - true;
if (swapped -- false)
return;
}
private:
//Used only in Bubble Sort. Ignore...
void swap(double& a, double& b)
{
double temp- a;
a - b;
b - temp;
};
int main)
VCreate a StatSet variable called "s".
//Add the number "55" to the StatSet "s".
//Calculate the median of the numbers.
return 0;
PIC CO
Transcribed Image Text:double median() //Before calculating the median, we must sort the //array. //If the array is even... if (numbers.size() % 2 -- 0) { //Get the mid index int mid - numbers.size()/ 2; //Get the two numbers in the mid. (mid is the i ndex of the right most middle number.) //Get mid ar'd mid - 1 double numl - numbers [mid]; double num2 = numbers [mid - 1]; //Calculate the average of the two middle numbe rs and return //the average. return average_mid; else //Determine the median index and return the //number at the median. int mid - numbers.size() /2; return numbers [mid]; } //Sorts the array in ascending order. void BubbleSort) for (int i - 0; i < numbers.size(); i++) { bool swapped false; for (int j - 0; j< numbers.size() - i - 1; j++ { if (numbers[j] > numbers[j + 1] swap(numbers[), number s[j + 11); swapped - true; if (swapped -- false) return; } private: //Used only in Bubble Sort. Ignore... void swap(double& a, double& b) { double temp- a; a - b; b - temp; }; int main) VCreate a StatSet variable called "s". //Add the number "55" to the StatSet "s". //Calculate the median of the numbers. return 0; PIC CO
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
class StatSet
{
private:
HCreate a vector of doubles that is called "numbers."
public:
//Nothing to do in the constructor.
StatSet()
}
// Add a new number to the set.
vòid add_num(double num)
{
}
double mean()
{
//Sum up the values and return the average.
double sum = 0;
//Write a loop that would iterate through the vector.
sum += numbers[i];
return sum / numbers.size();
}
double median()
//Before calculating the median, we must sort the
//array.
Transcribed Image Text:#include <iostream> #include <vector> using std::cout; using std::vector; class StatSet { private: HCreate a vector of doubles that is called "numbers." public: //Nothing to do in the constructor. StatSet() } // Add a new number to the set. vòid add_num(double num) { } double mean() { //Sum up the values and return the average. double sum = 0; //Write a loop that would iterate through the vector. sum += numbers[i]; return sum / numbers.size(); } double median() //Before calculating the median, we must sort the //array.
Expert Solution
steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning