Thread: Help with Accelerated C++ exercise

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    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.

    Many thanks for your help,
    Spiros

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    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

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    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...

    thanks for your help!

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I have a giant block of enable features as well, however I didnt show them (so I wrote "output truncated"), because I just wanted to show the version and "-Wall" output. So I dont know what the problem is, when you say "I think I may know the reason". From this we can see our two versions arent the same, but I dont think such a warning wasnt in your version also. But of course its possible that is the reason (which I think would be extremely odd). However, again, something like using a variable without initializing it is certainly a big problem, so I dont think it was only introduced after 4.3.3.

    Anyways, your welcome. Who knows what other warnings (which can lead to problems/runtime errors) you arent seeing with these flags!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  2. Confused by template exercise
    By Sharke in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2009, 11:50 PM
  3. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM