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

Write C++ code for the following questions: a. Define a class MyArray consisting

ID: 3771522 • Letter: W

Question

Write C++ code for the following questions:
a. Define a class MyArray consisting of two data members. One is an integer pointer for the dynamic integer array; another one is an integer stores the size of the dynamic integer array. Declare the default constructor, copy constructor, print function and destructor for the class.

b. Implement the constructors, destructor and print function that defined in the question a. above, just outside the class definition. The copy constructor should make a deep copy from a MyArray object. The print function will print out all elements of the array in one line with white space separated.

c. Implement a member function times for the class MyArray. The function return type is MyArray type. The funtion takes an integer as a parameter.The function uses the integer parameter multiply with every element in the array and save the new values in a temporary MyArray object. Finally the function returns the temporary MyArray object. The data members of the current object cannot be changed.

this is a c++ question,and I know how to do the a & b, just need help with c, thanks.

Explanation / Answer

There is some info missing so I would make some assumptions about the MyArray Class.
Probably array of integers will be initialized in the constructor of the class MyArray. The size is probably passed as a parameter to the constructor. So probably your code is something
like this

MyArray obj(5); // 5 will be the size of the array

Now comming to your question i:e part c) It says a member function times will take a parameter and this parameter will be multiplied to all members of the array and create a new object with the
updated values

so first of all we will create a new object of MyArray and initialize its integer array with size already set in this object (this member) and then update its array values.

here is the times function code

MyArray MyArray::timer(int z)
{
       int i=0;
       MyArray tobj(this->n);
       for(i=0;i<this->n;i++)
   {
       tobj.a[i]=a[i]*z;
      
   }
       return tobj;
   }

I hope I understood your question. Please inform for more assistance or clearfication.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote