#include<iostream>
usingnamespace std;
void MatrixMulti(int A[][],int B[][],int AB[][]){//size of A is m*n && that of B
//is n*p .
for(int i=0;i<m;i++)
for(int j=0;j<p;j++){
int S=0;
for(int l=0;l<n;l++){
S+=A[i][l]*B[l][j];
}
AB[i][j]=S;
}
}
You have to pass the size of both dimensions. If the matrices are of different sizes then you'll have to pass the sizes of both matrices.
Hypothetically, this could be the least number of parameters you'd need to pass:
1 2 3
// Note: B_width can be inferred from A_height, while both sizes of AB can be inferred
// from A_width and B_height.
void mult(int **A, int A_width, int A_height, int **B, int B_height, int **AB);
By the way, despite array of pointers to arrays having seemingly nice syntax, they're actually a pain to deal with in code. Matrices like the ones you have there can be replaced by a single array of n*m elements.
After applying the changes you suggested i am still getting an error in the main()
{I am posting the modified code and the error . Could you pls help ! }
This code is not valid C++. Some compilers will accept it as an extension to the language, but when you try to compile that code in other compilers they will reject it.
Sorry, I have no idea how to pass variable-length arrays.
can't stress enough to just use 1-d pointers here.
or better, one-d vectors.
you are making this way too hard by trying to use 2-d arrays or ** constructs.
you can cast solid block to and from 1-d and reshape it to any 2-d that is valid.
that is, if you have 100 items in a row in ram, you can make that 1x100, 2x50, 3x3, ... anything where AxB <= 100 is valid. This does not work for ** though, because those are not solid blocks, unless the ** came from a solid block and is just an address of a [][] allocated thing.
I can show you that, but its messy and the wrong way to do things for your problem -- pointer magic like this has no place outside of 'look what I can do' foolin' around code.
I wrote a hybrid C/C++ pre STL matrix library using 1-d pointers in the early 90s. I can show you how to do that, but its not good code (fast, but not pretty); we have better tools now.