From e249ebe4e3be4c85d99be53cb786ad81878b9ed7 Mon Sep 17 00:00:00 2001 From: Reshief Luminon <reshief@gmx.net> Date: Tue, 22 Oct 2019 04:07:05 +0200 Subject: [PATCH] Add sample with custom class and more complex operations --- src/matrix.h | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/sample_4.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/matrix.h create mode 100644 src/sample_4.cpp diff --git a/src/matrix.h b/src/matrix.h new file mode 100644 index 0000000..8db2dd9 --- /dev/null +++ b/src/matrix.h @@ -0,0 +1,65 @@ +#ifndef __MATRIX_H__ +#define __MATRIX_H__ +#include <vector> +#include <iostream> + +namespace magic { + using std::vector; + + // This is a custom Matrix type to allow for matrix multiplication + class Matrix { + public: + Matrix(uint8_t rows, uint8_t columns) { + this->_rows = rows; + this->_columns = columns; + this->entries.assign(rows, vector<int32_t>(columns, 0)); + } + + void setValue(uint8_t i, uint8_t j, int32_t value) { + entries[i][j] = value; + } + + int32_t getValue(uint8_t i, uint8_t j) { + return entries[i][j]; + } + + // Calculate the matrix product with another matrix + Matrix multiply(Matrix &other) { + // Create the result matrix + Matrix result(this->_rows, other._columns); + + // Calculate all entries in the resulting matrix + for (uint8_t r = 0; r < this->_rows; r++) { + for (uint8_t c = 0; c < other._columns; c++) { + for (uint8_t link = 0; link < this->_columns; link++) { + int32_t res = result.getValue(r, c) + this->getValue(r, link) * other.getValue(link, c); + result.setValue(r, c, res); + } + } + } + + // Return the resulting matrix + return result; + } + + // Output the matrix to screen + void print() { + using std::cout; + using std::endl; + + for (uint8_t r = 0; r < this->_rows; r++) { + for (uint8_t c = 0; c < this->_columns; c++) { + cout << entries[r][c] << '\t'; + } + cout << endl; + } + } + + private: + uint8_t _rows; + uint8_t _columns; + vector<vector<int32_t>> entries; + }; +} + +#endif \ No newline at end of file diff --git a/src/sample_4.cpp b/src/sample_4.cpp new file mode 100644 index 0000000..b466ac5 --- /dev/null +++ b/src/sample_4.cpp @@ -0,0 +1,68 @@ +#include "matrix.h" +#include <iostream> + +// A simple program to multiply to matrices read in from std input + +int main(int argc, char **argv) { + using std::cin; + using std::cout; + using std::cerr; + using std::endl; + + uint8_t rows1, columns1; + cout << "Number of rows of the first matrix" << endl; + cin >> rows1; + cout << "Number of columns of the first matrix" << endl; + cin >> columns1; + + // Import our custom Matrix type + using magic::Matrix; + + Matrix matrix_1(rows1, columns1); + + cout << "Please enter the entries of the matrix in a row-after-row fashion with spaces separating subsequent entries" << endl; + for (uint8_t r = 0; r < rows1; r++) { + for (uint8_t c = 0; c < columns1; c++) { + int32_t val; + cin >> val; + matrix_1.setValue(r, c, val); + } + } + + cout << "Matrix 1 was read:" << endl; + matrix_1.print(); + + + uint8_t rows2, columns2; + cout << "Number of rows of the second matrix" << endl; + cin >> rows2; + + if (rows2 != columns1) { + cerr << "The number of rows in the second matrix has to equal the number of columns in the first matrix" << endl; + // Declare an error and stop execution + exit(1); + } + cout << "Number of columns of the second matrix" << endl; + cin >> columns2; + + Matrix matrix_2(rows2, columns2); + + cout << "Please enter the entries of the matrix in a row-after-row fashion with spaces separating subsequent entries" << endl; + for (uint8_t r = 0; r < rows2; r++) { + for (uint8_t c = 0; c < columns2; c++) { + int32_t val; + cin >> val; + matrix_2.setValue(r, c, val); + } + } + + cout << "Matrix 2 was read:" << endl; + matrix_2.print(); + + Matrix product = matrix_1.multiply(matrix_2); + + cout << "M1 * M2 =" << endl; + product.print(); + + return 0; +} \ No newline at end of file -- GitLab