Concept explainers
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.
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:
Want to see more full solutions like this?
Chapter 10 Solutions
Absolute C++
Additional Engineering Textbook Solutions
Management Information Systems: Managing The Digital Firm (16th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Electric Circuits. (11th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
SURVEY OF OPERATING SYSTEMS
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- Problem of checking Compile errors Runtime errors ======== } ng; } You have the following IQueue interface. Implement a Queue class derived from IQueue. You can use STL containers discussed in class, such as vector, queue, stack, deque, map. #include using namespace std; class IQueue { public: }; virtual void Enqueue(int val) virtual int Dequeue() = 0; virtual int Size() const = 0; int main() = 0; { Queue q; ===== } cout << q.Size() << endl; q. Enqueue(10); q.Enqueue(20); q. Enqueue(30); cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; ==== ====arrow_forwardlogicarrow_forwardQ1: For the Figure Below if the input to the first tank is step with magnitude 2 find 1. What type of relation between (tanks 1 and 2) and tank 3 2. Initial real value of H2, if the steady state value is 10 3. Final Value of H3 4. H1 at t=1.5 5. For the system tank1 and tank 2 only which case is applied to them (overdamping, underdamped or critically damping) A₁=1 A₂=1 Tank 1 R₁ = 2 * Tank 2 R₁₂=2 A3=0.5 hy R₁=4 Tank 3arrow_forward
- Please original work Talk about the most common challenges encountered in a data warehouse What are some creative ways to overcome those challenges What is for one real world example where your method would be effective Please cite in text references and add weblinksarrow_forwardWhat is the differences between mobile website navigation and traditional website navigation?arrow_forwardPoint 10:26 Explain P 10:26 10:25arrow_forward
- A Solve each member force of the truss shown below. F 30 kN E 30 kN 3 m 3 m D C B 4 m 4 m 4 m 4 marrow_forward5:11 PM Fri Dec 13 Tt Problem 8 should be answered as a triple quoted comment after the code PROBLEM G. Put your name and assignment information here. The Bells Edgar Allen Poe HEAR the sledges with the bells. Siver bells! What a world of merriment their melody foretels How they tinkle, tinkle, tinkle, In the icy air of night While the stars, that oversprinkle All the heavens, seem to twinkle With a crystalline delight Keeping time, time, time. In a sort of Runic rhyme. To the tintinnabulation that so musically wells From the bells, bells, bells, bells, Bells, bells, bells From the jingling and the tinkling of the bells Hear the mellow wedding bels Golden bells! What a world of happiness their harmony foretells Through the balmy air of night How they ring out their delight! From the molten-golden notes. And all in tune. What a liquid ditty floats To the turtle-dove that stens, while she gloats On the moon! Oh, from out the sounding cells. What a gush of euphony voluminously wells! How…arrow_forward1. Could you see yourself making a career out of being a System Analyst? why? 2. What is one question you have about being a system analyst still or the system analysis and design process?arrow_forward
- Please, don't include lots of explanation.arrow_forwardI need help writing the recursive function called powerlist that computes the power of n (given as a parameter) of the numbers in a list. I need to use map. An example of what I have to do could be powerlist([6,4,3],5)=[6^5,4^5,3^5].arrow_forwardCan you show me how to redesign the Milwaukee Rep website using HTML and CSS using Sublime Text?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education