Problems in passing a multidimensional array to a function

Dear Sirs,
I have some problems in passing a 2D array to a function. I know that I have to define the dimensione of the second index of the array, but in my code it is defined in the main function.
This is the code for the matrix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double L, p_mut, mu;
  cout <<"The length of the sequence is: L= "; 
  cin >>L;
  cout<< endl;
  double totTypes = pow(2,L);
  int dimension = (int)totTypes;
(...)
 double intMatrix[dimension][dimension];//let's define the interaction matrix and initialize it to zero
   double interactStrength_temp = 0;
   for (int line=0; line<dimension; line++){
     for (int column=0; column<dimension; column++){
       infile2 >>interactStrength_temp;
       intMatrix[line][column]=interactStrength_temp;
     }
   }

This is the code for the function:

1
2
3
4
5
6
7
double hamiltonian (double array_occupancy[], int dim, int index, double matrix[][dimension], double mu, double totPop_average){
  double Hterm = 0;
  for (int i=0; i<dim; i++){
    Hterm += array_occupancy[i]*matrix[index][i];
  }
  return (1/totPop_average)*Hterm-mu*totPop_average;
}


The compiler says me that dimension is not declared in this scope and as a consequence it does not recognize the argument "matrix" as a proper matrix. Do you know how to solve this problem? I tried to define a constant but I cannot use the function pow(.,.) so I would have other problems.
Thank you very much for your help!
Yours faithfully,

Andrea Cairoli
Hi

Edit: first point has been removed

The second point see
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/forum/articles/7459/
Last edited on
Thank you for the references, I'll try to read them carefully. I did not understand the first point you're talking about because i know what is the size of a matrix. Did i make some mistake in the code?
Thank you very much.
Andrea
Hi

I think I should provide you same basic examples, if you have any question, or there is some thing unclear just post your question


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
// if you want to pass an array in this way the second dimension must be provided, known at compile time
int sum(int a[][3], int m){
	//do some things here
        int summ = 0 ;
	int n = 3 ;
	for(int i = 0 ; i < m ; i++)
		for(int j = 0 ; j <n ; j++)
			summ+= a[i][j];
	return summ;

}
// passing a n array which is created dynamically
int sum(int** a, int m, int n ){
	//do some things here
	int summ = 0 ;
		for(int i = 0 ; i < m ; i++)
			for(int j = 0 ; j <n ; j++)
				summ+= a[i][j];
		return summ;
}

int main()
{
        // if you use [dimension][dimension] to define an array 
        // dimension must be know at compile time
	int a[3][3] = {{3, 6, 7},{-5, 0,1},{-3,5,2}};

        // passing the array unto the function and getting the return value
        int i = sum( a, 3);
	cout<< i << endl ;

	int n = 3 ;// you may use cin>> n or whatever to assign a value to n
	int m = 3 ;//you may use cin>> m or whatever to assign a value to m

	// create an array dynamically the size n and m must not be know at compile time
        int** b = new int*[m] ;
	
        for(int i = 0 ; i < m ; i++)
		b[i] = new int[n] ;

	for(int i = 0 ; i < m ; i++)
		for(int j = 0 ; j <n ; j++)
			b[i][j] = ( i + j ) * i;
	
        // passing the array unto the function
        i = sum(b, 3, 3 );
	cout<< i << endl;

	// you must delete the array that you created dynamically using the operator new
	for(int i = 0 ; i < m ; i++)
			delete [] b[i];
	delete [] b;
	return 0;
}


Hope it helps
Last edited on
Thank you very much. I solved the problems!
Topic archived. No new replies allowed.