X
Xernoth
Hi,
This is my first post here, so please be gentle. I've been studying c+
+ by mostly using the book Accelerated C++ by Andrew Koenig and
Barbara E. Moo.
So far, I've been picking things up all right, and I've come to an
exercise 3-2 that requires the following:
3-2. Write a program to compute and print the quartiles (that is, the
quarter of the numbers with the largest values, the next highest
quarter, and so on) of a set of integers.
After researching what quartiles are, and using the method defined
here: http://www.statcan.ca/english/edu/power/ch12/range.htm (as from
what I've read, there are different methods used for computing lower
and upper quartiles) I've been able to write a console program that
seems to calculate correctly. However, I would like advice, from a
programming perspective, on what I could improve.
For those who haven't read the above book, it has a unique way of
introducing C++, as the first chapter jumps straight into the use of
strings, so a lot of basics, like use of functions and so on, have not
been covered as yet.
My code is as below:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::cout << "Enter a range of integers followed by 'end': ";
std::vector<double> integerSet;
int x;
while (std::cin >> x)
integerSet.push_back(x);
sort(integerSet.begin(), integerSet.end());
typedef std::vector<double>::size_type vector_Size;
vector_Size size = integerSet.size();
vector_Size mid = size / 2;
double median;
double lowerQuartile;
double upperQuartile;
median = size % 2 == 0 ? (integerSet[mid] + integerSet[mid-1]) / 2
: integerSet[mid];
vector_Size midlq = mid / 2;
lowerQuartile = mid % 2 == 0 ? (integerSet[midlq] +
integerSet[midlq - 1]) / 2
: integerSet[midlq];
vector_Size miduq = size - midlq;
upperQuartile = mid % 2 == 0 ? (integerSet[miduq] +
integerSet[miduq - 1]) / 2
: integerSet[miduq - 1];
int unsigned i = 0;
std::cout << "Ordered Data: ";
while (i != size)
{
std::cout << integerSet << " ";
i++;
}
std::cout << std::endl;
std::cout << "Median: " << median << std::endl;
std::cout << "Lower Quartile: " << lowerQuartile << std::endl;
std::cout << "Upper Quartile: " << upperQuartile << std::endl;
return 0;
}
Thanks
This is my first post here, so please be gentle. I've been studying c+
+ by mostly using the book Accelerated C++ by Andrew Koenig and
Barbara E. Moo.
So far, I've been picking things up all right, and I've come to an
exercise 3-2 that requires the following:
3-2. Write a program to compute and print the quartiles (that is, the
quarter of the numbers with the largest values, the next highest
quarter, and so on) of a set of integers.
After researching what quartiles are, and using the method defined
here: http://www.statcan.ca/english/edu/power/ch12/range.htm (as from
what I've read, there are different methods used for computing lower
and upper quartiles) I've been able to write a console program that
seems to calculate correctly. However, I would like advice, from a
programming perspective, on what I could improve.
For those who haven't read the above book, it has a unique way of
introducing C++, as the first chapter jumps straight into the use of
strings, so a lot of basics, like use of functions and so on, have not
been covered as yet.
My code is as below:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::cout << "Enter a range of integers followed by 'end': ";
std::vector<double> integerSet;
int x;
while (std::cin >> x)
integerSet.push_back(x);
sort(integerSet.begin(), integerSet.end());
typedef std::vector<double>::size_type vector_Size;
vector_Size size = integerSet.size();
vector_Size mid = size / 2;
double median;
double lowerQuartile;
double upperQuartile;
median = size % 2 == 0 ? (integerSet[mid] + integerSet[mid-1]) / 2
: integerSet[mid];
vector_Size midlq = mid / 2;
lowerQuartile = mid % 2 == 0 ? (integerSet[midlq] +
integerSet[midlq - 1]) / 2
: integerSet[midlq];
vector_Size miduq = size - midlq;
upperQuartile = mid % 2 == 0 ? (integerSet[miduq] +
integerSet[miduq - 1]) / 2
: integerSet[miduq - 1];
int unsigned i = 0;
std::cout << "Ordered Data: ";
while (i != size)
{
std::cout << integerSet << " ";
i++;
}
std::cout << std::endl;
std::cout << "Median: " << median << std::endl;
std::cout << "Lower Quartile: " << lowerQuartile << std::endl;
std::cout << "Upper Quartile: " << upperQuartile << std::endl;
return 0;
}
Thanks