pointer 2d array to function

Hi everybody ! i want multiplication of two dynamic 2d arrays ,but i don't know how ? plz help.my code is here.


# include<iostream>
# include<cstdlib>
# include<ctime>
void multiply(int **,int,int,int **,int ,int);
using namespace std;
int main()
{
int i,j,row1,row2,colum1,colum2;
int **array1,**array2; // pointr arrays

cin>>row1;
cin>>colum1;
cin>>row2;
cin>>colum2;


array1=new int *[row1]; // dynamic arrays
array2=new int *[row2];

for(i=0;i<colum1;i++)
{
array1[i]=new int[colum1];

}
for(j=0;j<colum2;j++)
{
array2[j]=new int[colum2];
}

// now filling arrays randomly;

for(i=0;i<row1;i++)
{
for(j=0;j<colum1;j++)
{
//srand(time(0));
array1[i][j]= rand()%51;
}
}

for(i=0;i<row2;i++)
{
for(j=0;j<colum2;j++)
{
//srand(time(0));
array2[i][j]= rand()%51;
}
}

if(colum1 != row2)
cout<<" multiplication impossible"<<endl;
else
multiply(array1,row1,colum1,array2,row2,colum2);
return 0;
}//main ends
void multiply(int **arra,int row,int colum,int **arr,int ro,int col)
{
int **array3;
array3= new int*[colum];
for(int o=0;o<colum;o++)
{
array3[o]= new int[ro];
}

for(int k=0;k<row;k++)
{
for(int y=0;y<col;y++)
{
????????

}
}
}
You've done pretty good so far.

One suggestion I can make quickly is to declare the product array in the main program, and then pass it into sub-routine "multiply".

Right now, you declare array "array3" inside the sub-routine and use that to hold the results of the matrix multiplication. But then how do you feed those results back into the main program?

If you declared "array3" in the main routine you could pass it into the sub-routine, do the matrix multiplication in the sub-routine, and then exit the sub-routine.

- - -

Also, you'll need a third control loop.
And each time you loop through a row of "arra" you'll need to initialize the value of the product array entry to 0.
Topic archived. No new replies allowed.