Skip to main content
Tweeted twitter.com/StackCodeReview/status/662920513459789824
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Mean, Median and Mode of a QVector

corrected use of markdown code-blocks
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
#include <QCoreApplication>
#include <QDebug>

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

    QVector<int> 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<int> ivect(vect);

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

        median =
        static_cast<double>((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<double>(sum) / vect.count();

    QSet<int> set;

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

    QMap<int, int> map;

    QSetIterator<int> 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<int, int> imap(map);

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

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

    return a.exec();
}
#include <QCoreApplication>
#include <QDebug>

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

QVector<int> 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<int> ivect(vect);

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

    median =
    static_cast<double>((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<double>(sum) / vect.count();

QSet<int> set;

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

QMap<int, int> map;

QSetIterator<int> 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<int, int> imap(map);

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

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

return a.exec();
}
#include <QCoreApplication>
#include <QDebug>

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

    QVector<int> 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<int> ivect(vect);

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

        median =
        static_cast<double>((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<double>(sum) / vect.count();

    QSet<int> set;

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

    QMap<int, int> map;

    QSetIterator<int> 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<int, int> imap(map);

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

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

    return a.exec();
}
deleted 3 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Qt - Containers - Mean, Median and Mode

Qt - Containers - Mean, Median and Mode

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 <QCoreApplication>
#include <QDebug>

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

QVector<int> 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<int> ivect(vect);

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

    median =
    static_cast<double>((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<double>(sum) / vect.count();

QSet<int> set;

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

QMap<int, int> map;

QSetIterator<int> 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<int, int> imap(map);

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

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

return a.exec();
}

Qt - Containers - Mean, Median and Mode

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 <QCoreApplication>
#include <QDebug>

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

QVector<int> 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<int> ivect(vect);

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

    median =
    static_cast<double>((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<double>(sum) / vect.count();

QSet<int> set;

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

QMap<int, int> map;

QSetIterator<int> 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<int, int> imap(map);

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

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

return a.exec();
}

Mean, Median and Mode

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 <QCoreApplication>
#include <QDebug>

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

QVector<int> 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<int> ivect(vect);

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

    median =
    static_cast<double>((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<double>(sum) / vect.count();

QSet<int> set;

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

QMap<int, int> map;

QSetIterator<int> 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<int, int> imap(map);

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

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

return a.exec();
}
edited tags
Link
Loading
added 18 characters in body
Source Link
Loading
Source Link
Loading