Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Matrix{
- private:
- int n, m;
- int **a;
- public:
- Matrix(int rows=2, int columns=2, int value=0){
- n = rows, m = columns;
- a = new int*[n];
- for (int i=0; i<n; i++){
- a[i] = new int[m];
- for (int j=0; j<m; j++) a[i][j] = value;
- }
- }
- void place(int row, int column, int val){
- a[row][column] = val;
- }
- int *get_all_as_array(){
- int *return_value = new int[n*m];
- for (int i=0; i<n; i++)
- for (int j=0; j<m; j++)
- return_value[m*i+j] = a[i][j];
- return return_value;
- }
- void print_content(){
- for (int i=0; i<n; i++){
- for (int j=0; j<m; j++)
- cout << a[i][j] << ' ';
- cout << endl;
- }
- }
- pair<int,int> get_dimensions(){
- return {n, m};
- }
- };
- int main(){
- Matrix m1 = Matrix(1, 10, 1), m2 = Matrix(2, 3, 12), m3 = Matrix();
- cout << "Matrices:\n";
- m1.print_content();
- cout << endl;
- m2.print_content();
- cout << endl;
- m3.print_content();
- int *arr1 = m1.get_all_as_array(), n1 = m1.get_dimensions().first * m1.get_dimensions().second;
- int *arr2 = m2.get_all_as_array(), n2 = m2.get_dimensions().first * m2.get_dimensions().second;
- int *arr3 = m3.get_all_as_array(), n3 = m3.get_dimensions().first * m3.get_dimensions().second;
- cout << "\nArrays:\n";
- for (int i=0; i<n1; i++){
- cout << arr1[i] << ' ';
- }
- cout << endl << endl;
- for (int i=0; i<n2; i++){
- cout << arr2[i] << ' ';
- }
- cout << endl << endl;
- for (int i=0; i<n3; i++){
- cout << arr3[i] << ' ';
- }
- cout << endl << endl;
- m2.place(1, 2, -5);
- cout << "Matrix2 after placing (-5) in position (1, 2):\n";
- m2.print_content();
- return 0;
- }
Add Comment
Please, Sign In to add comment