-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion1.cpp
102 lines (91 loc) · 2.25 KB
/
question1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Name: Maleek Hussain Ali
Roll number: 22i-1526
Class: SE-B
*/
#include<iostream>
using namespace std;
void checkMatrix(int, int);
void inputValidation(int&);
int main()
{
int row1, column1, row2, column2, matrix1[10][10], matrix2[10][10], r = 0, result[10][10];
cout << "\nEnter rows of first matrix: ";
cin >> row1;
inputValidation(row1);
cout << "\nEnter columns of first matrix: ";
cin >> column1;
inputValidation(column1);
cout << "\nEnter rows of second matrix: ";
cin >> row2;
inputValidation(row2);
cout << "\nEnter columns of second matrix: ";
cin >> column2;
inputValidation(column2);
checkMatrix(column1, row2);
//filling first matrix
for(int i = 0; i < row1; i++)
{
for(int j = 0; j < column1; j++)
{
cout << "\nEnter value of a" << (i + 1) << "" << (j + 1) << ": ";
cin >> matrix1[i][j];
}
}
//filling second matrix
for(int i = 0; i < row2; i++)
{
for(int j = 0; j < column2; j++)
{
cout << "\nEnter value of b" << (i + 1) << "" << (j + 1) << ": ";
cin >> matrix2[i][j];
}
}
//multiplication of matrix
for(int i = 0; i < row1; i++)
{
for(int j = 0; j < column2; j++)
{
for(int k = 0; k < row2; k++)
{
r += matrix1[i][k] * matrix2[k][j];
}
result[i][j] = r;
r = 0;
}
}
cout << "\nProduct of given two matrices is displayed below: ";
cout << endl << endl;
//result matrix output
for(int i = 0; i < row1; i++)
{
cout << "|";
for(int j = 0; j < column2; j++)
{
cout << result[i][j];
if(j != (column2 - 1))
cout << "\t";
}
cout << "|" << endl;
}
return 0;
}
void checkMatrix(int row, int column)
{
if(row != column){
cout << "\nError!!!!!!!!!!!!!!!!!!";
exit(0);}
}
void inputValidation(int& rangeOfMatrix)
{
while(rangeOfMatrix > 10)
{
cout << "Please input number less than or equal to 10: ";
cin >> rangeOfMatrix;
}
while(rangeOfMatrix < 1)
{
cout << "Please enter value greater than zero: ";
cin >> rangeOfMatrix;
}
}