Hi all,

I'm working my way through this book and I'm having some trouble in an exercise. It asks to write a program to compute and print quartiles (the quarter of the numbers with largest values) of a set of integers.

Here's my attempt:

Code:

#include <iostream> //for cin cout endl
#include <vector> //for vectors
#include <algorithm> //for sort
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int num, count;
vector<int> vec;
vector<int>::size_type i, remainder;
cout << "Please enter integer numbers: " << endl;
while (cin >> num) {
vec.push_back(num);
}
if (vec.size()==0) {
cout << "Please rerun the program and enter integers: ";
return 1;
}
sort(vec.begin(), vec.end());
i=vec.size();
remainder=(vec.size()%4);
cout << "Program output:" << endl;
// print all the quartiles
while (i>remainder) {
cout << vec[i-1];
if (count%4==0)
cout << endl;
--i;
}
cout << endl;
// print any remaining numbers
for (;i>0;--i)
cout << vec[i-1] << "leftovers!";
return 0;
}

My problem is in the for loop. If I run the program and enter five integers, then just before this loop, DDD shows:

- i=1, remainder=1 before the loop

- goes to the for loop

- i=0, remainder=1 at the cout line

- DDD goes to stl_vector.h, and to:

Code:

operator[](size_type __n)
{ return *(this->_M_impl._M_start + __n); }

- then back to my program at the line where the for is, with i=0, remainder=1

- then to the cout line, but i is now 429467295

- then again to stl_vector.h, and then the for loop iterates, but without writing anything.

Can someone please explain where is my mistake in this program?

Sorry if the above is a bit confusing, I'm trying to provide as much as info as I can.

Many thanks for your help,

Spiros