Multidimensional arrays with unknown dimension as argument

Look at title: is this really impossible in c++? I can't pass a multidimensional array as an argument to a function if its dimensions are unknown?

What I want to do is something like this:

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
using namespace std;
#include <cstdlib> 
#include<iostream>
#include <complex>

void func(complex<double>**, int);

int main() {

	int N;
	
	cout << "State N: " << endl;
	cin >> N;

	complex<double> A[N][N];
	for(int i=0; i<N; i++) {
		for(int j=0; j<N; j++) {
			A[i][j]=complex<double>(3.0,3.0);
		}
	}
	
        // Here's the problem
	func(A, N);
	
	return 0;
}

void func(complex<double> **A, int N) {

	for(int i=0; i<N; i++) {
		for(int j=0; j<N; j++) {
			A[i][j] += complex<double> (1.0,1.0);
		}
	}
}

But no matter how I try to call func (e.g. func(A[0][0],N), func(&A[0],N), func(&&A,N) and so on) I can't get it to work.
Last edited on
The solution is called std::vector. You should really look into the STL.
Ok I will, thanks
closed account (D80DSL3A)
I got it to work two different ways.
First though, declaring the size of a static array at run time isn't supposed to work:
1
2
3
4
int N;	
	cout << "State N: " << endl;
	cin >> N;
	complex<double> A[N][N];

My compiler (VC2008) won't allow this but apparently some will.

The first solution uses arrays of fixed size (3x3 in example here):
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
#include <iostream>
#include <complex>
using namespace std;

const int N = 3;
void func(complex<double> A[][N], int N) {
	for(int i=0; i<N; i++) {
		for(int j=0; j<N; j++) {
			A[i][j] += complex<double> (1.0,1.0);
		}
	}
}

int main()
{	
	complex<double> A[N][N];
	for(int i=0; i<N; i++)
		for(int j=0; j<N; j++)
			A[i][j]=complex<double>(3.0,3.0);

	func(A, N);	

	cout << endl;
	return 0;
}

You want your array sizes to be determined at run time though.
Use dynamic memory allocation for this:
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
#include <iostream>
#include <complex>
using namespace std;


void func(complex<double>** A, int N) {
	for(int i=0; i<N; i++) {
		for(int j=0; j<N; j++) {
			A[i][j] += complex<double> (1.0,1.0);
		}
	}
}

int main()
{	
	int N;
	cout << "State N: " << endl;
	cin >> N;

	complex<double>** A = new complex<double>*[N];// allocate an array of pointers
	for(int i=0; i<N; i++)
	{
		A[i] = new complex<double>[N];// allocate an array to each pointer
		for(int j=0; j<N; j++)
			A[i][j]=complex<double>(3.0,3.0);
	}

	func(A, N);
	
	// release the memory when finished with it.
	for(int i=0; i<N; i++)
		delete [] A[i];
	delete [] A;

	cout << endl;
	return 0;
}

Other methods include using a one dimensional array then emulate 2D behavior with it, or use std::vector as hanst99 suggested.
this article is very usefull.Thanks fun2code, now i can do my homework.
How to enter a complex number in (a,bi) trong VS2008?
please help me.
Oh, I didn't realize more people had posted, thank you fun2code for more suggestions! I ended up following hanst99's solution. By the way I have no problem using static arrays and declaring size at runtime, I'm using g++... If it is possible to do so, why would some compilers refuse? I guess the answer is something to do with memory, that stuff is pretty fuzzy to me.

tuandt5: Unless the answer is compiler-dependent I think it's like this: say you want to make a complex number z=3-2i, do like this

1
2
complex<double> z;
z = complex<double> (3.0,-2.0);


Or

complex<double> z = complex<double> (3.0,-2.0);

Assuming you want the real and imaginary part to be double; if not replace the type within brackets.
Last edited on
Topic archived. No new replies allowed.