Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 10, Problem 1PP

Reread the code in Display 10.9. Then, write a class TwoD that implements the two-dimensional dynamic array of doubles using ideas from this display in its constructors. You should have a private member of type pointer to double to point to the dynamic array, and two int (or unsigned int) values that are MaxRows and MaxColS.

You should supply a default constructor for which you are to choose a default maximum row and column sizes and a parameterized constructor that allows the programmer to set maximum row and column sizes.

Further, you should provide a void member function that allows setting a particular row and column entry and a member function that returns a particular row and column entry as a value of type double.

Remark: It is difficult or impossible (depending on the details) to overload ( ) so it works as you would like for two-dimensional arrays. So simply use accessor and mutator functions using ordinary function notation.

Overload the + operator as a friend function to add two two-dimensional arrays. This function should return the TwoD object whose ith row, jth column element is the sum of the ith row, jth column element of the left-hand operand TWOD object and the ith row, jth column element of the right-hand operand TwoD object.

Provide a copy constructor, an overloaded operator z, and a destructor.

Declare class member functions that do not change the data as const members.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program Plan:

  • Define class named as TwoD and declare the variables for rows and columns.
  • Declare a variable of pointer to double to point to the dynamic array.
  • Define the default constructor and parameterized constructor to initialise rows and columns.
  • Define the copy constructor that copies the values of one object array to another object array.
  • Define operator + function declared as friend that add two dimensional arrays and return the sum.
  • Use the setValue () function to set the values in the matrices m1 and m2 of double type.
  • Finally write the main function to test the class TwoD.

Program Description:The purpose of the program is to set the values of two matrices of double type and add them to print the third matrix using the class TwoD and its constructors.

Explanation of Solution

Program:

//header files
#include <iostream>
usingnamespacestd;
//Create Class TwoD
classTwoD
{
//Private Data members
private:
intMaxRows;
intMaxCols;
//Declare array
double&Twoarr;
//Access Specifier
public:
//Create Default Constructor
TwoD()
{
//chooses default rows and columns
MaxRows=10;
MaxCols=10;
Twoarr=newdouble*[MaxRows];
for(int it =0; it <MaxRows;++it)
{
Twoarr[it]=newdouble[MaxCols];
}
}
//Create parameterised Constructor for setting the rows and columns
TwoD(int rows,int cols)
{
//chooses Default rows and columns
MaxRows= rows;
MaxCols= cols;
Twoarr=newdouble*[MaxRows];
for(int it =0; it <MaxRows;++it)
{
Twoarr[it]=newdouble[MaxCols];
}
}
//included the copy constructor
TwoD(constTwoD&matrix)
{
//rows and columns assigned to the constructor object
MaxRows=matrix.MaxRows;
MaxCols=matrix.MaxCols;
Twoarr=newdouble*[MaxRows];
for(int it =0; it <MaxRows;++it)
{
Twoarr[it]=newdouble[MaxCols];
for(intjt=0;jt<MaxCols;++jt)
{
Twoarr[it][jt]=matrix.Twoarr[it][jt];
}
}
}
//Destructor
~TwoD()
{
for(int it =0; it <MaxRows;++it)
{
delete[]Twoarr[it];
}
deleteTwoarr;
}

//member function for setting the values
voidsetValue(int row,int col,doubleval)
{
Twoarr[row][col]=val;
}
//Friend function which overloads the "+" Operator
friendTwoDoperator+(TwoD&m1,TwoD&m2)
{
TwoD*m3 =newTwoD(m1.MaxRows, m1.MaxCols);
for(int it =0; it < m3->MaxRows;++it)
{
for(intjt=0;jt< m3->MaxCols;++jt)
{
//Performs addition
                m3->Twoarr[it][jt]= m1.Twoarr[it][jt]+ m2.Twoarr[it][jt];
}
}
return*m3;
}
//Void function to print the values
void print()
{
for(int it =0; it <MaxRows;++it)
{
for(intjt=0;jt<MaxCols;++jt)
{
cout<<Twoarr[it][jt]<<"";
}
cout<<endl;
}
}
};

intmain()
{
//declare variables
intmaxRows;
intmaxColumns;
//Getting inputs from the user
cout<<"Enter row Dimensions of the array:"<<endl;
cin>>maxRows;
cout<<"Enter Column Dimensions of the array:"<<endl;
cin>>maxColumns;
cout<<"&********************************"**lt;<endl;
cout<<"Echoing the two-dimensional Array"<<endl;
cout<<"&********************************"**lt;<endl;
//passing parameters to the class objects
TwoDm1(maxRows,maxColumns);
TwoDm2(maxRows,maxColumns);

for(int it =0; it <maxRows;++it)
{
for(intjt=0;jt<maxColumns;++jt)
{
m1.setValue(it,jt,0.2*(it +jt));
m2.setValue(it,jt,0.3*(it -jt));
}
}
//printing the results
cout<<"&***********"**lt;<endl;
cout<<"Matrix 1 "<<endl;
cout<<"&***********"**lt;<endl;
m1.print();
cout<<"&***********"**lt;<endl;
cout<<"Matrix 2"<<endl;
cout<<"&***********"**lt;<endl;
m2.print();
//performs the addition operation
TwoD m3 = m1 + m2;
cout<<"&***********"**lt;<endl;
cout<<"Addition "<<endl;
cout<<"&***********"**lt;<endl;
m3.print();
return0;
}

Explanation:

First, TwoD class is defined along with all the required variables and constructors. Then, from the main ()function, the instances of the class is called, the rows of the array and the column of the array are entered by the user.

The two dimensional array is echoed randomly. Two matrices of given order are generated. The sum of these two matrices are obtained and displayed on the output screen.

Output Screenshot:

Absolute C++, Chapter 10, Problem 1PP

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Create a Vector2D class with properties x and y and a proper constructor method. Plus, minus and multiplication with a scalar operators should be overloaded. Create a Body class with properties of pos, vel, mass and constructor(float Mass, Vector2D Pos, Vector2D Vel), addForce(Vector2D F) and Move(float dt) methods. Your program should create an array or collection of Bodies, at each time step (dt), calculate forces that bodies exert each other and apply accordingly. Finally, in a 2D space, bodies should move freely based on their properties and applied forces. You can use option buttons or another element to represent bodies and move them inside the form.
Create a class named Collection for which each object can hold integers. The class should have following two private data members1. An integer pointer named data that holds a reference of an array created dynamically according to the specified size. 2. A constant integer named size to hold the maximum size of the array. Provide the implementation of following constructors and a destructor1. A constructor which accepts an integer that represents the size of an array and initializes it to the "empty collection," i.e., a collection whose array representation contains all 0. 2. An additional constructor that receives an array of integers and the size of that array and uses the array to initialize a collection object. 3. A copy constructor to initialize a collection object with already existing object. 4. A destructor to free any memory resources occupied by the collection object. Provide following member functions for the common set operations 1. getSize returns the size of collection.2.…
One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector . This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following A private member variable called dynamicArray that references a dynamic array of type string. A private member variable called size that holds the number of entries in the array. A default constructor that sets the dynamic array to NULL and sets size to 0. A function that returns size . A function named addEntry that takes a string as input. The function should create a new dynamic array one element larger than dynamicArray , copy all elements from dynamicArray into the new array, add the new string onto the end of the new array, increment size, delete the old dynamicArray ,…

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
9.1: What is an Array? - Processing Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=NptnmWvkbTw;License: Standard Youtube License