add solution for hw 5

This commit is contained in:
JamesFlare1212
2025-03-12 10:00:49 -04:00
parent c20ce7c2c3
commit 903e76ef1f
6 changed files with 772 additions and 326 deletions

View File

@@ -2,75 +2,63 @@
#define MATRIX_H
#include <iostream>
#include <cmath>
class Matrix {
public:
// Constructors & Destructor
Matrix(); // Default constructor (creates an empty 0 x 0 matrix)
Matrix(unsigned int rows, unsigned int cols, double fill);
Matrix(const Matrix &other);
~Matrix();
Matrix& operator=(const Matrix &other);
// Accessors for dimensions
unsigned int num_rows() const;
unsigned int num_cols() const;
// Clears the matrix (deallocates any memory and sets size to 0 x 0)
void clear();
// Safe accessor and modifier methods
bool get(unsigned int row, unsigned int col, double &value) const;
bool set(unsigned int row, unsigned int col, double value);
// Simple matrix operations
void multiply_by_coefficient(double coefficient);
bool swap_row(unsigned int row1, unsigned int row2);
void transpose();
// Binary matrix operations (modifies this matrix)
bool add(const Matrix &other);
bool subtract(const Matrix &other);
// Advanced accessors: returns a dynamic array with the requested row or column.
// The caller is responsible for deleting the returned array.
double* get_row(unsigned int row) const;
double* get_col(unsigned int col) const;
// Quarter the matrix into four equally sized quadrants.
// The four matrices are returned in a dynamically allocated array in the order:
// UL, UR, LL, LR.
// If the matrix is too small (i.e. less than 2 rows or 2 cols), returns four empty matrices.
Matrix* quarter() const;
// Equality operators
bool operator==(const Matrix &other) const;
bool operator!=(const Matrix &other) const;
// Friend overloaded output operator for printing the matrix.
friend std::ostream& operator<<(std::ostream &out, const Matrix &m);
private:
unsigned int rows;
unsigned int cols;
double** data;
//allocate memory and fill with a given value
void allocateMemory(unsigned int r, unsigned int c, double fill);
//deallocate memory
void deallocateMemory();
public:
////Constructors
Matrix();
Matrix(unsigned int r, unsigned int c, double fill);
//copy constructor
Matrix(const Matrix& other);
////Destructor
~Matrix();
////Accessors
Matrix& operator=(const Matrix& other);
unsigned int num_rows() const;
unsigned int num_cols() const;
//deallocates memory and resets rows/cols to 0)
void clear();
//if (row, col) is within bounds, stores the element in `value` and returns true;
//otherwise, returns false
bool get(unsigned int row, unsigned int col, double &value) const;
////Modifier
//if (row, col) is within bounds, sets the element to `value` and returns true;
//otherwise, returns false.
bool set(unsigned int row, unsigned int col, double value);
//multiplies every element in the matrix by the provided coefficient
void multiply_by_coefficient(double coeff);
//swaps two rows of the matrix. Returns true if both indices are valid,
//false otherwise
bool swap_row(unsigned int row1, unsigned int row2);
//transposes the matrix in place (switches rows and columns).
void transpose();
//adds another matrix to this one element-wise (if dimensions match) and returns true;
//otherwise, returns false.
bool add(const Matrix& other);
//subtracts another matrix from this one element-wise (if dimensions match)
//and returns true; otherwise, returns false.
bool subtract(const Matrix& other);
//returns a new dynamically allocated array (of size num_cols)
//containing the elements in the specified row.
double* get_row(unsigned int row) const;
//returns a new dynamically allocated array (of size num_rows)
//containing the elements in the specified column.
double* get_col(unsigned int col) const;
//divides the matrix into four quadrants and returns a pointer to an array of
//4 Matrix objects: Upper Left, Upper Right, Lower Left, Lower Right.
//each quadrant is of size ceil(rows/2) x ceil(cols/2)
//and the quadrants overlap when the dimensions are odd.
Matrix* quarter() const;
////Operators
//equality operator: two matrices are equal if they have the same dimensions and
//every corresponding element differs by no more than a small epsilon.
bool operator==(const Matrix& other) const;
//inequality operator
bool operator!=(const Matrix& other) const;
//overloaded output operator for printing the matrix
friend std::ostream& operator<<(std::ostream& out, const Matrix& m);
// Helper functions to allocate and deallocate the 2D array.
void allocate(unsigned int r, unsigned int c, double fill);
void deallocate();
};
#endif