• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 19th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <vector>
3. #include <iomanip>
4.
5. using namespace std;
6.
7. #include "matrix_multiply.h" // zie onderaan in deze pastebin voor die code
8.
9. int main ()
10. {
11.     //matrices and dimensions
12.     int rows1, cols1, rows2, cols2;
13.     int **matrix1 = 0, **matrix2 = 0, **result = 0;
14.
16.     cout << "Enter matrix dimensions" << endl;
17.     cin >> rows1 >> cols1 >> rows2 >> cols2;
18.
19.     //TODO: allocate memory for matrixes
20.     matrix1 = new int*[rows1];
21.     for(int i = 0; i < rows1; ++i)
22.         {
23.             matrix1[i] = new int[cols1];
24.         }
25.     matrix2 = new int*[rows2];
26.     for(int i = 0; i < rows2; ++i)
27.         {
28.             matrix2[i] = new int[cols2];
29.         }
30.     result = new int*[rows1];
31.     for(int i = 0; i < rows1; ++i)
32.         {
33.             result[i] = new int[cols2];
34.         }
35.
36.     // Read values from the command line into a matrix
39.
40.     // Multiply matrix1 one and matrix2, and put the result in matrix result
41.     multiply_matrix(matrix1, rows1, cols1, matrix2, rows2, cols2, result);
42.
43.
44. //    print_matrix(matrix1, rows1, cols1);
45. //    print_matrix(matrix2, rows2, cols2);
46.     print_matrix(result, rows1, cols2);
47.
48.     //TODO: free memory holding the matrices
49.
50.     return 0;
51. }
52.
53. void read_matrix(int **matrix, int rows, int cols)
54. {
55.     cout << "Enter a matrix" << endl;
56.     for (int i = 0; i < rows; ++i)
57.     {
58.         for (int j = 0; j < cols; ++j)
59.         {
60.             cin >> matrix[i][j];
61.         }
62.     }
63. }
64.
65. void print_matrix(int **matrix, int rows, int cols)
66. {
67.     cout << "Result" << endl;
68.     for (int i = 0; i < rows; ++i)
69.     {
70.         for (int j = 0; j < cols; ++j)
71.         {
72.             cout << matrix[i][j] << "\t";
73.         }
74.         cout << endl;
75.     }
76. }
77.
78. void multiply_matrix(int **matrix1 , int rows1, int cols1, int **matrix2, int rows2, int cols2, int **result)
79. {
80.     for (int i = 0; i < rows1; ++i)
81.     {
82.         for (int j = 0; j < cols2; ++j)
83.         {
84.             for (int k = 0; k < rows2; ++k)
85.             {
86.                 result[i][j] += matrix1[i][k] * matrix2[k][j];
87.             }
88.         }
89.     }
90. }
91.
92.
93.
94.
95. //////////////////////// header file hieronder //////////////////////////
96.
97.
98. #ifndef MATRIX_MULTIPLY_H_INCLUDED
99. #define MATRIX_MULTIPLY_H_INCLUDED
100.
101. void read_matrix(int **matrix, int rows, int cols);
102.
103. void print_matrix(int **matrix, int rows, int cols);
104.
105. void multiply_matrix(int **matrix1 , int rows1, int cols1, int **matrix2, int rows2, int cols2, int **result);
106.
107. #endif // MATRIX_MULTIPLY_H_INCLUDED
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top