0

i tried to change the array in my program with vector,it was working well when i used array but when i change it, the program cant work properly. Qt says no error and it could run but when i press the generate or the calculate button the program seems to fail and i don't really know where the mistakes are

so here are my codes

mainwindow.h

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <math.h>
#include <vector>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int r, maxt, mint;
    std::vector<float> temp;
    float avg, sd;


private:
    Ui::MainWindow *ui;

public slots:
    void min();
    void max();
    void calc();
    void gen();


};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("Arfyan Rabbani");
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect (ui->Calculate,SIGNAL (clicked()),this,SLOT(calc()));
    connect (ui->Generate,SIGNAL (clicked()),this,SLOT(gen()));
    connect (ui->MaxSlider,SIGNAL (valueChanged(int)),this,SLOT(max()));
    connect (ui->MinSlider,SIGNAL (valueChanged(int)),this,SLOT(min()));

    maxt=37;
    mint=25;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::calc()
{
    float totaltemperature = 0;
    for (unsigned int i=0;i<24;i++)
      totaltemperature = totaltemperature + temp[i];
    avg = totaltemperature/24;
    QString average = QString ("Average = %1").arg(avg);
    ui->textBrowser_calc->setText(average);

    float stdev=0;
    for (unsigned int i=0;i<24;i++)
        stdev = stdev + pow((temp[i]-avg),2);
    sd = sqrt(stdev/24);
    QString stand = QString ("Standard Deviation = %1").arg(sd);
    ui->textBrowser_calc->append(stand);


}

void MainWindow::gen()
{
    r = maxt-mint+1;
    ui->textBrowser_data->setText("Temperature of The Room");
    for (unsigned int i=0;i<24;i++)
    {
        temp[i]= mint+rand()%r;
        QString t = QString ("Height %1 = %2").arg(i+1).arg(temp[i]);
        ui->textBrowser_data->append(t);
    }

}

void MainWindow::max()
{
    maxt = ui->MaxSlider->value();
}
void MainWindow::min()
{
    mint = ui->MinSlider->value();
}

i am currently learning how to use vectors, so i would be glad if someone could help me.

thanks!!

juanchopanza
  • 223,364
  • 34
  • 402
  • 480

2 Answers2

0

Your vector data member temp is default-initialized and therefore empty, yet you treat as if it weren't:

temp[i] // out of bounds access, undefined behaviour.

This triggers undefined behaviour.

Have a look at a good std::vector reference, particularly std::vector::resize, std::vector::push_back, std::vector::emplace_back, and the constructor taking an integer for initial size. For example, to instantiate temp to hold 24 zero-initialized elements,

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    temp(24)                 // init temp to hold 24 0.0f
{
  ....
}

After that you can safely index temp as you would an array of 24 floats.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

If you want to replace a fixed size array as in

struct Foo {
    int bar[10];
}

you should better use std::array:

struct Foo {
    std::array<int,10> bar;
}

If you really want to use vector, you have to change its size during runtime. Either during construction, by resizing it or adding elements to its back:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
temp(24)
{
    //...
}

OR:

temp.resize(24);
for (unsigned int i = 0; i<24; i++)
{
    temp[i] = mint + rand() % r;
    //...
}

OR (less efficient)

for (unsigned int i = 0; i<24; i++)
{
    temp.push_back(mint + rand() % r);
    //...
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102