-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultip(DC).cpp
More file actions
110 lines (78 loc) · 2.34 KB
/
Copy pathMatrixMultip(DC).cpp
File metadata and controls
110 lines (78 loc) · 2.34 KB
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
103
104
105
106
107
108
109
110
#include<bits/stdc++.h>
using namespace std;
int **allocate(int n)
{
int **a = (int **)malloc(sizeof(int *)*n);
for(int i=0;i<n;i++)
{
a[i] = (int *)malloc(sizeof(int)*n);
}
return a;
}
int **ADD(int**c,int **a,int **b,int Row_c,int Col_c,int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
c[i+Row_c][j+Col_c] = a[i][j] + b[i][j];
}
}
return c;
}
int **MatrixMultiply(int **a,int **b,int Row_A,int Col_A,int Row_B,int Col_B,int size)
{
int **c = allocate(size);
if(size==1)
{
c[0][0] = a[Row_A][Col_A] * b[Row_B][Col_B];
}
else
{
int new_size = size/2;
ADD(c,MatrixMultiply(a,b,Row_A,Col_A,Row_B,Col_B,new_size),MatrixMultiply(a,b,Row_A,Col_A+new_size,Row_B+new_size,Col_B,new_size),0,0,new_size);
ADD(c,MatrixMultiply(a,b,Row_A,Col_A,Row_B,Col_B+new_size,new_size),MatrixMultiply(a,b,Row_A,Col_A,Row_B+new_size,Col_B+new_size,new_size),0,new_size,new_size);
ADD(c,MatrixMultiply(a,b,Row_A+new_size,Col_A,Row_B,Col_B,new_size),MatrixMultiply(a,b,Row_A+new_size,Col_A+new_size,Row_B+new_size,Col_B,new_size),new_size,0,new_size);
ADD(c,MatrixMultiply(a,b,Row_A+new_size,Col_A,Row_B,Col_B+new_size,new_size),MatrixMultiply(a,b,Row_A+new_size,Col_A+new_size,Row_B+new_size,Col_B+new_size,new_size),new_size,new_size,new_size);
}
return c;
}
int main()
{
int k ;
printf("Enter The value of K (n = 2 ^ k) : ");
cin >> k;
int n = pow(2,k);
printf("The value of n is : %d\n",n);
int **a,**b;
a = allocate(n);
b = allocate(n);
printf("Enter the Matrix 1 : \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("Enter value of a[%d][%d] : ",i+1,j+1);
cin >> a[i][j];
}
}
printf("Enter the Matrix 2 : \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("Enter value of b[%d][%d] : ",i+1,j+1);
cin >> b[i][j];
}
}
int**c = MatrixMultiply(a,b,0,0,0,0,n);
printf("The product of two matrices is : \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}