Click here to Skip to main content
15,888,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this program should print multiplication of 2 matrices and the result should be in a dynamic array but sometimes it gives me a wrong output

What I have tried:

int main() {
	
	int r1, c1,r2, c2;
	int** mult;
	int m1[15][15] , m2[15][15];
	cout << "enter the number of row=";
	cin >> r1;
	cout << "enter the number of column=";
	cin >> c1;
	cout << "enter the first matrix element=\n";
	for (int i = 0; i < r1; i++)
	{
		for (int j = 0; j < c1; j++)
		{
			cin >> m1[i][j];
		}
	}
	cout << "enter the number of row for second matrix=";
	cin >> r2;
	cout << "enter the number of column for second matrix=";
	cin >> c2;
	cout << "enter the second matrix element=\n";
	for (int i = 0; i < r1; i++)
	{
		for (int j = 0; j < c1; j++)
		{
			cin >> m2[i][j];
		}
	}

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

			for (int k = 0; k < r2; k++)
				mult[i][j] += (m1[i][k] * m2[k][j]);
		}
	}
	
	cout << "multiplication of the two matrices are :" << endl;
	for (int i = 0; i < r1; i++) {
		for (int j = 0; j < c2; j++) {
			cout << mult[i][j] << "  ";
		}

		cout << endl;
	}
	
}
Posted
Updated 12-Mar-22 0:13am

I see two problems in your code:

1.
you can't have four arbitrary values for row and column counts; two of these numbers must be equal in order for the matrix multiplication to be possible.

2.
for the second matrix, you ask for r2 and c2, and then use r1 and c1 for entering the matrix elements.
 
Share this answer
 
Comments
Member 15420232 12-Mar-22 1:20am    
I really didn't realize I was using r1 and c1 for the second matrix
thank you very much for your help.
CPallini 12-Mar-22 7:55am    
5.
I would start by replacing
C++
cout << "enter the number of row for second matrix=";
cin >> r2;
cout << "enter the number of column for second matrix=";
cin >> c2;
cout << "enter the second matrix element=\n";
for (int i = 0; i < r1; i++)
{
    for (int j = 0; j < c1; j++)
    {
        cin >> m2[i][j];
    }
}

with
C++
cout << "enter the number of row for second matrix=";
cin >> r2;
cout << "enter the number of column for second matrix=";
cin >> c2;
cout << "enter the second matrix element=\n";
for (int i = 0; i < r2; i++) // replace r1 by r2
{
    for (int j = 0; j < c2; j++) // replace c1 by c2
    {
        cin >> m2[i][j];
    }
}
 
Share this answer
 
Comments
CPallini 12-Mar-22 7:55am    
5
The C++ standard library is good and freely available, why don't use it?
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  size_t row[2], col[2];
  cout << "please enter the number of rows of the first matrix ";
  cin >> row[0];
  cout << "now enter the number of columns of the first matrix ";
  cin >> col[0];
  row[1] = col[0];
  cout << "finally enter the number of columns of the second  matrix ";
  cin >> col[1];

  vector < vector<int> > m[2];
  vector < vector<int> > p;


  for (size_t i = 0; i<2; ++i)
  {
    cout << "please enter M" << i << " items\n";
    for ( size_t r = 0; r < row[i]; ++r)
    {
      m[i].push_back(vector<int>{});
      for ( size_t c = 0; c < col[i]; ++c)
      {
        int v;
        cout << "M" << i <<"[" << r << "][" << c << "] = ";
        cin >> v;
        m[i][r].push_back(v);
      }
    }
  }


  for ( size_t r = 0; r < row[0]; ++r)
  {
    p.push_back( vector<int>{} );
    for ( size_t c = 0; c < col[1]; ++c)
    {
      p[r].push_back(0);
      for ( size_t k = 0; k < col[0]; ++k)
        p[r][c] += m[0][r][k] * m[1][k][c];

    }
  }

  cout << "product matrix items\n";
  for ( size_t r = 0; r < row[0]; ++r)
  {
    for ( size_t c = 0; c < col[1]; ++c)
    {
      cout << p[r][c] << " ";
    }
    cout << "\n";
  }

}
 
Share this answer
 
Comments
Patrice T 12-Mar-22 7:58am    
+5 too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900