in coding, how do you actually track down the bug??? example this code where we want to find the quartile....

ignore the "odd" part, not done on there yet, but the even part just successfully running but produce no result after input (0, random huge number, and another 0)

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
    vector<int> numberstore;
    int input;

    // real input
    while (cin >> input)

    // re-arranged
    sort(numberstore.begin(), numberstore.end());

    vector<double> quartilestore;
    // even amount
    if (numberstore.size() % 2 == 0)
        // compute
        int quadro = 4;
        for (int quartile = 1; quartile < quadro; ++quartile)
            double curr_quartile = (quartile/quadro) * numberstore.size();
            if (quartile == 2)
                curr_quartile = (curr_quartile + ((quartile/quadro) * numberstore.size() - 1)) / 2;

    // odd amount
    } else {


    // output
    for (vector<double>::size_type counter = 0; counter < quartilestore.size(); ++counter)
        cout << quartilestore[counter] << endl;

    return 0;
5 Years
Discussion Span
Last Post by mike_2000_17

First of all, you could save yourself some time on lines 6-10 by just using using namespace std; Also, I assume you are trying to find the interquartile range? In that case this can be done in very simple linear time. Think about this, there are three quartiles (the points separating the four quarters of the data) one of them is the first, one of them is the third, and the one in the middle is usually called the median. Once you have sorted your array, you do not care at all about the values in it. You can access the quartiles simply by using:

int median=numberstore[numberstore.size()/2];//it really is as simple as that
int firstQuartile=numberstore[numberstore.size()/4];//we want the element that is a quarter of the way in
int thirdQuartile=numberstore[(numberstore.size()/4)*3];//this is the most complicated one, but it can easily be derived from the one above

Hope that helped! (I also hope that I am not entirely wrong :P)


Listing individual items in a namespace you will use in a program and not "polluting" by writing for the entire namespace is a very acceptable method, and many would feel is the preferred method, though many of us use the "convenient" path of exposing the whole namespace.


in coding, how do you actually track down the bug??? example this code where we want to find the quartile....

Too late now, but before you write too much code, compile and test in smaller pieces.
Now, start outputting variables at key places to follow what the code is doing. Compare the values output with the values you calculate they should be.
In this case after the sort display the sorted values.
At line 28 display the value numberstore.size() .
In the for loop, output the values being computed.

Edited by WaltP: n/a

Votes + Comments
ha3 thnx, never thought 1 month leaving make me forget of this step xD


If you cant see where you have an error or a bug than go for debugging -:)
Debug your code, when you debug your code you see what your codes really do, and you see how funny error and bug you may have coded :-)

Edited by therockon7throw: n/a


If you are using an IDE to do your programming, then most decent IDEs have a debugger included in them. The basic way to use a debugger is to set a break-point (a point in the code at which the execution will be suspended). Then, you run the program until the break-point and then step through each line one by one. Between each step you can usually evaluate the variables (local variables, arrays, etc.) to see if they have the value they should have at that point. See your IDE's documentation or help or tutorials to find out how to do these things (set break-points, step through lines of code, and evaluate variables).

If you are not using an idea and don't have access to this type of debugger, then do as WaltP suggested: just print out all relevant information at all relevant points in your code, run the program and inspect the output to see if it makes sense and where it goes wrong.


thnx WaltP, ur post always been a help for me :)

btw, I'm using Code::Blocks, and never use debugger before, can anyone give a little insight on that? wiki just no help this time...

Edited by Vasthor: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.