This is my code that creates two matrices with pointers, I wonder if somebody co
ID: 3536668 • Letter: T
Question
This is my code that creates two matrices with pointers, I wonder if somebody could should me how to do it with a class using mutators, accessor, constructor and destructors. Also with you want to point into a function please use -> to learn how to use it
/*************************************************************
*This program use dynamic memory allocation for the 2D array,*
*where the size of the matrix and the elements of the matrix *
*will be provided by the user or read from a text file. The *
*program should allow the user to enter 2 matrices and *
*specify the operations to be performed on them. Include *
*functions like addition, subtraction and multiplication for *
*two matrices and the transpose of a matrix *
*************************************************************/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
//global variables declaration
int **ptrmatrixA;
int rowsA, colmsA;
int **ptrmatrixB;
int rowsB, colmsB;
void additionMatrices()
{
if(rowsA == rowsB && colmsA == colmsB)
{
for(int i = 0; i<rowsA; i++)
{
cout << "[";
for(int j =0; j<colmsA; j++)
{
cout << setw(5) << *(*(ptrmatrixA + i)+j) + *(*(ptrmatrixB +i)+j);
}
cout << "]" << ' ';
}
}
else
cout << "Bad Dimensions";
}
void subtractionMatrices()
{
if(rowsA == rowsB && colmsA == colmsB)
{
for(int i = 0; i<rowsA; i++)
{
for(int j =0; j<colmsA; j++)
{
cout << "[";
cout << setw(5) << *(*(ptrmatrixA + i)+j) - *(*(ptrmatrixB +i)+j);
}
cout << "]" << ' ';
}
}
else
cout << "Bad Dimensions";
}
void multiplicationMatrices()
{
if ( colmsA == rowsB)
{
for(int i = 0; i<rowsA; i++)
{
cout << "[ ";
for(int j = 0; j <colmsB; j++)
{
int sum =0;
for( int k = 0; k<colmsA; k++)
{
sum += *(*(ptrmatrixA +i) + k) * *(*(ptrmatrixB + k) +j);
}
cout << setw(5) << sum;
}
cout << "] ";
}
}
}
void transposeMatrices()
{
cout << "Trasnposed for Matrix 1: ";
for(int i=0; i<colmsA; i++)
{
cout << "[";
for(int j=0; j<rowsA; j++)
{
cout << setw(5) << *(*(ptrmatrixA + j) + i);
}
cout << "] ";
}
cout << "Trasnposed for Matrix 2: ";
for(int i=0; i<colmsB; i++)
{
cout << "[";
for(int j=0; j<rowsB; j++)
{
cout << setw(5) << *(*(ptrmatrixB + j) + i);
}
cout << "] ";
}
}
int main()
{
ptrmatrixA = new int *[rowsA];
ptrmatrixB = new int *[rowsB];
int answer;
cout << "Please enter how many rows for Matrix 1: ";
cin >> rowsA;
cout << "Please enter how many columns for Matrix 1: ";
cin >> colmsA;
for(int i =0; i<rowsA; i++)
*(ptrmatrixA +i) = new int[colmsA];
cout << "Now enter the values of Matrix 1: " << endl;
for(int i = 0; i <rowsA; i++)
{
for(int j = 0; j < colmsA; j++)
{
cout << "What is the value of (" << i << "," << j << "): ";
cin >> *(*(ptrmatrixA +i)+j);
}
}
cout << "Please enter how many rows for Matrix 2: ";
cin >> rowsB;
cout << "Please enter how many columns for Matrix 2: ";
cin >> colmsB;
for(int i =0; i<rowsB; i++)
*(ptrmatrixB +i) = new int[colmsB];
cout << "Now enter the values of Matrix 2: " << endl;
for(int i = 0; i <rowsB; i++)
{
for(int j = 0; j < colmsB; j++)
{
cout << "What is the value of (" << i << "," << j << "): ";
cin >> *(*(ptrmatrixB +i)+j);
}
}
//Keep repeating till exit operation
do
{
cout << "Please select one of the operations and press ENTER" << endl;
cout << "1. Matrix Addition" << endl;
cout << "2. Matrix Subtraction" << endl;
cout << "3. Matrix Multiplication" << endl;
cout << "4. Matrix Transpose" << endl;
cout << "5. Exit" << endl << endl;
cin >> answer;
switch(answer)
{
case 1:
system("cls");
additionMatrices();
break;
case 2:
system("cls");
subtractionMatrices();
break;
case 3:
system("cls");
multiplicationMatrices();
break;
case 4:
system("cls");
transposeMatrices();
break;
case 5:
cout << "Thank you for using the program. Exiting now..." << endl;
break;
default:
break;
}
}while(answer != 5);
// free the memory
for(int i=0; i<rowsA; i++)
delete [] *(ptrmatrixA +i);
delete []ptrmatrixA;
for(int j=0; j<rowsB; j++)
delete [] *(ptrmatrixB +j);
delete []ptrmatrixB;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
class MatrixType
{
public:
MatrixType();
MatrixType(int r,int c);
void MakeEmpty();
void SetSize(int rowsSize, int colSize);
void StoreItem(int item, int row, int col);
int getItem(int row,int col);
void Add(MatrixType otherOperand, MatrixType& result);
void Sub(MatrixType otherOperand, MatrixType& result);
void Mult(MatrixType otherOperand, MatrixType& result);
MatrixType transpose();
void Print();
bool AddSubCompatible(MatrixType otherOperand);
bool MultCompatible(MatrixType otherOperand);
int getNumRows();
int getNumCols();
private:
int** values;
int numRows;
int numCols;
};
MatrixType MatrixType::transpose()
{
MatrixType m(numRows,numCols);
for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
m.StoreItem(values[i][j],j,i);
return m;
}
MatrixType::MatrixType()
{
numRows=0;
numCols=0;
}
MatrixType::MatrixType(int r,int c)
{
numRows=r;
numCols=c;
values=new int*[numRows];
for(int i=0;i<numRows;i++)
values[i]=new int [numCols];
}
void MatrixType::MakeEmpty()
{
}
int MatrixType::getNumRows()
{
return numRows;
}
int MatrixType::getNumCols()
{
return numCols;
}
void MatrixType::SetSize(int rowsSize, int colSize)
{
numRows=rowsSize;
numCols=colSize;
values=new int*[numRows];
for(int i=0;i<numRows;i++)
values[i]=new int [numCols];
}
void MatrixType::StoreItem(int item, int row, int col)
{
values[row][col]=item;
}
int MatrixType::getItem(int row,int col)
{
return values[row][col];
}
bool MatrixType::AddSubCompatible(MatrixType otherOperand)
{
if(numRows==otherOperand.getNumRows() && numCols==otherOperand.getNumCols())
return true;
else
return false;
}
bool MatrixType::MultCompatible(MatrixType otherOperand)
{
if(numCols==otherOperand.getNumRows())
return true;
else
return false;
}
void MatrixType::Add(MatrixType otherOperand, MatrixType& result)
{
if(AddSubCompatible(otherOperand)==false)
{
cout<<"cannot add incomatible matrices ";
return;
}
for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
result.StoreItem(values[i][j]+otherOperand.getItem(i,j),i,j);
}
void MatrixType::Sub(MatrixType otherOperand, MatrixType& result)
{
if(AddSubCompatible(otherOperand)==false)
{
cout<<"cannot sub incomatible matrices ";
return;
}
for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
result.StoreItem(values[i][j]-otherOperand.getItem(i,j),i,j);
}
void MatrixType::Mult(MatrixType otherOperand, MatrixType& result)
{
int sum=0;
if(AddSubCompatible(otherOperand)==false)
{
cout<<"cannot multiply incomatible matrices ";
return;
}
for(int i=0;i<numRows;i++)
{
for(int j=0;j<otherOperand.getNumCols();j++){
sum=0;
for(int k=0;k<numCols;k++)
sum=sum+values[i][k]*otherOperand.getItem(k,j);
result.StoreItem(sum,i,j);
}
}
}
void MatrixType::Print()
{
for(int i=0;i<numRows;i++)
{
for(int j=0;j<numCols;j++)
cout<<values[i][j]<<" ";
cout<<endl;
}
}
int main()
{
MatrixType m1,m2,m3,m4,m5;
m1.SetSize(2,2);
m2.SetSize(2,2);
m3.SetSize(2,2);
m4.SetSize(2,2);
m5.SetSize(2,2);
m1.StoreItem(1,0,0);
m1.StoreItem(2,0,1);
m1.StoreItem(3,1,0);
m1.StoreItem(4,1,1);
m2.StoreItem(4,0,0);
m2.StoreItem(3,0,1);
m2.StoreItem(2,1,0);
m2.StoreItem(1,1,1);
m1.Add(m2,m3);
m1.Sub(m2,m4);
m1.Mult(m2,m5);
cout<<"Matrix 1 ";
m1.Print();
cout<<endl;
cout<<"Matrix 2 ";
m2.Print();
cout<<endl;
cout<<"result of addition ";
m3.Print();
cout<<endl;
cout<<"result of subtraction ";
m4.Print();
cout<<endl;
cout<<"result of multiplication ";
m5.Print();
cout<<endl;
cout<<"transpose of matrix 1 ";
m1.transpose().Print();
cout<<"transpose of matrix 2 ";
m2.transpose().Print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.