Mean, Median and Mode of a QVector I would like some help condensing this code down to size. I am trying to utilize Qt's container classes as efficiently as possible. I do not believe this code reflects my objective.

#include &lt;QCoreApplication&gt;
#include &lt;QDebug&gt;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QVector&lt;int&gt; vect;

    vect.append(0);
    vect.append(0);
    vect.append(0);
    vect.append(0);
    vect.append(1);
    vect.append(1);
    vect.append(2);
    vect.append(1);

    QVectorIterator&lt;int&gt; ivect(vect);

    double median = 0;
    if(vect.count() % 2 == 0){

        median =
        static_cast&lt;double&gt;((vect[vect.count() / 2 - 1] + vect[vect.count() / 2])) / 2;
    }
    else{

        median = vect[vect.count() / 2];
    }

    int sum = 0;
    while(ivect.hasNext()){

        sum += ivect.next();
    }

    double mean = static_cast&lt;double&gt;(sum) / vect.count();

    QSet&lt;int&gt; set;

    ivect.toFront();
    while(ivect.hasNext())
    {
        set &lt;&lt; ivect.next();
    }

    QMap&lt;int, int&gt; map;

    QSetIterator&lt;int&gt; iset(set);

    ivect.toFront();
    while(iset.hasNext())
    {
        int count = 0;
        while(ivect.hasNext())
        {
            if(iset.peekNext() == ivect.next())
            {
                count++;
            }
        }
        ivect.toFront();
        map.insert(iset.next(), count);
    }

    QMapIterator&lt;int, int&gt; imap(map);

    int maxValue = 0;
    QString mode;
    while(imap.hasNext())
    {
        imap.next();
        if(imap.value() &gt; maxValue)
        {
           maxValue = imap.value();
           mode = QString::number(imap.key());
        }
        else if(imap.value() == maxValue)
        {
            mode = "NULL";
        }
    }

    qDebug() &lt;&lt; set;
    qDebug() &lt;&lt; map;
    qDebug() &lt;&lt; "Mean:" &lt;&lt; mean;
    qDebug() &lt;&lt; "Median:" &lt;&lt; median;
    qDebug() &lt;&lt; "Mode frequency:" &lt;&lt; maxValue;
    qDebug() &lt;&lt; "Mode:" &lt;&lt; mode;

    return a.exec();
}
Sorted vs Unsorted

Your QVector sequence contains an unsorted sequence of values.  Your median calculation has a precondition that your sequence is sorted.  Consider the sequence 1, 0, 1, what is the median?

Small, Focused, and Testable Functions

Prefer writing functions that employ the single responsibility principle.  Splitting out median and testing it with different sequence types would have pointed out a problem. How about calculating the mean of an empty container? Is division by zero a problem?

Standard Algorithms

Being able to understand when you should use standard algorithms and applying them is very important.  Mean can be written using std::accumulate().  Median can be written in terms of std::nth_element().  Mode can be written using boost::bimap (or utilize std::unordered_map+std::multimap) and std::upper_bound() with std::transform() to return the modes of a sequence.  Not all sequences are uni-modal.