Write function to add and multiply two matrices. The header of the
functions is as follow:
void addMatrix( int a[][COULMN_SIZE], int b[][COULMN_SIZE],
int c[][COULMN_SIZE], int rowSize )
c matrix will hold the result of adding
void multiplyMatrix( int a[][COULMN_SIZE],
intb [][COULMN_SIZE],
int c[][COULMN_SIZE], int rowSize )
c matrix will hold the result of multiplying
هدا سؤال حليت بس مابعرف اجمع الرحاء مساعدتي
الحل بالمرفق
#include <iostream>
using namespace std;
const int COLUMN_SIZE = 5;
/** The method for multiplying two matrices */
void multiplyMatrix(int a[] [COLUMN_SIZE], int b[] [COLUMN_SIZE], int result[] [COLUMN_SIZE], int rowSize)
{
for (int i = 0; i < COLUMN_SIZE; i++)
for (int j = 0; j < COLUMN_SIZE; j++)
for (int k = 0; k < COLUMN_SIZE; k++)
result [j] += a [k] * b[k] [j];
}
/** Print result */
void printResult(int m1[] [COLUMN_SIZE], int m2[] [COLUMN_SIZE], int m3[] [COLUMN_SIZE], char op, int rowSize)
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < COLUMN_SIZE; j++)
cout << " " << m1 [j];
if (i == COLUMN_SIZE / 2)
cout << " " << op << " ";
else
cout << " ";
for (int j = 0; j < COLUMN_SIZE; j++)
cout << " " << m2 [j];
if (i == COLUMN_SIZE / 2)
cout << " = ";
else
cout << " ";
for (int j = 0; j < COLUMN_SIZE; j++)
cout << " " << m3 [j];
cout << endl;
}
}
int main()
{
// Create two matrices as two dimensional arrays
int matrix1[5] [5];
int matrix2[5] [5];
int result[5] [5];
// Assign random values to matrix1 and matrix2
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
{
matrix1 [j] = rand();
matrix2 [j] = rand();
}
multiplyMatrix(matrix1, matrix2, result, 5);
cout << "The multiplication of the matrices is " << endl;
printResult(matrix1, matrix2, result, '*', 5);
system ("pause");
}