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

USING C++: You have implemented a Complex class in the previous homework. In thi

ID: 3821891 • Letter: U

Question

USING C++:

You have implemented a Complex class in the previous homework. In this problem, you will
be implementing an Array class, with some additional features, such as dynamic memory
allocation and de ning the copy constructor and the assignment operator. Please refer to
Stephen Prata, Chapter 12 for details.
(a) The class must have the following data members:
An int for the array size.
A pointer to an int for the array elements.
(b) Memory management should be done dynamically using the appropriate new operator
in the constructor and the delete operator in the destructor.
(c) The class must also de ne the following:
A constructor
A copy constructor
Assignment operator
Addition operator
A method to sort the array
A method to display the array elements
Test the code for all of the above functionality.

This is the code that the question is reffering to, just need to add the above:

#include "stdafx.h"

#include <stdio.h>

#include <iostream>

#include <math.h>

using namespace std;

class complex {

  

public:

    double real;

    double imag;

    //Declarations:

    complex();

    complex(double x, double y);

    double Addreal(complex c1, complex c2);

    double Addimaginary(complex c1, complex c2);

    void display();

    double MagnitudeSq();

    double Magnitude();

    double Polar();

    complex Conjugate();

       //Overloading operators:

    complex operator+ (const complex & other);

    complex operator- (const complex & other);

    complex operator* (const complex & other);

    complex operator/ (const complex & other);

    bool operator== (const complex & right);

    bool operator!= (const complex & right);

};

complex::complex(){             

    real = 0;

    imag = 0;

}

complex::complex(double x, double y) {

    real = x;

    imag = y;

}

double complex::Addreal(complex c1, complex c2){//adds real part

    complex c3;

    c3.real = c1.real + c2.real;

    return c3.real;

}

double complex::Addimaginary(complex c1, complex c2){//adds imaginary part

    complex c3;

    c3.imag = c1.imag + c2.imag;

    return c3.imag;

}

void complex::display(){// displays result a+bi

    cout << real << " + " << imag << "i" <<endl;

    cout << ' ';

}

double complex::MagnitudeSq(){//gets the magnitude squared[used below]

    double magnSq = (real*real)+(imag*imag);

    return magnSq;

}

double complex::Magnitude(){//get the magnitude

    double magnSq;

    magnSq = MagnitudeSq();//[above]

    double magn = sqrt(magnSq);

    return magn;

}

double complex::Polar(){//produces polar form

    double pol=0, phaseA;

    double magn, a, b;

    magn = Magnitude();

    phaseA = atan(imag / real);

    a = magn * (cos(phaseA));

    b = magn * (sin(phaseA));

    cout << "The polar coordinates of complex: " << "(" << a << " , " << b << ")" << endl;

    return pol;

}

complex complex::Conjugate(){//conjugates the expression

    complex comp;

    comp.real = real;

    comp.imag = -1 * (imag);

   

    cout << "The conjugate of complex: " << comp.real << " + " << comp.imag << "i" << endl;

    return comp;

}

//Overload operator functions:

complex complex::operator+ (const complex & other){

    return complex(real+other.real , imag+other.imag);

}

complex complex::operator- (const complex & other){

    return complex(real-other.real , imag-other.imag);

}

complex complex::operator* (const complex & other){

    return complex(real*other.real , imag*other.imag);

}

complex complex::operator/ (const complex & other){

    return complex(real/other.real, imag/other.imag);

}

bool complex::operator== (const complex & right){

    if ((real == right.real) && (imag = right.imag))

        return true;

    else

        return false;

}

bool complex::operator!= (const complex & right){

    if ((real != right.real) && (imag != right.imag))

        return true;

    else

        return false;

}

int main(){

    using namespace std;

    complex c1, c2, c3, comp;

    double magn, pol;

    int s,p,t,z;

    //fixed given numbers:

    c1 = complex(1.9 , 4.2);

    c2 = complex(0.5 , 2.9);

   

    cout << "The real part of c1: " << c1.real << endl;

    cout << "The imaginary part of c1: " << c1.imag << endl;

    c1.display();

   

    cout << "The real part of c2: " << c2.real << endl;

    cout << "The imaginary part of c2: " << c2.imag << endl;

    c2.display();

    //user chooses desired output:

    while(1){

        cout << "(1)Real & Imaginary (2)Magnitude (3)Polar (4)Conjugate (5)Addition (6)Subtraction (7)Muliplication (8)Division (9)Comparison (using ==) (10)Comparison (using !=)" << endl;

        cin >> s;

        switch(s){

            case 1: // Addreal & Addimaginary

                c3.real = c3.Addreal(c1, c2);

                c3.imag = c3.Addimaginary(c1, c2);

                cout << "The complex number of c3: " << c3.real << " + " << c3.imag << "i" << endl;

                break;

            case 2: // Magnitude

                cout << " (1) c1 (2) c2 (3) c3" << endl;

                cin >> t;

               

                switch(t){

                    case 1:

                        magn = c1.Magnitude();

                        cout << "The magnitude of c1: " << magn << endl;

                        break;

                    case 2:

                        magn = c2.Magnitude();

                        cout << "The magnitude of c2: " << magn << endl;

                        break;

                    case 3:

                        magn = c3.Magnitude();

                        cout << "The magnitude of c3: " << magn << endl;

                        break;

                }

               

            case 3: // Polar

                cout << " (1) c1 (2) c2 (3) c3" << endl;

                cin >> z;

               

                switch(z){

                    case 1:

                        pol = c1.Polar();

                        break;

                    case 2:

                        pol = c2.Polar();

                        break;

                    case 3:

                        pol = c3.Polar();

                        break;

                }

            

            case 4: // Conjugate

                cout << " (1) c1 (2) c2 (3) c3" << endl;

                cin >> p;

               

                switch(p){

                    case 1:

                        comp = c1.Conjugate();

                        break;

                    case 2:

                        comp = c2.Conjugate();

                        break;

                    case 3:

                        comp = c3.Conjugate();

                        break;

                }

               

            case 5: // Addition

                c3=c1+c2;

                c3.display();

                break;

            case 6: // Subtraction

                c3=c1-c2;

                c3.display();

                break;

            case 7: // Multiplication

                c3=c1*c2;

                c3.display();

                break;

            case 8: // Division

                c3=c1/c2;

                c3.display();

                break;

            case 9: // Comparison (==)

                if(c1==c2){

                    cout << "Two complex numbers are the same" << endl;

                }

                             else{

                                    cout << "Two complex numbers are not the same" << endl;

                             }

                break;

            case 10: // Comparison (!=)

                if(c1!=c2){

                    cout << "Two complex numbers are not the same" << endl;

                }

                             else{

                                    cout << "Two complex numbers are the same" << endl;

                             }

                break;

            default://incase an unrecognized value is entered

                cout << "Value out of range, try again (1-10): " << endl;

         

        }

    }

   

    return 0;

}

Explanation / Answer

include<iostream>

using namespace std;

class Array
{
   int size;
   int *arr;
   public:
       Array(){}
       Array(int size);
       Array(Array &arr);
       void display();
       void sort();
       void addElements();
       Array& operator+(Array &arr1);
       void operator=(Array &arr );
       int& operator[](int i);
       int getSize(){
           return size;
       }
};

Array::Array(int s)
{
   size=s;
   arr= new int[size];
}

Array::Array(Array &a)
{
   size=a.size;
   arr= new int[size];
   for(int i=0;i<size;i++)
   {
       arr[i]=a[i];
   }
}

int& Array::operator[](int i)
{
if( i > size )
   {
cout << "Index out of bounds" <<endl;
return arr[0];
}
return arr[i];
}

void Array::addElements()
{
   cout<<"Add "<<size<<" elements to the array: ";
   for(int i=0;i<size;i++)
   {
       cin>>arr[i];
   }  
}

void Array::display()
{
   for(int i=0;i<size;i++)
   {
       cout<<arr[i]<<" ";
   }
   cout<<endl;
}

void Array::sort()
{
   int i,j,temp;
   for(i=1;i<size;i++)
   {
       temp=arr[i];
       for(j=i-1;j>=0;j--)
       {
           if(temp<arr[j])
           {
               arr[j+1]=arr[j];
           }
           else break;
       }
       arr[j+1]=temp;
   }
}

Array& Array::operator+(Array &arr1)
{
   Array a(size + arr1.size);
  
   int i;
   for(i=0;i<size;i++)
       a[i]=arr[i];
   for(i; i<(size + arr1.size);i++)
       a[i]=arr1[i-size];
   a.display();
   return a;
}

void Array::operator=(Array &arr1)
{
   size=arr1.size;
   for(int i=0;i<size;i++)
       arr[i]=arr1[i];
}

int main()
{
   int ch;
   cout<<"Creating an array ";
   int size;
   cout<<" Enter the size of the array: ";
   cin>>size;
   Array arr1(size);
   cout<<"Enter elements ";
   for(int i=0;i<arr1.getSize();i++)
       cin>>arr1[i];
      
   cout<<"Display array elements ";
   cout<<" Elements in the array are: ";
   arr1.display();
  
   cout<<"Sort array ";
   arr1.sort();
   cout<<" After sorting elements are: ";
   arr1.display();
  
   cout<<"Add two arrays to form one array ";
   cout<<" Enter the size of the array: ";
   cin>>size;
   Array arr2(size);
   cout<<" Enter elements: ";
   for(int i=0;i<arr2.getSize();i++)
       cin>>arr2[i];
   Array arr3;
   arr3=arr1+arr2;
   cout<<" After adding the two arrays the elements in the final array are: ";
   arr3.display();
  
   return 0;
}