Napisz program, który wczytuje dwie macierze, a potem, jeżeli jest to możliwe, dodaje je i wypisuje macierz będącą ich sumą.

Odpowiedź :

Odpowiedź:

#include <iostream>

#include <vector>

using namespace std;

int main() {

vector<vector<int>> macierz_1 = {{6, 0, 9, 7}, {9, 9, 3, 8}, {4, 3, 9, 9}, {4, 0, 6, 6}};

vector<vector<int>> macierz_2 = {{0, 7, 5, 6}, {0, 3, 2, 8}, {8, 5, 4, 6}, {8, 5, 8, 1}};

vector<vector<int>> suma_macierz;

if ((macierz_1.size() != macierz_2.size() || macierz_1[0].size() != macierz_2[0].size())) {

 cout << "Rozmiary macierzy nie są równe" << endl;

 return 0;

}

suma_macierz = macierz_1;

for (int i = 0; i < macierz_2.size(); i++) {

 for (int j = 0; j < macierz_2[i].size(); j++) {

  suma_macierz[i][j] += macierz_2[i][j];

 }

}

for (int i = 0; i < suma_macierz.size(); i++) {

 for (int j = 0; j < suma_macierz[i].size(); j++) {

  cout << suma_macierz[i][j] << " ";

 }

 cout << endl;

}

return 0;

}

Bardzo ciekawe zadanko jeśli chce się zrobić macierze o dowolnych wymiarach.

Rozwiązanie:

#include <iostream>

#include <cassert>

class Matrix

{

private:

double** m_matrix{ nullptr };

int m_rows{ 0 }, m_cols{ 0 };

public:

Matrix() = default;

Matrix(int n, int m);

Matrix(const Matrix& arg);

~Matrix();

void fill1();

void fill0();

void printMatrix(); // DEBUG -- can be deleted

friend std::ostream& operator<<(std::ostream& out, const Matrix& m);

friend std::istream& operator>>(std::istream& in, Matrix& m);

friend Matrix operator+(Matrix& a, Matrix& b);

};

Matrix::Matrix(int n, int m) // Creates an n x m matrix

{

m_rows = n;

m_cols = m;

m_matrix = new double*[n];

for (int row = 0; row < n; row++)

{

 *(m_matrix + row) = new double[m];

}

//std::cout << "Constructor\n"; // DEBUG

}

Matrix::Matrix(const Matrix& arg) // Makes a deep copy of an existing object

{

m_rows = arg.m_rows;  

m_cols = arg.m_cols;

m_matrix = new double*[m_rows];

for (int row = 0; row < m_rows; row++)

{

 *(m_matrix + row) = new double[m_cols];

}

for (int row = 0; row < m_rows; row++)

{

 for (int col = 0; col < m_cols; col++)

 {

  *(*(m_matrix + row) + col) = *(*(arg.m_matrix + row) + col);

 }

}

//std::cout << "Copy constructor\n"; // DEBUG

}

Matrix::~Matrix()

{

for (int row = 0; row < m_rows; row++)

{

 delete[] *(m_matrix + row);

}

delete[] m_matrix;

//std::cout << "Destructor\n";

}

void Matrix::fill1() // fils the matrix with ones

{

for (int row = 0; row < m_rows; row++)

{

 for (int col = 0; col < m_cols; col++)

 {

  *(*(m_matrix + row) + col) = 1;

 }

}

}

void Matrix::fill0() // fills the matrix with zeros

{

for (int row = 0; row < m_rows; row++)

{

 for (int col = 0; col < m_cols; col++)

 {

  *(*(m_matrix + row) + col) = 0;

 }

}

}

std::ostream& operator<<(std::ostream& out, const Matrix& m)

{

for (int row = 0; row < m.m_rows; row++)

{

 for (int col = 0; col < m.m_cols; col++)

 {

  out << *(*(m.m_matrix + row) + col) << ' ';

 }

 out << '\n';

}

return out;

}

std::istream& operator>>(std::istream& in, Matrix& m)

{

double input;

for (int row = 0; row < m.m_rows; row++)

{

 for (int col = 0; col < m.m_cols; col++)

 {

  std::cout << "Entry (" << row << ", " << col << "): ";

  in >> input;

  *(*(m.m_matrix + row) + col) = input;

 }

}

return in;

}

Matrix operator+(Matrix& a, Matrix& b)

{

assert(a.m_rows == b.m_rows && a.m_cols == b.m_cols);

Matrix sum{ a.m_rows, a.m_cols };

for (int row = 0; row < a.m_rows; row++)

{

 for (int col = 0; col < a.m_cols; col++)

 {

  *(*(sum.m_matrix + row) + col) = *(*(a.m_matrix + row) + col) + *(*(b.m_matrix + row) + col);

 }

}

return sum;

}

void Matrix::printMatrix()

{

for (int row = 0; row < m_rows; row++)

{

 for (int col = 0; col < m_cols; col++)

 {

  std::cout << *(*(m_matrix + row) + col) << ' ';

 }

 std::cout << '\n';

}

}

int main()

{

std::cout << "Dimensions of the first matrix (space separated): ";

int n, m;

std::cin >> n >> m;

Matrix a{ n,m };

std::cout << "\nFill the first matrix\n";

std::cin >> a;

std::cout << "Dimensions of the first matrix (space separated): ";

std::cin >> n >> m;

Matrix b{ n,m };

std::cout << "\nFill the second matrix\n";

std::cin >> b;

std::cout << "a:\n" << a;

std::cout << "b:\n" << b;

Matrix c = a + b;

std::cout << "c = a + b:\n" << c;

}