Must be possible: 3d arrays

Hi there. I've read a lot archived threads now but I can't find the answer.
I thought it was straight forward to make a double[][][] but apparently it isn't as I've found that it's these buggers which makes my program crash...

I'm making a math program and I really need that third array. Can someone please tell me how it's possible?

My guess would be that I need a new header or that I have to call them something else than double or long double...

It would be highly appreciated - thanks for your time.
Last edited on
closed account (Lv0f92yv)
It is possible, here is an example:


1
2
3
4
5
6
7
8
9
double a[5][6][5];

	for ( int i = 0; i < 5; i++ )
		for ( int j = 0; j < 6; j++ )
			for ( int k = 0; k < 5; k++ )
			{
				a[i][j][k] = j + k + i;
				cout << a[i][j][k] << "\n";
			}


Note that the condition is always less than the size allocated to the array.

Hope this helps.
Yes thank you. That's straight forward but my program crashes when having more than two arrays. I just wonder why it crashes.
Look just post your code - or we'll just end up playing some stupid guessing game.
Okay thanks. This is some of the code, I don't think it's necessary to post it all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void beregnH(double w[maxgrad], long double H[maxgrad][maxgrad][maxgrad], int kol, int mmax)
{
	double I[maxgrad][maxgrad];
	int i, j;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<mmax;j=j+1)
		{
			I[i][j]=0;
			if(i==j)
			{
				I[i][i]=1;
			}
		}
	}
	for(j=0;j<mmax;j=j+1)
	{
		for(i=0;i<mmax;i=i+1)
		{
			H[i][j][kol]=I[i][j]-2*w[i]*w[j];
		}
	}
}


So "H" will consist of a "kol" number of matrices. But my C++ don't like more than two arrays.
Last edited on
Chaospling wrote:
But my C++ don't like more than two arrays.

So what C++ (compiler) do you use?
An old one, C++ 6.0 but I though it still should be possible.
Get... MinGW... or VS 2010 Express. That program... is from 1998.

-Alba... tross
I'm sure it can handle more than 2 dimension arrays.
Post the whole code.
Hehe I know it's old. I just had problems with the newer ones and this is what's being used in my school. I don't think that I should post it all... It's quite long. But I can give you a little more.
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
void opgave4(double A[maxgrad][maxgrad], double b[maxgrad], int mmax, int nmax);
	long double H[maxgrad][maxgrad][maxgrad], R[maxgrad][maxgrad][maxgrad], Q[maxgrad][maxgrad][maxgrad];
	double l[maxgrad], x[maxgrad], reflx[maxgrad], w[maxgrad], Qt[maxgrad][maxgrad], Q1tb[maxgrad], R1Q1tb[maxgrad][maxgrad+1], xloes[maxgrad], I[maxgrad][maxgrad], tjekA[maxgrad][maxgrad];
	int kol;
	char svar;
	opgave4praesentation();
	do
	{
		cout<<"\n Oensker du at finde mindste kvadraters loesningen via grafisk forstaaelse, hvor du skal\n";
		cout<<" indtaste linien l, eller via metoden hvor ingen linie skal indtastes. Tast l hvis du vil\n";
		cout<<" bruge den grafiske forstaaelse: ";
		cin>>svar;
		if(svar=='l')
		{
			indtastl(A, l, mmax);
			for(kol=0;kol<nmax;kol=kol+1)
			{
				beregnx(A, R, x, kol, mmax);
				beregnreflx(x, l, reflx, mmax);
				beregnwl(x, reflx, w, mmax);
				beregnH(w, H, kol, mmax);
				beregnR(H, A, R, kol, mmax, nmax);
				beregnQ(H, Q, kol, mmax, nmax);
			}
		}
		else
		{
			for(kol=0;kol<nmax;kol=kol+1)
			{
				beregnx(A, R, x, kol, mmax);
				beregnw(x, w, kol, mmax);
				beregnH(w, H, kol, mmax);
				beregnR(H, A, R, kol, mmax, nmax);
				beregnQ(H, Q, kol, mmax, nmax);
			}
		}
		transponermatrix1(Q, Qt, mmax, nmax);
		beregnQ1tb(Qt, b, Q1tb, mmax, nmax);
		totalmatrix1(R, Q1tb, R1Q1tb, mmax, nmax);
	
		cout<<"\n\n Vi har nu totalmatricen:\n\n";
		udskrivtotalmatrix(R1Q1tb, nmax);
	
		backwardssubstitution(R1Q1tb, xloes, nmax);
	
		cout<<"\n\n Her er mindste kvadraters loesningen:\n";
		udskrivloesning(xloes, nmax);
		tjekloesning1(Qt, Q, I, mmax, nmax);

		cout<<"\n Da Q er en ortonormal matrix burde Q transponeret ganget med Q meget gerne give enhedsmatricen.\n";
		cout<<" Her er hvad Qt*Q bliver:\n\n";
		udskrivmatrix2(I, nmax);
		tjekloesningA1(Q, R, tjekA, mmax, nmax);

		cout<<"\n Vi har jo at A=Q*R. Saa her har du foerst din indtastet matrix A og efterfoelgende matrix A beregnet\n";
		cout<<" via Q*R som altsaa meget gerne skulle ligne den foerste.\n\n";
		udskrivmatrix(A, mmax, nmax);
		cout<<"\n\n";
		udskrivmatrix(tjekA, mmax, nmax);

		cout<<"\n Oensker du at finde mindste kvadraters loesningen igen ved brug af Householder reflections? ";
		cin>>svar;
	}
	while(svar=='j');
}


void opgave4praesentation()
{

}


void indtastl(double A[maxgrad][maxgrad], double v[maxgrad], int mmax)
{
	double temp, temp1;
	int i;
	char svar;
	cout<<"\n Indtast elementerne for den faste linie/vektor l, som der skal reflekteres om.\n";
	do
	{
		do
		{
			for(i=0;i<mmax;i=i+1)
			{
				cout<<"\n Indtast element nr."<<i+1<<" i vektoren her: ";
				cin>>v[i];
			}
			temp=A[0][0]/v[0];
			i=1;
			do
			{
				temp1=A[i][0]/v[i];
				i=i+1;
			}
			while(temp==temp1&&i<mmax);
			if(temp==temp1)
			{
				cout<<"\n Den indtastet vektor har samme retning som den foerste kolonne i matrix A, saa indtast\n";
				cout<<" en ny vektor\n.";
			}
		}
		while(temp==temp1);
		cout<<"\n Dette er den indtastet linie der skal reflekteres om:\n\n";
		udskrivvektor(v, mmax);
		cout<<"\n Vil du indtaste elementerne om? ";
		cin>>svar;
	}
	while(svar=='j');
}


void beregnreflx(double x[maxgrad], double l[maxgrad], double reflx[maxgrad], int mmax)
{
	double temp, temp1, temp2, projx[maxgrad];
	int i;
	temp1=0;
	temp2=0;
	for(i=0;i<mmax;i=i+1)
	{
		temp1=x[i]*l[i]+temp1;
		temp2=pow(l[i],2)+temp2;
	}
	temp=temp1/temp2;
	for(i=0;i<mmax;i=i+1)
	{
		projx[i]=temp*l[i];
		reflx[i]=2*projx[i]-x[i];
	}
}


void beregnwl(double x[maxgrad], double reflx[maxgrad], double w[maxgrad], int mmax)
{
	double temp, temp1, temp2;
	int i;
	temp=0;
	for(i=0;i<mmax;i=i+1)
	{
		temp2=x[i]-reflx[i];
		temp=pow(temp2,2)+temp;
	}
	temp1=1/sqrt(temp);
	for(i=0;i<mmax;i=i+1)
	{
		w[i]=temp1*(x[i]-reflx[i]);
	}
}


void beregnH(double w[maxgrad], long double H[maxgrad][maxgrad][maxgrad], int kol, int mmax)
{
	double I[maxgrad][maxgrad];
	int i, j;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<mmax;j=j+1)
		{
			I[i][j]=0;
			if(i==j)
			{
				I[i][i]=1;
			}
		}
	}
	for(j=0;j<mmax;j=j+1)
	{
		for(i=0;i<mmax;i=i+1)
		{
			H[i][j][kol]=I[i][j]-2*w[i]*w[j];
		}
	}
}


void beregnR(long double H[maxgrad][maxgrad][maxgrad], double A[maxgrad][maxgrad], long double R[maxgrad][maxgrad][maxgrad], int kol, int mmax, int nmax)
{
	int i, j, k;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			R[i][j][kol]=0;
		}
	}
	if(kol==0)
	{
		for(k=0;k<nmax;k=k+1)
		{
			for(i=0;i<mmax;i=i+1)
			{
				for(j=0;j<mmax;j=j+1)
				{
					R[i][k][kol]=H[i][j][kol]*A[j][k]+R[i][k][kol];
				}
			}
		}
	}
	else
	{
		for(k=0;k<nmax;k=k+1)
		{
			for(i=0;i<mmax;i=i+1)
			{
				for(j=0;j<mmax;j=j+1)
				{
					R[i][k][kol]=H[i][j][kol]*R[j][k][kol-1]+R[i][k][kol];
				}
			}
		}
	}
}


void beregnQ(long double H[maxgrad][maxgrad][maxgrad], long double Q[maxgrad][maxgrad][maxgrad], int kol, int mmax, int nmax)
{
	int i, j, k;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<mmax;j=j+1)
		{
			Q[i][j][kol]=0;
		}
	}
	if(kol==0)
	{
		for(i=0;i<mmax;i=i+1)
		{
			for(j=0;j<mmax;j=j+1)
			{
				Q[i][j][kol]=H[i][j][kol];
			}
		}
	}
	else
	{
		for(k=0;k<mmax;k=k+1)
		{
			for(i=0;i<mmax;i=i+1)
			{
				for(j=0;j<mmax;j=j+1)
				{
					Q[i][k][kol]=Q[i][j][kol-1]*H[j][k][kol]+Q[i][k][kol];
				}
			}
		}
	}
}


void beregnx(double A[maxgrad][maxgrad], long double R[maxgrad][maxgrad][maxgrad], double x[maxgrad], int kol, int mmax)
{
	int i;
	if(kol==0)
	{
		for(i=0;i<mmax;i=i+1)
		{
			x[i]=A[i][0];
		}
	}
	else
	{
		for(i=0;i<mmax;i=i+1)
		{
				x[i]=R[i][kol][kol-1];
		}
	}
}


void beregnw(double x[maxgrad], double w[maxgrad], int kol, int mmax)
{
	double yk, xk, sk, xkyk;
	int i;
	yk=0;
	for(i=kol;i<mmax;i=i+1)
	{
		yk=pow(x[i],2)+yk;
	}
	if(x[kol]<0)
	{
		yk=sqrt(yk);
		sk=yk;
		xk=-x[kol];
	}
	if(x[kol]>0)
	{
		yk=-sqrt(yk);
		sk=-yk;
		xk=x[kol];
	}
	xkyk=x[kol]-yk;
	w[kol]=xkyk;
	if(kol>0)
	{
		for(i=0;i<kol;i=i+1)
		{
			w[i]=0;
		}
	}
	if(kol<mmax)
	{
		for(i=kol+1;i<mmax;i=i+1)
		{
			w[i]=x[i];
		}
	}
	for(i=0;i<mmax;i=i+1)
	{
		w[i]=w[i]/sqrt(2*sk*(sk+xk));
	}
}


void transponermatrix1(long double A[maxgrad][maxgrad][maxgrad], double At[maxgrad][maxgrad], int mmax, int nmax)
{
	int i, j;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			At[j][i]=A[i][j][nmax-1];
		}
	}
}


void beregnQ1tb(double At[maxgrad][maxgrad], double b[maxgrad], double Atb[maxgrad], int mmax, int nmax)
{
	int i, j;
	for(i=0;i<nmax;i=i+1)
	{
		Atb[i]=0;
	}
	for(i=0;i<nmax;i=i+1)
	{
		for(j=0;j<mmax;j=j+1)
		{
			Atb[i]=At[i][j]*b[j]+Atb[i];
		}
	}
}


void totalmatrix1(long double AtA[maxgrad][maxgrad][maxgrad], double Atb[maxgrad], double R1Q1tb[maxgrad][maxgrad+1], int mmax, int nmax)
{
	int i, j;
	for(i=0;i<nmax;i=i+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			R1Q1tb[i][j]=AtA[i][j][nmax-1];
		}
	}
	for(i=0;i<nmax;i=i+1)
	{
		R1Q1tb[i][nmax]=Atb[i];
	}
}


void tjekloesning1(double Qt[maxgrad][maxgrad], long double Q[maxgrad][maxgrad][maxgrad], double I[maxgrad][maxgrad], int mmax, int nmax)
{
	double temp;
	int i, j, n;
	for(i=0;i<nmax;i=i+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			I[i][j]=0;
		}
	}
	for(n=0;n<nmax;n=n+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			temp=0;
			for(i=0;i<mmax;i=i+1)
			{
				temp=Qt[j][i]*Q[i][n][nmax-1]+temp;
			}
			I[i][n]=temp;
		}
	}
}


void tjekloesningA1(long double Q[maxgrad][maxgrad][maxgrad], long double R[maxgrad][maxgrad][maxgrad], double tjekA[maxgrad][maxgrad], int mmax, int nmax)
{
	int i, j, n;
	for(i=0;i<mmax;i=i+1)
	{
		for(j=0;j<nmax;j=j+1)
		{
			tjekA[i][j]=0;
		}
	}
	for(n=0;n<nmax;n=n+1)
	{
		for(i=0;i<mmax;i=i+1)
		{
			for(j=0;j<nmax;j=j+1)
			{
				tjekA[i][n]=Q[i][j][nmax-1]*R[j][n][nmax-1]+tjekA[i][n];
			}
		}
	}
}
Topic archived. No new replies allowed.