Advertisement
Haval1

array * array

Jan 19th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include"stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
  8.  
  9.     cout << "Enter rows and columns for first matrix: ";
  10.     cin >> r1 >> c1;
  11.     cout << "Enter rows and columns for second matrix: ";
  12.     cin >> r2 >> c2;
  13.  
  14.     // If column of first matrix in not equal to row of second matrix,
  15.     // ask the user to enter the size of matrix again.
  16.     while (c1 != r2)
  17.     {
  18.         cout << "Error! column of first matrix not equal to row of second.";
  19.  
  20.         cout << "Enter rows and columns for first matrix: ";
  21.         cin >> r1 >> c1;
  22.  
  23.         cout << "Enter rows and columns for second matrix: ";
  24.         cin >> r2 >> c2;
  25.     }
  26.  
  27.     // Storing elements of first matrix.
  28.     cout << endl << "Enter elements of matrix 1:" << endl;
  29.     for (i = 0; i < r1; i++)
  30.         for (j = 0; j < c1; j++)
  31.         {
  32.             cout << "Enter element a" << i + 1 << j + 1 << " : ";
  33.             cin >> a[i][j];
  34.         }
  35.  
  36.     // Storing elements of second matrix.
  37.     cout << endl << "Enter elements of matrix 2:" << endl;
  38.     for (i = 0; i < r2; i++)
  39.         for (j = 0; j < c2; j++)
  40.         {
  41.             cout << "Enter element b" << i + 1 << j + 1 << " : ";
  42.             cin >> b[i][j];
  43.         }
  44.  
  45.     // Initializing elements of matrix mult to 0.
  46.     for (i = 0; i < r1; i++)
  47.         for (j = 0; j < c2; j++)
  48.         {
  49.             mult[i][j] = 0;
  50.         }
  51.  
  52.     // Multiplying matrix a and b and storing in array mult.
  53.     for (i = 0; i < r1; i++)
  54.         for (j = 0; j < c2; j++)
  55.             for (k = 0; k < c1; k++)
  56.             {
  57.                 mult[i][j] += a[i][k] * b[k][j];
  58.             }
  59.  
  60.     // Displaying the multiplication of two matrix.
  61.     cout << endl << "Output Matrix: " << endl;
  62.     for (i = 0; i < r1; i++) {
  63.         for (j = 0; j < c1; j++)
  64.         {
  65.             cout << " " << mult[i][j];
  66.         }
  67.         cout << endl;
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement