Arrays as arguments.

I am having trouble with a simple problem. I keep kicking myself for not know this but I believe I have made 3 arrays that are 10 by 10 in size and now I need to Store the matrix product of the first two arguments in the 3rd argument. But this is where I am stuck. Any pointers or help?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main() {

   int a[10][10];
   int b[10][10];
   int c[10][10];

return 0;

}



Thank you.

For example you can declare the function the following way
const int N = 10;

int ( * )[N] MatrixProduct( const int ( *a )[N], const int ( *b )[N], int ( *c )[N], int n );

To call the function use the following expression

MatrixProduct( a, b, c, N );
Last edited on
Does this take matrix products of the first two arguments (a,b) ans store them in the third argument (c)? And I am not sure what the const int N = 10; is for.

sorry for the quesitons.

Thank you for helping.
Const int N = 10 is for 10.:)
As for the function then the qualifier const means that elements of the array will not be changed. As the original matrices will not be changed the first two parameters have qualifier const. the third parameter will have the result of the product.
Can someone explain the syntax of the function declaration to me?
 
int ( * )[N] MatrixProduct( const int ( *a )[N], const int ( *b )[N], int ( *c )[N], int n );

Mainly just all of the ( * )[N]
Arrays passed by value as function arguments are implicitly converted to the pointer to their first element. The element of a multidimensional array is in turn an array with less dimension. So this record

int ( * )[N]

declares pointer to an one-dimensional array that is an element of a two-dimensional array.

Consider

1.

int a[N]; // element has type int

void f( int *a ); // pointer to the element of the array

2.

int a[N][N]; // element has type int[N];

void f( int ( *a )[N] ); // pointer to the element of the arrray.

(*)[N] is just a pointer to an array (to a row in this case). You could also pass your 2D arrays by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void MatrixProduct(const int(&a)[10][10], const int(&b)[10][10], int (&c)[10][10])
{
    for(int i = 0; i < 10; ++i)
        for(int j = 0; j < 10; ++j) {
            c[i][j] = 0;
            for(int k = 0; k < 10; ++k)
                c[i][j] += a[i][k] + b[k][j];
        }
}

int main() {

   int a[10][10];
   int b[10][10];
   int c[10][10];

   MatrixProduct(a, b, c);
}


But, in general, in C++, it's best to use a matrix library (such as boost.ublas or Eigen) or write your own matrix class, so that you can write c = a*b
Last edited on
Thank you all for this information. I am having trouble understanding how to write a function that takes 3 10x10 arrays as arguments and store the matrix product of the first two arguments in the 3rd argument. I think that is what you are doing @Cubbi, when I print c it give me the location of 'c' I think that is what your doing. please correct me if I am wrong.
Topic archived. No new replies allowed.