5

I'm writing a program which takes the elements of two different matrices, then it will multiply them and next it will save them in a multidimensional array. But it only works for square matrix. (Visual Studio 2013 gives no error.) I'll be pleased if you help me edit this code in order to multiply every kind of matrix.

    int a, b, c, d;
    int Mat1[10][10];
    int Mat2[10][10];
    int Mat3[10][10];
    Back1:
    cout << endl << "Enter the number of rows in Matrix 1 : ";
    cin >> a;
    cout << endl << "Enter the number of columns in Matrix 1 : ";
    cin >> b;
    cout << endl << "Enter the number of rows in Matrix 2 : ";
    cin >> c;
    cout << endl << "Enter the number of column in Matrix 2 : ";
    cin >> d;
    if (b != c) {
        cout << endl << "******************************************"
             << "\nThis type of Matrix can't be multiplied . " << endl;
        goto Back1;
    }
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            cout << endl << "(MAT 1 ) \n\nEnter the Element of row " << i + 1
                 << " column " << j + 1 << " : ";
            cin >> Mat1[i][j];
        }
    }
    for (int i = 0; i < c; i++) {
        for (int j = 0; j < d; j++) {
            cout << endl << "(MAT 2 ) \n\nEnter the Element of row " << i + 1
                 << " column " << j + 1 << " : ";
            cin >> Mat2[i][j];
        }
    }
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < d; j++) {
            Mat3[i][j] = Mat1[i][j] * Mat1[i][j];
        }
    }
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < d; j++) {
            cout << setw(4) << Mat3[i][j] << setw(4);
        }
        cout << endl;
    }
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
Erfan
  • 169
  • 1
  • 1
  • 11
  • If you're referring to the normal mathematical definition of matrix multiplication, then your code is wrong. You need at least one more inner `for` loop to sum up element products. – Drew McGowen Aug 11 '14 at 18:42
  • 1
    You may indent/format your code, and create sub-functions to ease readability. – Jarod42 Aug 11 '14 at 18:46
  • 1
    Please indent your code, using spaces and not tabs, and usually using an indent level of 4 (but almost any indent will be OK as long as it is consistent). Does VS 2010 support VLAs — variable length arrays? If not, you'll have to simulate them, which is modestly painful. – Jonathan Leffler Aug 11 '14 at 18:47
  • 1
    Please, there is no need for the `goto`. Just use a `do-while`: `bool matrixOK=false; do { ... if (b == c) matrixOK=true; } while (!matrixOK);` – PaulMcKenzie Aug 11 '14 at 18:49

1 Answers1

8

Your code for multiplication of the matrices is wrong. Instead of:

for (int i = 0; i < a; i++)
{
   for (int j = 0; j < d; j++)
   {
      Mat3[i][j] = Mat1[i][j] * Mat1[i][j];
   }
}

You need:

for (int i = 0; i < a; i++)
{
   for (int j = 0; j < d; j++)
   {
      Mat3[i][j] = 0;
      for (int k = 0; k < c; k++)
      {
         Mat3[i][j] += Mat1[i][k] * Mat2[k][j];
      }
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @PaulMcKenzie What if I want the user try again if the entered numbers are not matched ? – Erfan Aug 11 '14 at 19:57