nord vpnnord vpn
Ad

Matrix

 avatar
Alexmegawin
c_cpp
7 months ago
2.2 kB
6
Indexable
Never
#include <iostream>
using namespace std;

class Matrix
{
private:
	double** storage;
public:
	const int n;
	const int m;

	Matrix(int _n, int _m) :
		n(_n), m(_m)
	{
		storage = new double* [n];

		for (int i = 0; i < n; i++)
		{
			storage[i] = new double[m];
		}

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				storage[i][j] = 0;
			}
		}
	}

	double get_number(int i, int j)
	{
		return storage[i][j];
	}

	void set_number(int i, int j, double num)
	{
		storage[i][j] = num;
	}

	static bool Sum(Matrix a, Matrix b, Matrix* c)
	{
		if ((a.n != c->n|| b.n != c->n) || (a.m != c->m || b.m != c->m))
		{
			return false;
		}

		for (int i = 0; i < a.n; i++)
		{
			for (int j = 0; j < a.m; j++)
			{
				c->storage[i][j] = a.storage[i][j] + b.storage[i][j];
			}
		}

		return true;
	}
};

class Matrix_3x3 : public Matrix
{
public:
	Matrix_3x3() : Matrix(3, 3){}
	
	double determinant()
	{
		double d1 = get_number(0, 0) * get_number(1, 1) * get_number(2, 2);
		double d2 = get_number(0, 2) * get_number(1, 1) * get_number(2, 0);


		return d1 + get_number(0, 1) * get_number(1, 2) * get_number(2, 0) + get_number(0, 2) * get_number(1, 0) * get_number(2, 1) - (d2 + get_number(0, 0) * get_number(1, 2) * get_number(2, 1) + get_number(0, 1) * get_number(1, 0) * get_number(2, 1));
	}
};


int main()
{
	Matrix a(4, 4);
	Matrix b(4, 4);
	Matrix c(4, 4);

	for (int i = 0; i < a.n; i++)
	{
		for (int j = 0; j < a.m; j++)
		{
			a.set_number(i, j, 1);
		}
	}

	for (int i = 0; i < b.n; i++)
	{
		for (int j = 0; j < b.m; j++)
		{
			b.set_number(i, j, 1);
		}
	}

	Matrix::Sum(a, b, &c);

	for (int i = 0; i < c.n; i++)
	{
		for (int j = 0; j < c.m; j++)
		{
			cout << c.get_number(i, j) << " ";
		}
		cout << endl;
	}
	cout << endl;


	Matrix_3x3 x;

	for (int i = 0; i < x.n; i++)
	{
		for (int j = 0; j < x.m; j++)
		{
			x.set_number(i, j, 1);
		}
	}

	for (int i = 0; i < x.n; i++)
	{
		for (int j = 0; j < x.m; j++)
		{
			cout << x.get_number(i, j) << " ";
		}
		cout << endl;
	}
	
	cout << endl << x.determinant();

	return 0;

}

nord vpnnord vpn
Ad