Dinamic array

int n = 10;

double *t=new double[n];

for (int i = 0; i < n; ++i)
{
t[i]=1e2;

}

I'm trying to create a dinamic array. But, when I run this code not in the main programm, but in function, array is create, but it has only one double value t[0].
What should I do to make my programm work properly?
What value does t[1] have after you've made the array as above?
Last edited on
t[1], doesn`t have any values. This program should create array with lenght 10, but it creates only one cell. I can send you all progect, if it will help. Or I can send picture, shows resalts of debagging.
How do you know it only made one double object? Post a bit more code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "widget.h"
#include "ui_widget.h"
#include <QTextEdit>
#include <QFileDialog>
#include <QTextStream>
#include <QList>
#include <math.h>
#include <new>
using namespace std;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QObject::connect(ui->open_file,SIGNAL(clicked()),this,SLOT(openfile()));
    QObject::connect(ui->flip_button,SIGNAL(clicked()),this,SLOT(flip()));
}

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

void Widget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

bool test(QList< QList<double> > columns)
{
    int n = 10;


            double *t=new double[n];

            for (int i = 0; i < n; ++i)
                    {
                t[i]=1e2;

                    }

    return true;
}

void diag(QList< QList<double> > &columns)
{

    for(int k=0;k<columns.size();k++)
    {
        for(int i=1+k;i<columns.size();i++)
        {
            double div=columns[i][k]/columns[k][k];

            for(int j=k;j<(columns[0].size());j++)
            {
                 columns[i][j]=columns[i][j]-columns[k][j]*div;
                 if(fabs(columns[i][j])<fabs(-1e-10)) columns[i][j]=0;
            }

        }
    }

}

void Widget::openfile()
{
    QString filename=QFileDialog::getOpenFileName(this,tr("Open Matrix"), "/home", tr("Text Files (*.txt *.dat)"));
    ui->open_file_line->setText(filename);
}
void Widget::flip()
{
    QString filename=ui->open_file_line->text();
    QFile file(filename);
         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
             return;
         QTextStream in(&file);
         int j=0;
         int i=1;
         int z=ui->spinBox->value();
         QList<double> strings;
         QList< QList<double> > columns;
         QString k="digit:";
         while (!in.atEnd()) {
             j++;
             QString line = in.readLine();
            char * temp_char = new char [line.size()+1];
             QByteArray ba = line.toAscii();
                const char* c_str2 = ba.data();
                strcpy (temp_char, c_str2);
                char * n;
                 n=strtok(temp_char," ");
             columns << strings;
               while(n != NULL)
                {
                 k.append(" ");
                 k.append(n);
                 double numb=atof(n);
                 int str=j-1;
                 columns[str].append(numb);
                         i++;
                 n=strtok(NULL," ");
               }

            delete [] temp_char;
         }
            char bufer[250]={0};
            int summ=5*((i-1)/(j-1))+(j-2)*3;
            char bufer2[100]={0};
            char prec[1]={0};
         int count,count2,shift=0;
         int l=((i-1)/(j));
         sprintf(prec,"%d",z);
         QString result;
          diag(columns);
          int sum2=0;
          for(summ=0;summ<j;summ++)
          {
           for(sum2=0;sum2<l;sum2++)
              {
               QTextStream(&result)<<columns[summ][sum2];
                   if(sum2!=(l-1))
                       QTextStream(&result) <<' ';
              }
            QTextStream(&result) <<'\n';
          }
          if(test(columns)==true)
          {
              QTextStream(&result) <<'\n';
              QTextStream(&result) << "Matrica sovmestna!";
          }
          else
          {
              QTextStream(&result) <<'\n';
              QTextStream(&result) << "Matrica nesovmestna!";
          }
          ui->textEdit->setText(result);

         memset(bufer,'\0',sizeof(bufer));
         memset(bufer2,'\0',sizeof(bufer2));
         for(i=0;i<columns.size();i++)
         columns[i].clear();
         columns.clear();
         strings.clear();
         file.close();
}


I saw it in debugging mode. As you see in "void Widget::flip()" dinamic char array is creating perfectly. But when I try to create it in function, I have problems.

My program make matrix like this:
1 1 1 3
0 1 0 1
0 0 -2 -2
0 0 0 0
from matrix like this:
1 1 1 3
2 3 2 7
3 1 1 5
5 -1 -1 3
And put it to textEdit.
I`ve found the error!)
In debagging mode the array is displayed like one element t[0][0] instead of number of elements in array, like it happened with the QList of QList.

I`ve found elements of the array with the following construction:

1
2
3
4
5
6
7
8
int a1=t[0][0];
            int a2=t[1][0];
            int a3=t[2][0];
            int a4=t[3][0];
            int a5=t[0][1];
            int a6=t[1][1];
            int a7=t[2][1];
            int a8=t[3][1];
Topic archived. No new replies allowed.