I am trying to complete "Accelerated C++" exercise 3-2. I have tested, and the lower quartile and medians are being calculated correctly, but the upper quartile is not.
For example, assuming the input "50, 60, 70, 80, 90, 100", it will output the quartiles as 60, 75, and 80.
I have two issues I wish to address:
1) The upper quartile, in this case, should be 90. 2) How do I get my program to display the float, or double, version of my numbers? The more precise quartile for the lower one is 62.5, not 60.
/* Write a program to compute and print the quartiles(quarter of the
* numbers with the largest values) of a set of integers
* The first quartile (Q1) is defined as the middle number between the smallest number and the median of the data set.
* The second quartile (Q2) is the median of the data.
* The third quartile (Q3) is the middle value between the median and the highest value of the data set.*/
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::vector;
using std::endl;
using std::cout;
using std::cin;
int main() {
double x = 0;
double median, lowerQt, upperQt;
median = lowerQt = upperQt = 0;
vector<double> set;
typedef vector<double>::size_type vec_sz;
cout << "Enter integers followed by EOF: ";
while(cin >> x)
set.push_back(x);
vec_sz size = set.size();
if(size == 0) {
cout << "invalid" << endl;
return 1;
}
vec_sz mid = size / 2;
vec_sz lower = mid / 2;
vec_sz upper = size - mid;
sort(set.begin(), set.end());
median = size % 2 == 0 ? (set[mid] + set[mid - 1]) / 2 : set[mid];
lowerQt = mid % 2 == 0 ? (set[lower] + set[lower - 1]) / 2 : set[lower];
upperQt = mid % 2 == 0 ? (set[upper] + set[upper - 1]) / 2 : set[upper];
cout << lowerQt << endl << median << endl << upperQt;
}