Skip to content
Snippets Groups Projects
Commit e249ebe4 authored by Kevin Höllring's avatar Kevin Höllring
Browse files

Add sample with custom class and more complex operations

parent 9f574587
No related branches found
No related tags found
No related merge requests found
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment