how would I multiply two 2d dynamic arrays and store them in another dynamically created array?

my code(mistaken) is:

#include<iostream>
using namespace std;

int main()
{
int r1,c1;
cout<<"Enter the row size for 2d array A\n";
cin>>r1;
cout<<"Enter the column size for 2d array A\n";
cin>>c1;

int ** A= new int * [r1];
for(int i=0;i<r1;i++)
{
A[i]= new int [c1];

}

for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
{
cin>> A[i][j];
}

int r2,c2;
cout<<"Enter the row size for 2d array B\n";
cin>>r2;
cout<<"Enter the column size for 2d array B\n";
cin>>c2;

int ** B= new int *[r2];
for(int i=0;i<r2;i++)
{
B[i]= new int [c2];
}

for(int i=0;i<r2;i++)
for(int j=0;j<c2;j++)
{
cin>> B[i][j];
}

int ** C= new int *[r1];
for(int i=0;i<r1;i++)
for(int j=0;j<r1;j++)
{
C[i][j]=0;
}


cout<<"AxB=C: "<<endl;
for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
{
int sum=0;
for(int k=0;k<r1;k++)
{
sum+= A[i][k]*B[k][j];
}
C[i][j]=sum;
}

for(int i=0;i<r1;i++)
{
for(int j=0;j<r1;j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}





return 0;
}
Last edited on
compare
1
2
3
4
5
6
7
8
9
10
11
	int** B = new int*[r2];
	for (int i = 0; i < r2; i++)
	{
		B[i] = new int[c2];
	}
	int** C = new int*[r1];
	for (int i = 0; i < r1; i++)
		for (int j = 0; j < r1; j++)
		{
			C[i][j] = 0;
		}
explain the differences.
Last edited on
Topic archived. No new replies allowed.