This is the code I have. How can I make the header file more organized and easie
ID: 3697677 • Letter: T
Question
This is the code I have. How can I make the header file more organized and easier for the user running the program to input values. It's organized up until they have to enter the elements. In the function "getmatrix()" it gets really unorganized. Can someone help me make it more understandable and straight forward?
HEADER ///////////////
#pragma once
#include<iostream>
using namespace std;
//class declartion
class matrix
{
public:
int row;
int col;
int arry[10][10];
public:
matrix() { }
matrix(int, int);
//member function declarations
friend ostream& operator<<(ostream& os, const matrix& m);
void getmatrix();
matrix operator+(const matrix& m);
matrix operator-(const matrix& m);
matrix operator*(const matrix& s);
};
// function that's called at beginning of switch case
void matrix::getmatrix()
{
cout << "Enter all elements of matrix" << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cin >> arry[i][j];
}
}
}
//CONTSTRUCTOR
matrix::matrix(int a, int b)
{
row = a;
col = b;
}
matrix matrix::operator+(const matrix& m)
{
matrix c(row, col);
if ((row == m.row) && (col == m.col))
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
c.arry[i][j] = arry[i][j] + m.arry[i][j];
}
}
}
else
{
cout << "Order of the two matrices should be same" << endl;
exit(0);
}
return c;
}
matrix matrix::operator-(const matrix& m)
{
matrix c(row, col);
if ((row == m.row) && (col == m.col))
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
c.arry[i][j] = arry[i][j] - m.arry[i][j];
}
}
}
else
{
cout << "Order of the two matrices should be same" << endl;
exit(0);
}
return c;
}
matrix matrix::operator*(const matrix& m)
{
matrix c(row, col);
if (col == m.row)
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < m.col; ++j) {
int total = 0;
for (int k = 0; k < col; ++k) {
total += arry[i][k] * m.arry[k][j];
}
c.arry[i][j] = total;
}
}
}
else
{
cout << "Multiplication not possible, enter matrix of order m(pXq) and n(qXr)" << endl;
exit(0);
}
return c;
}
ostream& operator<<(ostream& os, const matrix& m)
{
for (int r = 0; r < m.row; ++r) {
for (int c = 0; c < m.col; ++c) {
os << m.arry[r][c];
os << " ";
}
os << endl;
}
os << endl;
return os;
}
DRIVER ////////////////////
#include <iostream>
#include "matrix.h"
#include <windows.h>
using namespace std;
int main()
{
int row1;
int col1;
int row2;
int col2;
int num;
while (true) {
//Menu display
cout << "1.) Addition" << endl;
cout << "2.) Subtraction" << endl;
cout << "3.) Multiplication" << endl;
cout << "4.) Exit" << endl << endl;
cout << "Select an operation from the menu: ";
cin >> num;
cout << "OPERATION" << " " << num << " SELECTED." << endl << endl;
Beep(700, 100);
if (num == 4) {
cout << "EXITING PROGRAM" << endl << endl;
// Exit Tune
Beep(700, 500);
Beep(525, 175);
Beep(500, 175);
Beep(525, 175);
Beep(550, 500);
Beep(525, 175);
Sleep(850);
cout << "Have a nice day!" << endl;
Beep(675, 300);
Sleep(200);
Beep(700, 300);
exit(0);
}
// matrix 1 /////////////////////////////////////////
cout << "Enter the number of rows in matrix1: ";
cin >> row1;
cout << endl;
Beep(700, 100);
cout << "Enter the number of columns in matrix1: ";
cin >> col1;
cout << endl;
Beep(700, 100);
// matrix 2 /////////////////////////////////////////
cout << "Enter the number of rows in matrix2: ";
cin >> row2;
cout << endl;
Beep(700, 100);
cout << "Enter the number of columns in matrix2: ";
cin >> col2;
cout << endl;
Beep(700, 100);
// matrix instantiations
matrix d(row1, col1);
matrix e(row2, col2);
matrix f(row1, col1);
matrix s(row1, col1);
matrix m(row1, col2);
switch (num)
{
case 1:
//input two matrix
d.getmatrix();
e.getmatrix();
f = d + e; // addition
cout << "Result: " << f << endl;
break;
case 2: //inputting two matrix
d.getmatrix();
e.getmatrix();
s = d - e; // subtraction
cout << "Result: " << s << endl;
break;
case 3: //inputting two matrix
d.getmatrix();
e.getmatrix();
m = d*e; // multiplication
cout << "Result: " << m << endl;
break;
default:
system("cls");
cout << "Invalid Input. Try again... ";
break;
}
}
system("pause");
return 0;
}
Explanation / Answer
#ifndef _MATRIX
#define _MATRIX
#include<vector>
#include<iostream>
using namespace std;
class Matrix
{
public:
int rows, col;
vector< vector<double> > mat;
Matrix();
Matrix(int, int);
void print();
Matrix add(Matrix, Matrix);
Matrix sub(Matrix, Matrix);
Matrix multiply(Matrix, Matrix);
Matrix& operator+(const Matrix&);
Matrix& operator=(const Matrix&);
Matrix& operator-(const Matrix&);
Matrix& operator*(const Matrix&);
};
Matrix::Matrix()
{
rows = 2;
col = 2;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(0);
mat.push_back(rw);
}
}
Matrix::Matrix(int r, int c)
{
rows = r;
col = c;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(0);
mat.push_back(rw);
}
}
// Display the matrix on screen
void Matrix::print()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < col; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
// Add 2 matrices operator overloading
Matrix Matrix::add(Matrix A, Matrix B)
{
if (A.rows != B.rows || A.col != B.col)
{
Matrix Z(0, 0);
return Z;
}
Matrix C(A.rows, A.col);
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
C.mat[i][j] = A.mat[i][j] + B.mat[i][j];
}
return C;
}
// Subtract 2 matrices
Matrix Matrix::sub(Matrix A, Matrix B)
{
if (A.rows != B.rows || A.col != B.col)
{
Matrix Z(0, 0);
return Z;
}
Matrix C(A.rows, A.col);
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
C.mat[i][j] = A.mat[i][j] - B.mat[i][j];
}
return C;
}
Matrix Matrix::multiply(Matrix A, Matrix B)
{
Matrix C(A.rows, B.col);
if (A.col != B.rows)
return C;
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
{
for (int k = 0; k < A.col; k++)
C.mat[i][j] += A.mat[i][k] * B.mat[k][j];
}
}
return C;
}
Matrix& Matrix::operator=(const Matrix&M)
{
mat.clear();
rows = M.rows;
col = M.col;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(M.mat[i][j]);
mat.push_back(rw);
}
return *this;
}
Matrix& Matrix::operator+(const Matrix &M)
{
if (M.rows != rows || M.col != col)
return *this;
Matrix *C = new Matrix(M.rows, M.col);
for (int i = 0; i < M.rows; i++)
{
for (int j = 0; j < M.col; j++)
C->mat[i][j] = this->mat[i][j] + M.mat[i][j];
}
return *C;
}
Matrix& Matrix::operator-(const Matrix &M)
{
if (M.rows != rows || M.col != col)
return *this;
Matrix *C = new Matrix(M.rows, M.col);
for (int i = 0; i < M.rows; i++)
{
for (int j = 0; j < M.col; j++)
C->mat[i][j] = this->mat[i][j] - M.mat[i][j];
}
return *C;
}
Matrix& Matrix::operator*(const Matrix &M)
{
Matrix *C = new Matrix(rows, M.col);
if (col != M.rows)
return *C;
for (int i = 0; i < C->rows; i++)
{
for (int j = 0; j < C->col; j++)
{
for (int k = 0; k < M.col; k++)
C->mat[i][j] += mat[i][k] * M.mat[k][j];
}
}
return *C;
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.