-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMultiplication2D.cpp
executable file
·80 lines (67 loc) · 1.99 KB
/
MatrixMultiplication2D.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
//============================================================================
// Name : MatrixMultiplication.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <ctime> //library for current time
#include <cstdlib> //library for random number
using namespace std;
void createMatrix(int *newMatrix,int n){
for(int i=0;i<n*n;i++){
int num=rand()%100+1;
newMatrix[i] = num;
}
}
void printMatrix(const int* matrix, int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
//int num=matrix[i*n+j];
cout << matrix[i*n+j] << " ";
}
cout << endl;
}
}
//classical way to multiply matrix
void classicalMultiply(const int* firstMatrix, const int* secondMatrix,int* newMatrix, int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
int *temp=newMatrix+i*n+j;
*temp=*temp+*(firstMatrix+i*n+k)*(*(secondMatrix+k*n+j));
}
}
}
}
int main() {
int n;
int *firstMatrix, *secondMatrix, *multipliedMatrix;
long timeBefore, timeAfter;
cout << "Please enter the size of the matrix: ";
cin >> n;
cout << "First matrix: "<<endl;
firstMatrix=new int[n*n];
createMatrix(firstMatrix,n);
//printMatrix((const int*)firstMatrix,n);
cout << "Second matrix: "<<endl;
secondMatrix=new int[n*n];
createMatrix(secondMatrix,n);
//printMatrix((const int*)secondMatrix,n);
multipliedMatrix=new int[n*n];
for(int i=0;i<n*n;i++){
multipliedMatrix[i]=0;
}
cout<<"begin multiplying:"<<endl;
timeBefore=time(NULL);
classicalMultiply((const int*)firstMatrix,(const int*)secondMatrix,multipliedMatrix,n);
timeAfter=time(NULL);
cout << "After multiply: " << endl;
//printMatrix((const int*)multipliedMatrix,n);
cout<< "It took " << (timeAfter-timeBefore) << " seconds for the classical multiplication."<<endl;
delete[] firstMatrix;
delete[] secondMatrix;
delete[] multipliedMatrix;
return 0;
}