passing 2d-arrays to a function with the dimensions

Hi,

I am writing a program where I want the function to take several 1d arrays and one 2d array as arguments. The sizes of these array also need to be arguments, as they are used in the function. I have read textbooks, and previous posts, watched tutorials and I cannot figure out how to do this. I have written some minimal examples:

This code works:

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 <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;


void testfunction(int M[6][6]);

int main (){


	int M[6][6]={{1,2,3,4,5,6},
			{2,3,4,5,6,7},
			{9,8,7,6,5,4},
			{3,4,5,6,7,8},
			{3,5,4,6,7,8},
			{2,3,4,7,6,5}, };

	testfunction(M);

    return 0;
}

void testfunction(int M[6][6]){
	for (int i=1; i<6; i++){
		for (int j=1; j<6; j++){
			if (i==j){
				M[i][j]=3;
				printf("i= %d j= %d M_ij= %d\n",i,j,M[i][j]);
			} else{
				M[i][j]=2*M[i][j];
				printf("i= %d j= %d M_ij= %d\n",i,j,M[i][j]);
			}
		}
	}
}


But, I want to include the dimensions of M as an argument to "testfunction", so I can use them in this function, as follows (this code does not work)

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
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;

void testfunction(double M[D*A][D*A], int A, int D);

int main (int argc, int A, int D, double M[D*A][D*A]){

	testfunction(M, A, D);

    return 0;
}

void testfunction(double (&M)[D*A][D*A], int A, int D){
	for (int i=1; i<(D*A); i++){
		for (int j=1; j<(D*A); j++){
			if (i==j){
				M[i][j]=A;
				printf("i= %d j= %d M_ij= %d\n",i,j,M[i][j]);
			} else{
				M[i][j]=D*M[i][j];
				printf("i= %d j= %d M_ij= %d\n",i,j,M[i][j]);
			}
		}
	}
}


I appreciate there is probably a lot wrong with this code, but I am very very stuck! How can I write this correctly, can someone please explain what I have done wrong?

Thanks
how about using std::vector?
it has a member fuction called size()


regarding the code you posted:
- you forgot to define M,A and D
- the function initialization and declaration need to have the same type, arguments and types of thes arguments M in line 7, but &M in line 16

Last edited on
Hello,

Regarding std::vector, I don't know much about it, but seems I could use it - however, I already know the size of M, so I thought it would be simple just to declare this? Is this not as simple as I thought?

Thanks for pointing out those two errors in my code. If I want to define A, D and M from the cmd line call, where do I declare them in the file? If I put them on line 6, there is still an error, but presumably I need to declare them outside of "main".

Can anyone recommend a good book/online reference, I am fed up of googling things and just getting the same questions asked on forums but no definitive/clear answers and explanations.

Thanks
you write something like

int A;

and once you read the users input with std::cin, you define the new value of your variable A=input;


for the basic stuff cplusplus.com has all the info you need. once you have to solve more difficult stuff, google is you best friend
Last edited on
Topic archived. No new replies allowed.