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

I posted this question before and I do not want the answer that is already on ch

ID: 3591662 • Letter: I

Question

I posted this question before and I do not want the answer that is already on chegg.

If you copy the answer that is already on chegg I will report you.

C++ question:

Create 5 files: main.cpp, bag.h, bag.cpp, complex.h, and complex.cpp

Create your own namespaces.

Create a class complex, which has a imaginary and real parts. The range is from 1 to 10 (integers).

The program should create random numbers each time we run the program.

The program should ask the user to input a complex like this 2 + 3i to be searched in the bag.

Full documentation and precondition and post condition is needed.

Code should be clean and the name of the identifiers should be appropriate to their actual meaning.

main.cpp contains the test of your work. The bag should have 100 numbers.

The expert Heisenberg asked to shared the answer that is already on chegg. The answer that I do not want.

So, here it is:

--------------------- Complex.h -------------------

// Class Complex

// Data types are real of type int and imag of type int

//

// Complex()

// Precondition : Default Constructor to initialize objects

//

// Complex(int , int)

// Precondition : Parametarized Constructor to initialize objects

//

// GETTER METHODS

//

// int getReal()

// Precondition : Return the real part of the Complex number

//

// int getImag()

// Precondition : Return the imag part of the Complex number

//

// SETTER METHODS

//

// void setReal(int)

// Precondition : set the real part of the Complex number to the given arguement

//

// void getImag(int)

// Precondition : set the imag part of the Complex number to the given arguement

//

#ifndef MY_COMPLEX

#define MY_COMPLEX

#include<iostream>

#include<cstdlib>

using namespace std;

namespace myComplex

{

    class Complex

    {

        int real;

        int imag;

       

        public:

           

            Complex();

            Complex(int, int);

            int getReal();

            int getImag();

            void setReal(int);

            void setImag(int);

    };

}

#endif

-----------------Complex.cpp------------------------

#include "Complex.h"

using namespace myComplex;

Complex::Complex()

{

    this->real = 0;

    this->imag = 0;

}

Complex::Complex(int real, int imag)

{

    this->real = real;

    this->imag = imag;

}

int Complex::getReal()

{

    return real;

}

int Complex::getImag()

{

    return imag;

}

void Complex::setReal(int real)

{

    this->real = real;

}

void Complex::setImag(int imag)

{

    this->imag = imag;

}

----------------------Bag.h--------------------

// Class Bag

// Data types are arr of type Complex and index of type int

//

// Bag()

// Precondition : Default Constructor to initialize objects

//

// BAG(int)

// Precondition : Parametarized Constructor to initialize objects

//

// NORMAL METHODS

//

// bool search(Complex)

// Precondition : Return true if the given element is found

//

#ifndef MY_BAG

#define MY_BAG

#include "Complex.cpp"

namespace myBag

{

    class Bag

    {

        // array of type Complex

        Complex *arr;

       

        // store the index of the last element in the array

        int index;

       

        public:

           

            // constructor

            Bag();

           

            // constructor

            Bag(int);

           

            // method to add Complex object to Bag

            void add(int , int);

           

            // method to search for a complex number in the bag

            bool search(Complex);

    };

}

#endif

----------------Bag.cpp------------------------

#include "Bag.h"

using namespace myBag;

Bag::Bag()

{

    this->arr = new Complex[10];

    this->index = 0;

}

Bag::Bag(int size)

{

    this->arr = new Complex[size];

    this->index = 0;

}

void Bag::add(int real, int imag)

{

    arr[index].setReal(real);

    arr[index].setImag(imag);

   

    //cout<<arr[index].getReal()<<" +"<<arr[index].getImag()<<"i added ";

   

    index++;

}

bool Bag::search(Complex ob)

{

    int i;

   

    for( i = 0 ; i < index ; i ++)

    {

        // if element is found

        if(ob.getReal() == this->arr[i].getReal() && ob.getImag() == this->arr[i].getImag())

            return true;

    }

   

    // if element is not found

    return false;

}

---------------main.cpp------------------

#include "Bag.cpp"

int main()

{

    Bag ob(50);

    int i;

   

    for(i=0;i<50;i++)

    {

        int real = rand() % 10 + 1;

        int imag = rand() % 10 + 1;

       

        ob.add(real, imag);

    }

   

    int r = rand() % 10 + 1;

    int im = rand() % 10 + 1;

   

    Complex temp(r, im);

   

    cout<<"Element to be searched : "<<r<<" + i"<<im<<" ";

   

    if(ob.search(temp))

        cout<<"Elemet is present in the bag ";

    else

        cout<<"Elemet is not present in the bag ";

}

Explanation / Answer

--------------------- Complex.h -------------------

#ifndef MY_COMPLEX
#define MY_COMPLEX
#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

namespace myComplex
{
    class Complex
    {

        int real;
        int img;
     

        public:

            Complex();
            Complex(int, int);
            int getReal();
            int getImg();
            void setReal(int);
            void setImg(int);
        double getMagnitude();
            void print();
            bool equals(Complex);
    };
}

#endif

-----------------Complex.cpp------------------------

#include "Complex.h"

using namespace myComplex;

Complex::Complex()
{
    real = rand()%10 + 1;
    img = rand()%10 + 1;
}

Complex::Complex(int real, int img)
{
    this->real = real;
    this->img = img;
}

int Complex::getReal()
{
    return real;
}

int Complex::getImag()

{
    return img;
}

void Complex::setReal(int real)
{
    this->real = real;
}

void Complex::setImag(int img)
{
    this->img = img;
}

double Complex::getMagnitude()
{
    return sqrt(real*real + img*img);
}

void Complex::print()
{
    cout << real << " + " << img << "i ";
}

boole Complex::equals(Complex &other)
{
   return real==other.getReal() && img==other.getImg();
}
----------------------Bag.h--------------------

#ifndef MY_BAG
#define MY_BAG
#include "Complex.cpp"

namespace myBag

{
    class Bag

    {

        // array of type Complex

        Complex *arr;
  
        // store the index of the last element in the array
        int count;

        public:1
            Bag();
            Bag(int);
            void add(int , int);
            bool search(Complex);
    };
}
#endif

----------------Bag.cpp------------------------

#include "Bag.h"

using namespace myBag;

Bag::Bag()
{
    arr = new Complex[10];
    count = 0;
}

Bag::Bag(int size)
{
    arr = new Complex[size];
    count = 0;
}

void Bag::add(int real, int imag)
{
    arr[index].setReal(real);
    arr[index].setImag(imag);

    //cout<<arr[index].print()<<std::endl;

    index++;
}

bool Bag::search(Complex ob)

{
    int i;
    for( i = 0 ; i < index ; i ++)
    {
        // if element is found
        if(arr[i].equals(ob))
            return true;
     }

    // if element is not found
    return false;
}

---------------main.cpp------------------

#include "Bag.cpp"

int main()

{
    Bag bag(100);
    int i;

    for(i=0;i<50;i++)
        bag.add();

    int r,im;

    cout << "Search complex number in the bag ";
    cout << "Enter real part: ";
    cin >> r;
    cout << "Enter imaginary part: ";
    cin >> im;

    Complex search(r, im);

    cout<<"Element to be searched : " << search.print() << " ";

    if(bag.search(temp))
        cout<<"Element is present in the bag ";
    else
        cout<<"Element is not present in the bag ";
}