Skip to content
Snippets Groups Projects
Commit 81e2b37e authored by Murtaza Khalid's avatar Murtaza Khalid
Browse files

Merge branch 'm39khali-master-patch-08500' into 'master'

Update main.cc, Makefile, matrix.cc, matrix.h files

See merge request m39khali/matrix!1
parents cd1db28f 4a8df48c
Branches master
No related tags found
1 merge request!1Update main.cc, Makefile, matrix.cc, matrix.h files
Makefile 0 → 100644
CXX = g++
CXXFLAGS = -std=c++14 -Wall -MMD
EXEC = main
OBJECTS = main.o matrix.o
DEPENDS = ${OBJECTS:.o=.d}
${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC}
-include ${DEPENDS}
.PHONY: clean
clean:
rm ${OBJECTS} ${EXEC} ${DEPENDS}
main.cc 0 → 100644
#include <iostream>
#include "matrix.h"
using namespace std;
//************************************************************************
// Helper variables and functions for test harness
//************************************************************************
// testharness operators
enum Op {NONE, CONSTRUCT, DELETE, COPY, MOVE, COPY_ASSIGN, MOVE_ASSIGN, READ, PRINT, SET, GET, ADD, MULT};
// converts a one-character input into its corresponding test harness operator
Op convertOp(string opStr) {
switch (opStr[0]) {
case 'c': return CONSTRUCT;
case 'C': return COPY;
case 'd': return DELETE;
case 'a': return COPY_ASSIGN;
case 'A': return MOVE_ASSIGN;
case 'r': return READ;
case 'p': return PRINT;
case 'm': return MOVE;
case 's': return SET;
case 'g': return GET;
case '+': return ADD;
case '*': return MULT;
default: return NONE;
}
}
// Reads a Matrix id (m#) from cin.
int readName() {
char m;
cin >> m;
if ( m != 'm' ) {
cerr << "Invalid name of Matrix variable.\n";
}
int index;
cin >> index;
return index;
}
// Read in the number of rows/columns
int readDimension(string dimensionDescription) {
int dimension;
cerr << "Enter number of " << dimensionDescription << " (>=1): ";
cin >> dimension;
return dimension;
}
// Given the Matrix object whose dimensions have been set, retrieve the dimensions and then read in the
// values from standard input.
void readMatrix(Matrix *matrix) {
int numRows = matrix->rows();
int numCols = matrix->cols();
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
int value;
cin >> value;
matrix->set(i,j,value);
}
}
}
// Create a matrix using the constructor to set the dimensions, then read in the values from cin.
void buildMatrix(Matrix *matrices[]) {
int index = readName();
int numRows = readDimension("rows");
int numCols = readDimension( "columns");
delete matrices[index];
matrices[index] = new Matrix(numRows, numCols);
readMatrix(matrices[index]);
}
// Use operator>> to read in the matrix of values.
void readInMatrix(Matrix *matrices[]) {
int index = readName();
cerr << "Enter matrix dimensions (number of rows, number of columns) followed by values: ";
delete matrices[index];
matrices[index] = new Matrix();
cin >> *(matrices[index]);
cout << "Matrix m" << index << " =\n" << *(matrices[index]) << endl;
}
// Invoke the destructor on the matrix.
void deleteMatrix(Matrix *matrices[]) {
int index = readName();
delete matrices[index];
matrices[index] = nullptr;
cout << "Matrix m" << index << " deleted" << endl;
}
// Use operator<< to output the matrix.
void printMatrix(Matrix *matrices[]) {
int index = readName();
cout << "Matrix m" << index << " =\n" << *(matrices[index]) << endl;
}
// Use Matrix move constructor to move contents of one matrix to another, temporary matrix.
void moveMatrix(Matrix *matrices[]) {
int index = readName();
Matrix m(std::move(*matrices[index]));
cout << "New Matrix =\n" << m << "\nm" << index << " =\n" << *(matrices[index]) << endl;
}
// Use Matrix copy constructor to copy contents of one matrix to another, temporary matrix.
void copyMatrix(Matrix *matrices[]) {
int index = readName();
Matrix m(*matrices[index]);
cout << "New Matrix =\n" << m << "\nm" << index<< " =\n" << *(matrices[index]) << endl;
}
// Use Matrix copy assignment operator to copy contents of one matrix to another.
void copyAssignMatrix(Matrix *matrices[]) {
int index1 = readName();
int index2 = readName();
*matrices[index1] = *matrices[index2];
cout << "New Matrix =\n" << *(matrices[index1]) << "\nold Matrix =\n" << *(matrices[index2]) << endl;
}
// Use Matrix move assignment operator to move contents of one matrix to another.
void moveAssignMatrix(Matrix *matrices[]) {
int index1 = readName();
int index2 = readName();
*matrices[index1] = std::move(*matrices[index2]);
cout << "m" << index1 << " =\n" << *(matrices[index1]) << "\nm" << index2 << " =\n"
<< *(matrices[index2]) << endl;
}
// Set m#[row][col] = value
void setMatrix(Matrix * matrices[]) {
int index = readName();
cerr << "Enter the row, column and value: ";
int row, col, value;
cin >> row >> col >> value;
matrices[index]->set(row, col, value);
cout << "m" << index << "[" << row << "][" << col << "] = " << value << endl;
}
// Get m#[row][col]
void getMatrix(Matrix *matrices[]) {
int index = readName();
cerr << "Enter the row and column: ";
int row, col;
cin >> row >> col;
cout << "m" << index << "[" << row << "][" << col << "] = "
<< matrices[index]->get(row, col ) << endl;
}
void addMatrices(Matrix *matrices[]) {
int index1 = readName();
int index2 = readName();
if (matrices[index1]->rows() != matrices[index2]->rows() ||
matrices[index1]->cols() != matrices[index2]->cols()) {
cerr << "Dimensions of m" << index1 << " != m" << index2 << endl;
return;
}
Matrix m = *matrices[index1] + *matrices[index2];
cout << "m" << index1 << " + m" << index2 << " =\n" << m << endl;
}
void multMatrices(Matrix *matrices[]) {
int index1 = readName();
int index2 = readName();
if (matrices[index1]->cols() != matrices[index2]->rows()) {
cerr << "Dimensions of m" << index1 << " != m" << index2 << endl;
return;
}
Matrix m = (*matrices[index1]) * (*matrices[index2]);
cout << "m" << index1 << " * m" << index2 << " =\n" << m << endl;
}
int main () {
Matrix *matrices[10] = {nullptr};
for (;;) {
cerr << "Command: ";
string command;
if (!(cin >> command)) break;
Op op = convertOp(command);
switch(op) {
case CONSTRUCT:
buildMatrix(matrices);
break;
case DELETE:
deleteMatrix(matrices);
break;
case READ:
readInMatrix(matrices);
break;
case PRINT:
printMatrix(matrices);
break;
case MOVE:
moveMatrix(matrices);
break;
case COPY:
copyMatrix(matrices);
break;
case COPY_ASSIGN:
copyAssignMatrix(matrices);
break;
case MOVE_ASSIGN:
moveAssignMatrix(matrices);
break;
case SET:
setMatrix(matrices);
break;
case GET:
getMatrix(matrices);
break;
case ADD:
addMatrices(matrices);
break;
case MULT:
multMatrices(matrices);
break;
default:
cerr << "Invalid command." << endl;
break;
}
}
for (auto &m: matrices) delete m;
}
matrix.cc 0 → 100644
#include <iostream>
#include <utility>
#include <iomanip>
#include "matrix.h"
using namespace std;
// makes a new matrix array
int **makeMatrixArr(int rows, int cols) {
int **m = new int* [rows];
for (int i = 0; i < rows; ++i) {
m[i] = new int[cols];
}
return m;
}
// makes stolen matrices
void Matrix::zeroMaker(Matrix &zeroM) {
for (int r = 0; r < zeroM.nrows; ++r) {
delete[] zeroM.m[r];
}
delete[] zeroM.m;
zeroM.nrows = 1;
zeroM.ncols = 1;
zeroM.m = new int* [1];
zeroM.m[0] = new int[1];
zeroM.m[0][0] = 0;
}
// swaps matrices
void Matrix::swap(Matrix &other) {
using std::swap;
swap(m, other.m);
swap(nrows, other.nrows);
swap(ncols, other.ncols);
}
// Matrix Constructor
Matrix::Matrix(int numRows, int numCols) {
m = makeMatrixArr(numRows, numCols);
nrows = numRows;
ncols = numCols;
}
// Matrix Destructor
Matrix::~Matrix() {
for (int r = 0; r < this->nrows; ++r) {
delete[] this->m[r];
}
delete[] this->m;
}
// Gets total rows
int Matrix::rows() const { // returns the number of rows in the matrix
return this->nrows;
}
// Gets total columns
int Matrix::cols() const { // returns the number of columns in the matrix
return this->ncols;
}
// Matrix Copy Constructor
Matrix::Matrix(const Matrix &copy): nrows{copy.nrows}, ncols{copy.ncols} {
m = makeMatrixArr(nrows, ncols);
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
m[i][j] = copy.m[i][j];
}
}
}
// Matrix Copy Assignment Operator
Matrix &Matrix::operator=(const Matrix &other) {
Matrix copy{other};
swap(copy);
return *this;
}
int Matrix::get(int row, int col) const{
return m[row][col];
}
void Matrix::set(int row, int col, int value) {
m[row][col] = value;
}
// Matrix Move Constructor
Matrix::Matrix(Matrix &&moved): nrows{moved.nrows}, ncols{moved.ncols} {
m = makeMatrixArr(nrows, ncols);
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
m[i][j] = moved.m[i][j];
}
}
zeroMaker(moved);
}
//Matrix Move Assignment Operator
Matrix &Matrix::operator=(Matrix &&other) {
swap(other);
zeroMaker(other);
return *this;
}
// Overloaded + to handle Matrix addition
Matrix Matrix::operator+(const Matrix &m2) const {
Matrix* sumM = new Matrix(nrows, ncols);
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
sumM->m[i][j] = m[i][j] + m2.m[i][j];
}
}
return *sumM;
}
// Overloaded * to handle Matrix multiplication
Matrix Matrix::operator*(const Matrix &m2) const {
Matrix* multM = new Matrix(nrows, ncols);
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
for (int x = 0; x < nrows; ++x) {
multM->m[i][j] += m[i][x] * m2.m[x][j];
}
}
}
return *multM;
}
// overloaded >> to use standard input to create a Matrix
istream &operator>>(std::istream &in, Matrix &mat) {
in >> mat.nrows;
in >> mat.ncols;
for (int r = 0; r < mat.nrows; ++r) {
delete[] mat.m[r];
}
delete[] mat.m;
mat.m = makeMatrixArr(mat.nrows, mat.ncols);
for (int i = 0; i < mat.nrows; ++i) {
for (int j = 0; j < mat.ncols; ++j) {
in >> mat.m[i][j];
}
}
return in;
}
// overloaded << to output Matrix to standard output
ostream &operator<<(std::ostream &out, const Matrix &mat) {
for (int i = 0; i < mat.nrows; ++i) {
for (int j = 0; j < mat.ncols; ++j) {
if (j == mat.ncols-1) {
out << setw(4) << mat.m[i][j] << " ";
} else {
out << setw(4) << mat.m[i][j] << " ";
}
}
out << endl;
}
return out;
}
matrix.h 0 → 100644
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
struct Matrix {
// Requires numRows >= 1 && numCols >= 1.
// allocate space and set values in 2-D array to 0--fill later using
// either operator>> or set().
Matrix(int numRows = 1, int numCols = 1);
Matrix(const Matrix &);
Matrix(Matrix &&);
~Matrix();
Matrix &operator=(const Matrix &);
Matrix &operator=(Matrix &&);
Matrix operator+(const Matrix &) const;
Matrix operator*(const Matrix &) const;
int rows() const; // returns the number of rows in the matrix
int cols() const; // returns the number of columns in the matrix
// Sets this's [row][col] == value
void set(int row, int col, int value);
// Returns this's [row][col]
int get(int row, int col) const;
// Add fields and helper methods below
// DO NOT MODIFY CODE ABOVE THIS LINE
int **m;
int nrows;
int ncols;
void swap(Matrix &other);
void zeroMaker(Matrix &m);
};
std::istream &operator>>(std::istream &, Matrix &);
std::ostream &operator<<(std::ostream &, const Matrix &);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment