Skip to content

Instantly share code, notes, and snippets.

@timurbakibayev
Created November 5, 2020 15:04
Show Gist options
  • Save timurbakibayev/cf99b596e53d13af889ea4e087ab8505 to your computer and use it in GitHub Desktop.
Save timurbakibayev/cf99b596e53d13af889ea4e087ab8505 to your computer and use it in GitHub Desktop.
Matrix Multiplication in C++
#include <iostream>
int main(int argc, const char * argv[]) {
int n = 800;
int a[n][n], b[n][n], mult[n][n], i, j, k;
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
a[i][j] = 1;
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
b[i][j] = 1;
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
mult[i][j]=0;
std::cout << std::endl << "Starting after matrix initialization..." << std::endl;
clock_t tStart = clock();
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
for(k = 0; k < n; ++k)
mult[i][j] += a[i][k] * b[k][j];
std::cout << std::endl << "I did it in "<< (double)(clock() - tStart)/CLOCKS_PER_SEC << "sec" << std::endl;
for(i = 0; i < 5; ++i)
for(j = 0; j < 5; ++j)
{
std::cout << " " << mult[i][j];
if(j == 4)
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment