#I have written this code for which one function is missing which I cant figure
ID: 3598800 • Letter: #
Question
#I have written this code for which one function is missing which I cant figure out, please help me to run this code. Thank you.
#include <iostream>
#include <cstdlib>
using namespace std;
typedef double* dptr;
class TwoD
{
public:
TwoD();
TwoD (int rows, int cols);
void set (int i, int j, double value);
double getValue (int rows, int cols);
friend const TwoD operator+ (const TwoD& arrA, const TwoD& arrB);
TwoD& operator = (const TwoD& rvalue);
TwoD (const TwoD& arr);
~TwoD();
int numOfRows();
int numOfColumns();
private:
int MaxRows;
int MaxCols;
dptr* a;
};
TwoD::TwoD():MaxRows(10), MaxCols(10)
{
a = new dptr[MaxRows];
for (int i = 0; i < MaxRows; i++)
a[i] = new double[MaxCols];
}
TwoD::TwoD (int rows, int cols):MaxRows(rows), MaxCols(cols)
{
a = new dptr[MaxRows];
for (int i = 0; i < MaxRows; i++)
a[i] = new double[MaxCols];
}
int TwoD::numOfRows()
{
return MaxRows;
}
int TwoD::numOfColumns()
{
return MaxCols;
}
void TwoD::set (int i, int j, double value)
{
a[i][j] = value;
}
double TwoD::getValue (int rows, int cols)
{
return a[rows][cols];
}
const TwoD operator + (const TwoD& arrA, const TwoD& arrB)
{
TwoD sumArr (arrA.MaxRows, arrB.MaxRows);
for (int i = 0; i < arrA.MaxRows; i++)
{
for (int j = 0; j < arrA.MaxCols; j++)
sumArr.a[i][j] = arrA.a[i][j] + arrB.a[i][j];
}
return sumArr;
}
TwoD::TwoD (const TwoD& arr) : MaxRows (arr.MaxRows), MaxCols (arr.MaxCols)
{
a = new dptr[MaxRows];
for (int i = 0; i < MaxRows; i++)
a[i] = new double[MaxCols];
for (int i = 0; i < MaxRows; i++)
for (int j = 0; j < MaxCols; j++)
a[i][j] = arr.a[i][j];
}
TwoD& TwoD::operator = (const TwoD& rvalue)
{
if (MaxRows != rvalue.MaxRows && MaxCols != rvalue.MaxCols)
{
for (int i = 0; i < MaxRows; i++)
delete[] a[i];
delete[] a;
a = new dptr[rvalue.MaxRows];
for (int i = 0; i < rvalue.MaxRows; i++)
a[i] = new double[rvalue.MaxCols];
}
MaxRows = rvalue.MaxRows;
MaxCols = rvalue.MaxCols;
for (int i = 0; i < MaxRows; i++)
{
for (int j = 0; j < MaxCols; j++)
a[i][j] = rvalue.a[i][j];
cout << endl;
}
return *this;
}
TwoD::~TwoD()
{
for (int i = 0; i < MaxRows; i++)
delete[] a[i];
delete[] a;
}
int main()
{
cout << " =================================== ";
cout << "Testing TwoD class: ";
cout << "=================================== ";
int d1, d2;
cout << "Enter the row and column dimensions of matrix1 ";
cin >> d1 >> d2;
TwoD matrix1(d1, d2);
cout << "Enter " << d1 << " rows of " << d2 << " doubles each ";
for (int i = 0; i < d1; i++)
for (int j = 0; j < d2; j++)
cin >> matrix1(i,j); // note: matrix1(i,j) is a function call. Which function?
cout << "Echoing matrix1: ";
for (int i = 0; i < d1; i++)
{
for (int j = 0; j < d2; j++)
cout << matrix1(i,j) << " ";
cout << endl;
}
cout << " Enter the row and column dimensions of matrix2 ";
cin >> d1 >> d2;
TwoD matrix2(d1, d2), matrix3;
cout << "Enter " << d1 << " rows of " << d2 << " doubles each ";
for (int i = 0; i < d1; i++)
for (int j = 0; j < d2; j++)
{
double element;
cin >> element;
matrix2.set(i, j, element); // another way of initializing the matrix
}
cout << "Echoing matrix2: ";
for (int i = 0; i < matrix2.numOfRows(); i++)
{
for (int j = 0; j < matrix2.numOfColumns(); j++)
cout << matrix2(i,j) << " ";
cout << endl;
}
cout << " Assigning matrix 1 to matrix 2 " << endl;
matrix2 = matrix1;
cout << "Displaying matrix2 resulted from the assignment. ";
for (int i = 0; i < matrix2.numOfRows(); i++)
{
for (int j = 0; j < matrix2.numOfColumns(); j++)
cout << matrix2(i,j) << " ";
cout << endl;
}
cout << " Assigning matrix 2 to matrix 3 " << endl;
matrix3 = matrix2;
cout << "Displaying matrix3 resulted from the assignment. ";
for (int i = 0; i < matrix3.numOfRows(); i++)
{
for (int j = 0; j < matrix3.numOfColumns(); j++)
cout << matrix3(i,j) << " ";
cout << endl;
}
TwoD matrix4 = matrix2 + matrix3;
cout << " Displaying matrix4 that is the sum of matrix2 + matrix3 ";
for (int i = 0; i < matrix4.numOfRows(); i++)
{
for (int j = 0; j < matrix4.numOfColumns(); j++)
cout << matrix4(i,j) << " ";
cout << endl;
}
matrix4 = matrix4 + matrix2 + matrix3;
cout << " Displaying matrix4 that is the sum of matrix4 + matrix2 + matrix3 ";
for (int i = 0; i < matrix4.numOfRows(); i++)
{
for (int j = 0; j < matrix4.numOfColumns(); j++)
cout << matrix4(i,j) << " ";
cout << endl;
}
system("pause");
return 0;
}
#Output for this program
/*
===================================
Testing TwoD class:
===================================
Enter the row and column dimensions of matrix1
2 3
Enter 2 rows of 3 doubles each
1.1 4 9.2
3 12 5
Echoing matrix1:
1.1 4 9.2
3 12 5
Enter the row and column dimensions of matrix2
3 2
Enter 3 rows of 2 doubles each
1 3
5 7
2 4
Echoing matrix2:
1 3
5 7
2 4
Assigning matrix 1 to matrix 2
Displaying matrix2 resulted from the assignment.
1.1 4 9.2
3 12 5
Assigning matrix 2 to matrix 3
Displaying matrix3 resulted from the assignment.
1.1 4 9.2
3 12 5
Displaying matrix4 that is the sum of matrix2 + matrix3
2.2 8 18.4
6 24 10
Displaying matrix4 that is the sum of matrix4 + matrix2 + matrix3
4.4 16 36.8
12 48 20
*/
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
// Renames double* to dptr
typedef double* dptr;
// Class TwoD definition
class TwoD
{
public:
//Prototype of member functions
TwoD();
TwoD (int rows, int cols);
void set (int i, int j, double value);
double getValue (int rows, int cols);
friend const TwoD operator+ (const TwoD& arrA, const TwoD& arrB);
TwoD& operator = (const TwoD& rvalue);
TwoD (const TwoD& arr);
~TwoD();
int numOfRows();
int numOfColumns();
private:
// Data member to store data
int MaxRows;
int MaxCols;
dptr* a;
};// End of class
// Default constructor for assigning maximum row and maximum column
// Default value is 10
TwoD::TwoD():MaxRows(10), MaxCols(10)
{
// Allocates memory to number of rows
a = new dptr[MaxRows];
// Loops till maximum rows
for (int i = 0; i < MaxRows; i++)
// Allocates memory for each column
a[i] = new double[MaxCols];
}// End of constructor
// Parameterize constructor
TwoD::TwoD (int rows, int cols):MaxRows(rows), MaxCols(cols)
{
// Allocates memory to number of rows
a = new dptr[MaxRows];
// Loops till maximum rows
for (int i = 0; i < MaxRows; i++)
// Allocates memory for each column
a[i] = new double[MaxCols];
}// End of constructor
// Function to return maximum number of rows
int TwoD::numOfRows()
{
return MaxRows;
}// End of function
// Function to return maximum number of columns
int TwoD::numOfColumns()
{
return MaxCols;
}// End of function
// Function to set data in given row and column position as parameter
void TwoD::set (int i, int j, double value)
{
a[i][j] = value;
}// End of function
// Function to return value specified in row and column position as parameter
double TwoD::getValue (int rows, int cols)
{
return a[rows][cols];
}// End of function
// Function to overload + operator
const TwoD operator + (const TwoD& arrA, const TwoD& arrB)
{
// Creates a temporary object of TwoD to store the result
TwoD sumArr (arrA.MaxRows, arrB.MaxCols);
// Loops till maximum number of rows
for (int i = 0; i < arrA.MaxRows; i++)
{
// Loops till maximum number of rows
for (int j = 0; j < arrA.MaxCols; j++)
// Addition process
sumArr.a[i][j] = arrA.a[i][j] + arrB.a[i][j];
}// End of loop
// Returns the object stores result
return sumArr;
}// End of function
// Parameterized constructor
TwoD::TwoD (const TwoD& arr) : MaxRows (arr.MaxRows), MaxCols (arr.MaxCols)
{
// Allocates memory to number of rows
a = new dptr[MaxRows];
// Loops till maximum number of rows
for (int i = 0; i < MaxRows; i++)
// Allocates memory for each column
a[i] = new double[MaxCols];
// Loops till maximum number of rows
for (int i = 0; i < MaxRows; i++)
// Loops till maximum number of columns
for (int j = 0; j < MaxCols; j++)
// Assigns values
a[i][j] = arr.a[i][j];
}// End of function
// Function to overload '=' operator
TwoD& TwoD::operator = (const TwoD& rvalue)
{
// Checks if maximum row is not equals to parameter object rvalue's maximum row
// and maximum column is not equals to parameter object rvalue's maximum column
if (MaxRows != rvalue.MaxRows && MaxCols != rvalue.MaxCols)
{
// Loops till maximum number of rows
for (int i = 0; i < MaxRows; i++)
// Release memory
delete[] a[i];
delete[] a;
// Allocates memory for for
a = new dptr[rvalue.MaxRows];
// Loops till maximum number of rows
for (int i = 0; i < rvalue.MaxRows; i++)
// Allocates memory for each column
a[i] = new double[rvalue.MaxCols];
}// End of if
// Assigns row and column values to maximum rows and maximum columns
MaxRows = rvalue.MaxRows;
MaxCols = rvalue.MaxCols;
// Loops till maximum number of rows
for (int i = 0; i < MaxRows; i++)
{
// Loops till maximum number of columns
for (int j = 0; j < MaxCols; j++)
// Assigns values
a[i][j] = rvalue.a[i][j];
cout << endl;
}// End of loop
return *this;
}// End of function
// Destructor
TwoD::~TwoD()
{
// Loops till maximum number of rows
for (int i = 0; i < MaxRows; i++)
// Release memory
delete[] a[i];
delete[] a;
}// End of destructor
// Main function definition
int main()
{
cout << " =================================== ";
cout << "Testing TwoD class: ";
cout << "=================================== ";
int d1, d2;
double value;
// Accepts number of rows and columns from the user
cout << "Enter the row and column dimensions of matrix1 ";
cin >> d1 >> d2;
// Creates an object of TwoD class and sets row and column
TwoD matrix1(d1, d2);
cout << "Enter " << d1 << " rows of " << d2 << " doubles each ";
// Loops till number of rows entered by the user
for (int i = 0; i < d1; i++)
// Loops till number of columns entered by the user
for (int j = 0; j < d2; j++)
{
// Accepts data and stores it in array
cin >> value;
matrix1.set(i,j, value);
}// End of loop
// note: matrix1(i,j) is a function call. Which function?
cout << "Echoing matrix1: ";
// Loops till number of rows entered by the user
for (int i = 0; i < d1; i++)
{
// Loops till number of rows entered by the user
for (int j = 0; j < d2; j++)
// Displays the values
cout << matrix1.getValue(i,j) << " ";
cout << endl;
}// End of loop
// Accepts number of rows and columns from the user
cout << " Enter the row and column dimensions of matrix2 ";
cin >> d1 >> d2;
// Creates two object of TwoD class and sets row and column for matrix2 object
// and default values for matrix3 object
TwoD matrix2(d1, d2), matrix3;
cout << "Enter " << d1 << " rows of " << d2 << " doubles each ";
// Loops till number of rows entered by the user
for (int i = 0; i < d1; i++)
for (int j = 0; j < d2; j++)
{
double element;
cin >> element;
matrix2.set(i, j, element); // another way of initializing the matrix
}// End of loop
cout << "Echoing matrix2: ";
// Loops till maximum number of rows
for (int i = 0; i < matrix2.numOfRows(); i++)
{
// Loops till maximum number of columns
for (int j = 0; j < matrix2.numOfColumns(); j++)
cout << matrix2.getValue(i,j) << " ";
cout << endl;
}// End of loop
cout << " Assigning matrix 1 to matrix 2 " << endl;
matrix2 = matrix1;
cout << "Displaying matrix2 resulted from the assignment. ";
// Loops till maximum number of rows
for (int i = 0; i < matrix2.numOfRows(); i++)
{
// Loops till maximum number of columns
for (int j = 0; j < matrix2.numOfColumns(); j++)
cout << matrix2.getValue(i,j) << " ";
cout << endl;
}// End of loop
cout << " Assigning matrix 2 to matrix 3 " << endl;
matrix3 = matrix2;
cout << "Displaying matrix3 resulted from the assignment. ";
// Loops till maximum number of rows
for (int i = 0; i < matrix3.numOfRows(); i++)
{
// Loops till maximum number of columns
for (int j = 0; j < matrix3.numOfColumns(); j++)
cout << matrix3.getValue(i,j) << " ";
cout << endl;
}// End of loop
TwoD matrix4 = matrix2;
matrix4 = matrix2 + matrix3;
// Loops till maximum number of rows
for (int i = 0; i < matrix4.numOfRows(); i++)
{
// Loops till maximum number of columns
for (int j = 0; j < matrix4.numOfColumns(); j++)
cout << matrix4.getValue(i,j) << " ";
cout << endl;
}// End of loop
matrix4 = matrix4 + matrix2 + matrix3;
cout << " Displaying matrix4 that is the sum of matrix4 + matrix2 + matrix3 ";
// Loops till maximum number of rows
for (int i = 0; i < matrix4.numOfRows(); i++)
{
// Loops till maximum number of columns
for (int j = 0; j < matrix4.numOfColumns(); j++)
cout << matrix4.getValue(i,j) << " ";
cout << endl;
}// End of loop
system("pause");
return 0;
}// End of main function
Sample Run:
===================================
Testing TwoD class:
===================================
Enter the row and column dimensions of matrix1
2
3
Enter 2 rows of 3 doubles each
1.1
4
9.2
3
12
5
Echoing matrix1:
1.1 4 9.2
3 12 5
Enter the row and column dimensions of matrix2
3
2
Enter 3 rows of 2 doubles each
1
3
5
7
2
4
Echoing matrix2:
1 3
5 7
2 4
Assigning matrix 1 to matrix 2
Displaying matrix2 resulted from the assignment.
1.1 4 9.2
3 12 5
Assigning matrix 2 to matrix 3
Displaying matrix3 resulted from the assignment.
1.1 4 9.2
3 12 5
2.2 8 18.4
6 24 10
Displaying matrix4 that is the sum of matrix4 + matrix2 + matrix3
4.4 16 36.8
12 48 20
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.