From 9b8dd8c6c6c50e968786a82b02f9361abdbcd680 Mon Sep 17 00:00:00 2001 From: tbarnes98 Date: Mon, 28 Mar 2022 21:32:22 -0500 Subject: [PATCH] need to valgrind --- Lab3/main.cpp | 24 ++++++++++++++++++++++-- Lab3/matrix.cpp | 38 +++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Lab3/main.cpp b/Lab3/main.cpp index ea1997c..5d4fe35 100644 --- a/Lab3/main.cpp +++ b/Lab3/main.cpp @@ -8,13 +8,33 @@ * @copyright Copyright (c) 2022 * */ -#include +#include #include "matrix.h" using namespace std; int main() { - Matrix m1(2,2); + int rows, cols; + cout << "Enter the number of rows in the test matrix: " << endl; + cin >> rows; + cout << "Enter the number of columns in the test matrix: " << endl; + cin >> cols; + Matrix m1(rows,cols); // add more tests + for(int i = 0; i < rows; i++){ + for(int j = 0; j < cols; j++){ + m1[i][j] = 4.268394 + (i+7)*(j+7); + } + m1[0][0] = 4.20; + m1[0][1] = 6.9; + m1[0][2] = 1.337; + m1[0][3] = 12.18; + + } + cout << m1 << endl; + m1*5; + cout << m1 << endl; + m1.clear(); + cout << m1; return 0; } diff --git a/Lab3/matrix.cpp b/Lab3/matrix.cpp index b6e375a..637e35f 100644 --- a/Lab3/matrix.cpp +++ b/Lab3/matrix.cpp @@ -25,7 +25,7 @@ Matrix::Matrix(unsigned int rows, unsigned int cols){ this->cols = cols; // Create the row pointer this->the_matrix = new Row*[rows]; - for(int i = 0; i < rows; i++){ + for(int i = 0; i < rows+1; i++){ // Assign the rows of the matrix to new memory this->the_matrix[i] = new Row(cols); } @@ -39,7 +39,7 @@ Matrix::Matrix(const Matrix& from){ this->cols = from.cols; // Create the row pointer with new row amount this->the_matrix = new Row*[rows]; - for(int i = 0; i < this->rows; i++){ + for(int i = 0; i < rows+1; i++){ // Set the pointer to each copied row the_matrix[i] = new Row(from[i]); } @@ -48,7 +48,7 @@ Matrix::Matrix(const Matrix& from){ // Destructor Matrix::~Matrix(){ // Explicitly calls the destructor for each row in the matrix - for(int i = 0; i < rows; i++) { + for(int i = 0; i < rows+1; i++) { the_matrix[i]->~Row(); } // Deletes the matrix itself @@ -95,12 +95,11 @@ Matrix Matrix::operator*(const Matrix& rhs) const{ return result; } -// Scalar multiplication - TODO +// Scalar multiplication Matrix Matrix::operator*(const double scale) const{ for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ - double temp = (*the_matrix[i])[j]*scale; - (*the_matrix[i])[j] = temp; + (*the_matrix[i])[j] = (*the_matrix[i])[j]*scale; } } Matrix result(this->rows, this->cols); @@ -117,7 +116,7 @@ Matrix Matrix::operator~() const{ // Clear Matrix void Matrix::clear(){ - for(int i = 0; i < rows ; i++) { + for(int i = 0; i < rows+1; i++) { the_matrix[i]->clear(); } } @@ -125,7 +124,7 @@ void Matrix::clear(){ // Access Operators - non-const Row& Matrix::operator[](unsigned int row){ // Ensure the row is in range - if(row >= rows){ + if(row > rows){ throw(out_of_range("Row is out of range")); } return *(the_matrix[row]); @@ -134,7 +133,7 @@ Row& Matrix::operator[](unsigned int row){ // Access Operators - const const Row& Matrix::operator[](unsigned int row) const{ // Ensure the row is in range - if(row >= rows){ + if(row > rows){ throw(out_of_range("Row is out of range")); } return *(the_matrix[row]); @@ -143,11 +142,28 @@ const Row& Matrix::operator[](unsigned int row) const{ // print to output stream - TODO void Matrix::out(std::ostream& os) const{ os << setprecision(4); - os << setw(10); + os << "["; + for(int i = 0; i < rows; i++) { + if(i != 0){ + cout << " "; + } + os << "[ "; + for(int j = 0; j < cols; j++) { + os << (*the_matrix[i])[j]; + if(j != cols-1){ + os << ", "; + } + } + os << " ]"; + if(i != rows-1){ + os << endl; + } + } + os << "]" << endl; // finish } -// global insertion operator - TODO +// global insertion operator std::ostream& operator<<(std::ostream& os, const Matrix& rhs){ rhs.out(os); return os;