0
#include <iostream>

#include <malloc.h>

using namespace std;


int main()

{
 int r,c,r2,c2;
 cout << "Enter the number of rows:- \n";
 cin >> r;
 cout << "\n" <<"Enter the number of columns: \n";
 cin >> c;
 int **mat;
 int **mat2;
 mat=(int**)malloc(sizeof(int)*r);  // allocating memory to a row
 for (int i=0;i<r;i++)
   {
      *(mat+i)=(int*)malloc(sizeof(int)*c);  /* allocating memory to 
                           columns in rows ( double pointers ) */
   }
cout << "\n\n" << "Please enter the 1st matrix of order:- " << r << 'x' 
     << c<< endl;

for (int i=0;i<r;i++)
    { for (int j=0;j<c;j++)
       {
           cin >> *(*(mat+i)+j);
           cout << endl;
       }
    }
cout << "Enter the number of rows(2nd):- \n";
cin >> r2;
cout << "\n" <<"Enter the number of columns(2nd): \n";
cin >> c2;
mat2=(int**)malloc(sizeof(int)*r2);
for (int i=0;i<r2;i++)
   {
     *(mat2+i)=(int*)malloc(sizeof(int)*c2);
   }
cout << "\n\n" << "Please enter the 2nd matrix of order:- " << r2 << 'x' 
     << c2 <<endl;

for (int i=0;i<r2;i++)
  {
    for (int j=0;j<c2;j++)
     {
        cin >> *(*(mat2+i)+j);
        cout << endl;
     }
  }
cout <<"\n\n\n" <<"The 1st matrix you entered is:- \n";

for (int i=0;i<r;i++)
  {  for (int j=0;j<c;j++)
     {
     cout << *(*(mat+i)+j) << '\t';
     }
     cout << endl;
  }
cout << "\n\n" << "The second matrix you entered is:- \n";

for (int i=0;i<r2;i++)
 {  for (int j=0;j<c2;j++)
       {
            cout << *(*(mat2+i)+j) << '\t';
       }
    cout << endl;
}

int **mat4;
mat4=(int**)malloc(sizeof(int)*r);

for (int i=0;i<r;i++)
   {
      *(mat4+i)=(int*)malloc(sizeof(int)*c2);
   }
if (c!=r2)
   {
      cout << "\n\n These two matrices cannot be multiplied as number of 
             columns("<< c<< ")of 1st matrix \nis not equal to number of 
             rows("<< r2<< ") of second matrix."; 
   }
if( c==r2)
 {
   for (int i=0;i<r;i++)
      {
       for (int z=0;z<c2;z++)
           { 
             for (int j1=0;j1<r2;j1++)
              {
               mat4[i][z]+= mat[i][j1] * mat2[j1][z]; /* logic to 
                                          multiply two matrices */
              }
           }

     }
  for (int i=0;i<r;i++)
      {
       for (int j=0;j<c2;j++)
          {
            cout <<  mat4[i][j] << '\t';
          } 
        cout << endl;
      }
  }
 return (1);
}

The problem occurs when I enter a 2x2 and a 2x3 matrix or any combination with c2=c+1. I get random large number in the output. Else if I enter any other combination of number of rows and columns, it works just fine. For example- If I put r2=3 and c2=4 , I'll get absurd values at column 1 and 3 no matter what input I do.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    why are you using `malloc`? – 463035818_is_not_an_ai Sep 28 '18 at 12:59
  • 4
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Sep 28 '18 at 12:59
  • 6
    `mat=(int**)malloc(sizeof(int)*r);` only works if pointers and ints are the same size. – 001 Sep 28 '18 at 13:00
  • 3
    I suggest you almost start over, by getting [a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn C++ properly. – Some programmer dude Sep 28 '18 at 13:00
  • On an unrelated note: Returning a non-zero value from `main` is in most environments considered an *error*. – Some programmer dude Sep 28 '18 at 13:01
  • I guess it should be `mat=(int**)malloc(sizeof( int*)*r);` – Yiğit Aras Tunalı Sep 28 '18 at 13:02
  • You're using C++. Use a 3rd party linear algebra library like BLAS (part of the Boost distribution). – Bathsheba Sep 28 '18 at 13:03
  • 2
    @YiğitArasTunalı Not really, `sizeof(int*)`. Or better yet `sizeof *mat`. Or even better `new int*[r]` instead of `malloc`. Or even better `std::vector> mat(r, std::vector(c));` – Some programmer dude Sep 28 '18 at 13:05
  • @YiğitArasTunalı That gives me an error. – Dipesh Malhotra Sep 28 '18 at 13:05
  • Sorry it was a typo corrected it now but as everyone is saying, unless you have a good reason to trying to re-invent the wheel I suggest that you use the 3rd party libraries since they will most likely be more efficent than the code you will implement – Yiğit Aras Tunalı Sep 28 '18 at 13:06

0 Answers0