Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- #include "matrix_multiply.h" // zie onderaan in deze pastebin voor die code
- int main ()
- {
- //matrices and dimensions
- int rows1, cols1, rows2, cols2;
- int **matrix1 = 0, **matrix2 = 0, **result = 0;
- //TODO: readin matrix dimensions
- cout << "Enter matrix dimensions" << endl;
- cin >> rows1 >> cols1 >> rows2 >> cols2;
- //TODO: allocate memory for matrixes
- matrix1 = new int*[rows1];
- for(int i = 0; i < rows1; ++i)
- {
- matrix1[i] = new int[cols1];
- }
- matrix2 = new int*[rows2];
- for(int i = 0; i < rows2; ++i)
- {
- matrix2[i] = new int[cols2];
- }
- result = new int*[rows1];
- for(int i = 0; i < rows1; ++i)
- {
- result[i] = new int[cols2];
- }
- // Read values from the command line into a matrix
- read_matrix(matrix1, rows1, cols1);
- read_matrix(matrix2, rows2, cols2);
- // Multiply matrix1 one and matrix2, and put the result in matrix result
- multiply_matrix(matrix1, rows1, cols1, matrix2, rows2, cols2, result);
- // print_matrix(matrix1, rows1, cols1);
- // print_matrix(matrix2, rows2, cols2);
- print_matrix(result, rows1, cols2);
- //TODO: free memory holding the matrices
- return 0;
- }
- void read_matrix(int **matrix, int rows, int cols)
- {
- cout << "Enter a matrix" << endl;
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j)
- {
- cin >> matrix[i][j];
- }
- }
- }
- void print_matrix(int **matrix, int rows, int cols)
- {
- cout << "Result" << endl;
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j)
- {
- cout << matrix[i][j] << "\t";
- }
- cout << endl;
- }
- }
- void multiply_matrix(int **matrix1 , int rows1, int cols1, int **matrix2, int rows2, int cols2, int **result)
- {
- for (int i = 0; i < rows1; ++i)
- {
- for (int j = 0; j < cols2; ++j)
- {
- for (int k = 0; k < rows2; ++k)
- {
- result[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- }
- //////////////////////// header file hieronder //////////////////////////
- #ifndef MATRIX_MULTIPLY_H_INCLUDED
- #define MATRIX_MULTIPLY_H_INCLUDED
- void read_matrix(int **matrix, int rows, int cols);
- void print_matrix(int **matrix, int rows, int cols);
- void multiply_matrix(int **matrix1 , int rows1, int cols1, int **matrix2, int rows2, int cols2, int **result);
- #endif // MATRIX_MULTIPLY_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement