-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmult.cpp
More file actions
67 lines (58 loc) · 1.75 KB
/
Copy pathmatrixmult.cpp
File metadata and controls
67 lines (58 loc) · 1.75 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
/* generar la matriz de forma pseudoaleatoria
usar malloc para la memoria y atoi para los enteros */
#include <iostream>
#include <vector>
#include <cstdlib>
int main ( int argc, char *argv[] )
{
if ( argc != 4 )
{
std::cerr << "Usage: " << argv[0] << " N1 M1 M2" << std::endl;
return 1;
}
const int N1 = std::atoi( argv[1] );
const int M1 = std::atoi( argv[2] );
const int M2 = std::atoi( argv[3] );
const int N2 = M1; // Number of rows of the second matrix equals the number of columns of the first matrix
// Allocate and fill matrices A and B with random values
std::vector<std::vector<int>> A( N1, std::vector<int>(M1) );
std::vector<std::vector<int>> B( N2, std::vector<int>(M2) );
std::vector<std::vector<int>> C( N1, std::vector<int>(M2, 0) ); // Result matrix initialized to 0
// Fill matrices A and B with random values
for ( int i = 0; i < N1; ++i )
{
for ( int j = 0; j < M1; ++j )
{
A[i][j] = std::rand() % 10; // Values between 0 and 9
}
}
for ( int i = 0; i < N2; ++i )
{
for ( int j = 0; j < M2; ++j )
{
B[i][j] = std::rand() % 10; // Values between 0 and 9
}
}
// Perform matrix multiplication
for ( int i = 0; i < N1; ++i )
{
for ( int j = 0; j < M2; ++j )
{
for ( int k = 0; k < M1; ++k )
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Print the result matrix C (optional)
std::cout << "Result Matrix C:" << std::endl;
for ( int i = 0; i < N1; ++i )
{
for ( int j = 0; j < M2; ++j )
{
std::cout << C[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}