Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey all, So i created a program that performs matrix addition and subtraction. I

ID: 3624983 • Letter: H

Question

Hey all,
So i created a program that performs matrix addition and subtraction.

I had to compile this using Makefile
and input a txt file that contains a matrix.
It compiles but it shows all 0's as and it does not read the numbers from the txt file.

 

 

 

All input data must be obtained from the stdin prompt.  You can feed in the data from a file by using input redirection.

 

The input file will contain the number of rows, number of columns and matrix of numbers from the top left to bottom right.Help Please!

Here are my codes:

****************************************
input.txt
****************************************
5 5
1 2 3 4 5
5 4 3 2 1
6 7 8 9 0
0 9 8 7 6
2 2 2 2 2


****************************************
Matrix.h
****************************************

#ifndef MATRIX_H
#define MATRIX_H

class Matrix
{
private:
const int numRows, numCols;
int **data;

public:

//constructors and destructors
Matrix(int m, int n); //parametric constructor, creates matrix of size mxn and reads values from stdin
Matrix(const Matrix &M); //copy constructor
~Matrix(); //destructor

//accessors/mutators
int getValue(int i, int j) const; //returns data at (i, j)
void setValue(int i, int j, int value); //sets data at (i, j) to value

int getNumRows() const; //returns number of rows
int getNumCols() const; //returns number of columns
//setNumRows and setNumCols intentionally not implemented

//IO
void display() const; //displays data
void read(); //reads in matrix values from stdin (possibly redirected from file)

//Arithmetic
void operator += (const Matrix &M); //performs matrix addition between calling object and argument M, stores result in calling object
void operator -= (const Matrix &M); //performs matrix subtraction between calling object and argument M, stores result in calling object

};

#endif //MATRIX_H


****************************************
Matrix.cpp
****************************************
#include
#include
#include
#include "Matrix.h"

using namespace std;

/*
* Constructors and Destructors
*/

Matrix::Matrix(int m, int n):numRows(m), numCols(n){ //parametric constructor, creates matrix of size mxn and reads values from stdin

data= new int* [numRows];

for (int i =0; i < numRows; ++i){
data[i] = new int [numCols];
}

for (int i = 0; i < numRows; ++i){
for(int j =0; j < numCols; ++j){
data[i][j]=0;
}
}
}

Matrix::Matrix(const Matrix &M): numRows(M.numRows), numCols(M.numCols){ //copy constructor

data = new int* [numRows];

for(int i = 0; i < numRows; ++i){
data[i] = new int [numCols];
}
for(int i = 0; i < numRows; ++i){
for (int j =0; j < numCols; ++j){
data[i][j] = M.data[i][j];
}
}
}

Matrix::~Matrix(){ //destructor

for(int i = 0; i < numRows; ++i){
delete [] data[i];
}
delete [] data;
}

/*
* Accessors/mutators
*/

int Matrix::getValue(int i, int j) const{ //returns data at (i, j)



if((i >= 0) && (i < numRows) && (j >=0) && (j < numCols)){
return data[i][j];
}
else {
cout << "ERROR!! Out of bounds." << endl;
exit(1);
}
}

void Matrix::setValue(int i, int j, int value){ //sets data at (i, j) to value


if (i > -1 && i < numRows){
if(j > -1 && j < numCols){
data[i][j] = value;
}
}
else {
cout << "ERROR. Out of bounds. " << endl;
exit(1);
}
}

int Matrix::getNumRows() const{//returns number of rows

return(numRows);

}

int Matrix::getNumCols() const{

return(numCols);

} //returns number of columns
//setNumRows and setNumCols intentionally not implemented
//IO

void Matrix::display() const{ //displays data

for(int i = 0; i < numRows; ++i){
for(int j = 0; j < numCols; ++j){
cout << data[i][j];
}
cout << endl;
}
}

void Matrix::read(){ //reads in matrix values from stdin (possibly redirected from fi

for(int i = 0; i < numRows; ++i){
for(int j = 0; j < numCols; ++j){
cin >> data[i][j];
}
}
}

/*
* Arithmetic
*/

void Matrix::operator += (const Matrix &M){ //performs matrix addition between calling object and argument M, stores result in calling object

if ((numRows == M.numRows) && (numCols == M.numCols)){

for(int i=0; i < numRows; ++i){
for(int j = 0; j< numCols; ++j){
data[i][j] += M.data[i][j];
cout << data[i][j];
}
}
}
else {
cout << "ERROR!!! Unable to perform matrix addition." << cout;
exit(1);
}
}

void Matrix::operator -= (const Matrix &M){ //performs matrix subtraction between calling object and argument M, stores result in calling objec


if ((numRows == M.numRows) && (numCols == M.numCols)){

for(int i=0; i < numRows; ++i){
for(int j = 0; j< numCols; ++j){
data[i][j] -= M.data[i][j];
cout << data[i][j];
}
}
}
else {
cout << "ERROR!!! Unable to perform matrix addition." << cout;
exit(1);
}
}

****************************************
Main.cpp
****************************************
#include
#include "Matrix.h"

using namespace std;

/*
* Main Function
*/
int main(int argc, char **argv)
{

//Create two Matrix obejcts
int m,n;
cout << "Enter number of rows:" << endl;
cin >> m;
cout << "Enter number of columns:" << endl;
cin >> n;

Matrix A(m,n); //using parametric constructor to create object and read values from console
Matrix B (A); //using copy constructor to copy from A



/*
* Test +=
*/
cout << "-----------------------" << endl;
cout << "Testing the += operator" << endl;
cout << "-----------------------" << endl;

//Display the two matrices
cout << "A:" << endl;
A.display();
cout << "B:" << endl;
B.display();

//Perform Matrix addition
A += B;

//Display result of A+=B
cout << "A+=B:" << endl;
A.display();


/*
* Test -=
*/
cout << "-----------------------" << endl;
cout << "Testing the -= operator" << endl;
cout << "-----------------------" << endl;

//Display the two matrices
cout << "A:" << endl;
A.display();
cout << "B:" << endl;
B.display();

//Perform Matrix subtraction
B -= A;

//Display result of B-=A
cout << "B-=A:" << endl;
B.display();

//clean up and exit
cout << endl;
return(0);

}//end Main

_______________________________________________________________________

Explanation / Answer

SOLVED