Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the code: #include #include #include math.h using namespace std; typedef double mattype; #define myfabs(x) fabs(x) class Matrix { private: // all data is

image text in transcribed

This is the code:

#include #include #include "math.h" using namespace std;

typedef double mattype; #define myfabs(x) fabs(x)

class Matrix { private: // all data is private mattype **m; int NR, NC; public: Matrix() { NR = 0; NC = 0; m = 0; } //Constructor Matrix(int rows, int cols) { allocate(rows, cols); } //Constructor Matrix(const Matrix &a); // Constructor ~Matrix() { unallocate(); } //Destructor void allocate(int rows, int cols); void unallocate(); int getRows() { return NR; } int getCols() { return NC; } void zero(); void set(mattype val, int rows, int cols) { m[rows][cols] = val; } mattype get(int rows, int cols) { return m[rows][cols]; } int gaussjordan(); int invert(); void addcolumns(int cols); // Add columns for I-mat void removecolumns(int cols); // Remove columns of I-mat Matrix &operator=(const Matrix &a); Matrix operator+(const Matrix &a); Matrix operator-(const Matrix &a); Matrix operator*(const Matrix &a); Matrix operator*(const mattype &a); // Scalar multiply by "a" Matrix operator/(const mattype &a); // Scalar divide by "a" Matrix operator!(void); // Unary operation to "invert" Matrix operator~(void); // Unary operation to "solve by gaussjordan" };

void Matrix::allocate(int rows, int cols) { if (rows>0 && cols>0) { NR = rows; NC = cols; m = new mattype *[NR]; for (int i = 0; i

void Matrix::unallocate() { if (m) { for (int i = 0; i

Matrix::Matrix(const Matrix &a) { allocate(a.NR, a.NC); for (int i = 0; i

void Matrix::zero() { if (!m) { cout

int Matrix::gaussjordan() { int i, j, k; mattype *t; int BIGrow;

for (j = 0; jmyfabs(m[BIGrow][j])) { BIGrow = i; } } if (myfabs(m[BIGrow][j])

// Normalization for (i = j + 1; i

// Elimination for (k = 0; k

void Matrix::addcolumns(int cols) { mattype *t; int i, j;

if (!m) { cout

for (i = 0; i

void Matrix::removecolumns(int cols) { mattype *t; int i, j;

if (!m) { cout

NC -= cols; for (i = 0; i

int Matrix::invert() { int i, j;

if (!m) { cout

addcolumns(j = NC); for (i = 0; i

i = gaussjordan(); removecolumns(j);

return i; }

ostream& operator

Matrix &Matrix::operator=(const Matrix &rhs) { if (this != &rhs) { if (NR != rhs.NR || NC != rhs.NC) { unallocate(); allocate(rhs.NR, rhs.NC); } for (int i = 0; i

Matrix Matrix::operator+(const Matrix &rhs) { Matrix t; if (NR != rhs.NR || NC != rhs.NC) { cout

Matrix Matrix::operator-(const Matrix &rhs) { Matrix t; if (NR != rhs.NR || NC != rhs.NC) { cout

Matrix Matrix::operator*(const Matrix &rhs) { Matrix t; if (NC != rhs.NR) { cout

Matrix Matrix::operator*(const mattype &a) { Matrix t(NR, NC); for (int i = 0; i

Matrix Matrix::operator/(const mattype &a) { Matrix t(NR, NC); if (mattype(a) == mattype(0)) { cout

Matrix Matrix::operator!(void) { Matrix t(*this); t.invert(); return t; }

Matrix Matrix::operator~(void) { Matrix t(NC, NR); for (int i = 0; i

class Regression { private: // all data is private double *x, *y; unsigned int n; public: void allocate(unsigned int num); void unallocate(); Regression(unsigned int num=0, double *xi=0, double *yi=0); Regression(const Regression &a); ~Regression(); Regression &operator=(const Regression &rhs); double linear(double &a, double &b); double poly(unsigned int degree, double *a); Regression operator+(const Regression &rhs); Regression operator-(const double &rhs); void print(ostream &os); };

void Regression::allocate(unsigned int num) { n = num; if (num == 0) { x = y = 0; return; } x = new double[n]; y = new double[n]; }

void Regression::unallocate() { if (x) delete[] x; if (y) delete[] y; }

Regression::Regression(unsigned int num, double *xi, double *yi) { allocate(num); for (unsigned int i = 0; i

Regression::Regression(const Regression &a) { allocate(a.n); for (unsigned int i = 0; i

Regression::~Regression() { unallocate(); }

Regression &Regression::operator=(const Regression &rhs) { if (this != &rhs) { unallocate(); allocate(rhs.n); for (unsigned int i = 0; i

double Regression::linear(double &a, double &b) { double sumx = 0, sumy = 0, sumx2 = 0, sumxy = 0; if (n == 0) { cout

double Regression::poly(unsigned int degree, double *av) { unsigned int i, j; double *sumxem, t;

if (n == 0) { cout

void Regression::print(ostream &os) { for (unsigned int i = 0; i

ostream& operator

Regression Regression::operator+(const Regression &rhs) { Regression t; unsigned int i,j; t.allocate(n + rhs.n); // Your code here return t; }

Regression Regression::operator-(const double &rhs) { Regression t; unsigned int i, j; t.allocate(n); // Your code here return t; }

int main(void) { unsigned int i, m; double e, a, b, *as; double x1[] = { 0, 1, 2, 3, 4 }; double y1[] = { 1, 4, -6, 5, -1 }; // change one of these numbers (keep them between 0 and +/-9) // y1[2] changed from 1 to -6 unsigned int n1 = sizeof(x1) / sizeof(x1[0]); double x2[] = { 5, 6, 7, 8, 9 }; double y2[] = { 2, 7, 1, -3, 2 }; // and change one of these numbers (keep them between 0 and +/-9) // y2[1] changed from 0 to 7 unsigned int n2 = sizeof(x2) / sizeof(x2[0]);

Regression data1(n1, x1, y1); Regression data2(n2, x2, y2);

cout

e = data1.linear(a, b); cout

m = 4; // set your order here //order changed from 2 to 4 as = new double[m + 1]; e = data1.poly(m, as);

cout

delete[] as;

cout

e = data2.linear(a, b); cout

m = 5; // set your order here //order changed from 2 to 5 as = new double[m + 1]; e = data2.poly(m, as);

cout

delete[] as;

return 0; }

2. ADDING TO THE DATA SETS For the regression class our data is stored dynamically in the object upon construction. We therefore cannot add new data to the object unless we facilitate a way. We will do this by overloading the +" operator to add the contents of two objects together to form a new one. The template of this function has already been provided; it is located just before the current main function. Inside it we create a new object "t" and set the number of points to be "n rhs.n". We are essentially writing the code for "t-*this + rhs". Everything you reference in this function without a preceding object name is referring to the first addend, the current object, whereas everything beginning with rhs." s referring to the second addend. You need to code a loop which will copy the "X" and "y" contents of the current object to the t" object (the sum). Then you must copy the "X" and y" contents of the "rhs" object to "t" while remembering to place it after the last one from the previous copy. Use your provided variables "i" and "j to do the work. You can look at the how we added columns in Matrix class as an example. Write the newly added code for the "operator+" function below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions