C++ Assignment Help

C++ Task – Classes and Data Abstraction

#include <iostream>

#include <iomanip>

 #include "matrix.h"
 using namespace std;

Matrix :: Matrix() {
 cnt++;
 row_size = maxRowSize;
 col_size = maxColSize;
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 matrix[r][c] = rand() % 10 + 1;
 }
 }
 }

Matrix :: Matrix(int row, int column) {
 if (row > maxRowSize || column > maxColSize) {
 cout << "Maximum allowed dimensions are 5 x 5" << endl;
 } else {
 cnt++;
 row_size = row;
 col_size = column;
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 matrix[r][c] = rand() % 10 + 1;
 }
 }
 }
 }

void Matrix :: setRowSize(int x) {
 row_size = x;
 }

void Matrix :: setColumnSize(int x) {
 col_size = x;
 }

void Matrix :: addValue(double x) {
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 matrix[r][c] = x;
 }
 }
 }

int Matrix :: getCnt() {
 return Matrix :: cnt;
 }

int Matrix :: getRowSize() {
 return row_size;
 }

int Matrix :: getColumnSize() {
 return col_size;
 }

void Matrix :: displayMatrix() {
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 cout << matrix[r][c] << " ";
 }
 cout << endl;
 }
 cout << endl;
 }

double Matrix :: getValue(int row, int col) {
 return matrix[row][col];
 }

Matrix Matrix :: add(Matrix &other) {
 if (getRowSize() != other.getRowSize() || getColumnSize() != other.getColumnSize()) {
 cout << "Matrix addition requires matrices with same dimensions" << endl << endl;
 } else {
 Matrix result = Matrix(getRowSize(), getColumnSize());
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 result.matrix[r][c] = matrix[r][c] + other.matrix[r][c];
 }
 }
 cout << "Resultant matrix after Addition:" << endl;
 result.displayMatrix();
 }
 }

Matrix Matrix :: substract(Matrix &other) {
 if (getRowSize() != other.getRowSize() || getColumnSize() != other.getColumnSize()) {
 cout << "Matrix subtraction requires matrices with same dimensions" << endl << endl;
 } else {
 Matrix result = Matrix(getRowSize(), getColumnSize());
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 result.matrix[r][c] = matrix[r][c] - other.matrix[r][c];
 }
 }
 cout << "Resultant matrix after Subtraction:" << endl;
 result.displayMatrix();
 }
 }

Matrix Matrix :: product(Matrix &other) {
 if (getColumnSize() != other.getRowSize()) {
 cout << "Matrix multiplication requires: No of rows in multiplicand matrix equal to No of rows in multiplier matrix" << endl << endl;
 } else {
 Matrix result = Matrix(getRowSize(), other.getColumnSize());
 result.addValue(0.0);
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < other.getColumnSize(); c++) {
 for (int k = 0; k < getColumnSize(); k++) {
 result.matrix[r][c] += (matrix[r][k] * other.matrix[k][c]);
 }
 }
 }
 cout << "Resultant matrix after Multiplication:" << endl;
 result.displayMatrix();
 }
 }

Matrix Matrix :: product(double scalar) {
 Matrix result = Matrix(getRowSize(), getColumnSize());
 for (int r = 0; r < getRowSize(); r++) {
 for (int c = 0; c < getColumnSize(); c++) {
 result.matrix[r][c] = matrix[r][c] * scalar;
 }
 }
 cout << "Resultant matrix after Scalar Multiplication:" << endl;
 result.displayMatrix();
 }




#include <iostream>
 #include<stdlib.h>
 #include "matrix.h"
 #include "matrix.cpp"

using namespace std;

int main() {
 double scalar;
 Matrix matA = Matrix(3,3);
 Matrix matB = Matrix(3,3);
 Matrix matC = Matrix(3,4);

cout << "Enter the scalar value for matrix multiplication : ";
 cin >> scalar;

cout << "Displaying matrix A" << endl;
 matA.displayMatrix();
 cout << "Displaying matrix B" << endl;
 matB.displayMatrix();
 cout << "Displaying matrix C" << endl;
 matC.displayMatrix();

// The sum of two matrixes with the same row and column size.
 cout << "Adding matrices with the same row and column size (matrix A and matrix B)" << endl;
 matA.add(matB);

// The sum of two matrices with different row or column size.
 cout << "Adding matrices with different row or column size (matrix A and matrix C)" << endl;
 matA.add(matC);

// The difference of two matrices with the same row and column size.
 cout << "Subtracting matrices with the same row and column size (matrix A and matrix B)" << endl;
 matA.substract(matB);

// The product of two matrices with the same row and column size.
 cout << "Multiplying matrices with the same row and column size (matrix A and matrix B)" << endl;
 matA.product(matB);

// The product of a matrix and a scalar value that is entered by the user.
 cout << "Multiplying matrix A with scalar value (" << scalar << ")" << endl;
 matA.product(scalar);

// Count of matrix objects created
 cout << "Total number of matrix objects created: " << matA.getCnt() << endl;

return 0;
 }
 /**
 ======== SAMPLE OUTPUT ========

Enter the scalar value for matrix multiplication : 2.5
 Displaying matrix A
 4 7 8
 6 4 6
 7 3 10

Displaying matrix B
 2 3 8
 1 10 4
 7 1 7

Displaying matrix C
 3 7 2 9
 8 10 3 1
 3 4 8 6

Adding matrices with the same row and column size (matrix A and matrix B)
 Resultant matrix after Addition:
 6 10 16
 7 14 10
 14 4 17

Adding matrices with different row or column size (matrix A and matrix C)
 Matrix addition requires matrices with same dimensions

Subtracting matrices with the same row and column size (matrix A and matrix B)
 Resultant matrix after Subtraction:
 2 4 0
 5 -6 2
 0 2 3

Multiplying matrices with the same row and column size (matrix A and matrix B)
 Resultant matrix after Multiplication:
 71 90 116
 58 64 106
 87 61 138

Multiplying matrix A with scalar value (2.5)
 Resultant matrix after Scalar Multiplication:
 10 17.5 20
 15 10 15
 17.5 7.5 25

Total number of matrix objects created: 7

**/




#ifndef MATRIX_H
 #define MATRIX_H

class Matrix
 {
 private:
 static int cnt;
 const static int maxRowSize = 5;
 const static int maxColSize = 5;
 int row_size;
 int col_size;
 double matrix[maxRowSize][maxColSize];

public:
 Matrix();
 Matrix(int row, int column);

void setRowSize(int x);
 void setColumnSize(int x);
 void addValue(double x);
 int getCnt();

int getRowSize();
 int getColumnSize();
 void displayMatrix();
 double getValue(int row, int col);

Matrix add(Matrix &other);
 Matrix substract(Matrix &other);
 Matrix product(Matrix &other);
 Matrix product(double scalar);
 };

int Matrix :: cnt = 0;

#endif