# Help with Accelerated C++ exercise

• 01-24-2010
s_siouris
Help with Accelerated C++ exercise
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.

Spiros
• 01-24-2010
One thing I noticed is that you use "count" without initializing it, which you would see if you had "-Wall" enabled when you compile. Does it work when you initialize this first? I realize that this doesnt seem to be the obvious main problem, but a problem nonetheless. I ran the code with your suggested input and cannot reproduce the same result.
• 01-24-2010
s_siouris
yes, the problem was with "count" I didn't use it properly and was concentrating in the for loop to find my mistake rather than looking at other aspects of the program. I've made some modifications around "count" and it works fine now.

I am also compiling with g++ -Wall but didn't show any warnings for not initialising this.

Many thanks!
Spiros
• 01-24-2010
Quote:

I am also compiling with g++ -Wall but didn't show any warnings for not initialising this.
I find that very hard to believe. Maybe you built your own g++ or something, and somehow disabled this--highly unlikely. G++ does show you the warning. Unless maybe you have some archaic version of it (which I would think stillwould show the warning).
Code:

```jordan@jordan-laptop:~\$ g++ -v [output truncated] gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9) jordan@jordan-laptop:~\$ g++ test.cpp jordan@jordan-laptop:~\$ g++ -Wall test.cpp test.cpp: In function ‘int main()’: test.cpp:38: warning: ‘count’ may be used uninitialized in this function```
I always like to use "-Wall" and "-pedantic" when compiling.
• 01-24-2010
s_siouris
I think I may know the reason:
Code:

```spiros@lenore:~/programming/cpp.accel.mine\$ g++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)```
I tried it again with -Wall and still no errors or warnings. This is the standard gcc version for Ubuntu 9.04. I haven't upgraded to 9.10 due to sound card problems...