Create a class complex: In our implementation each complex has an imaginery part
ID: 3869869 • Letter: C
Question
Create a class complex: In our implementation each complex has an imaginery part and a real part that are integers in range [1 10].
Our goal is to have a bag of complex numbers with size 100.
The problem is: To search for a specific complex number we have to match both real and imaginary parts that is more time consuming than a simple integer, especialy when the range of imaginary and real numbers is only 10 numbers. The problem gets worst if the type of object that you are storing in the bag is more difficult to compare.
Suggest a strategy to overcome this problem by linear increase in the memory. Implement your strategy for the complex number.
You should have complex.h, complex.cpp, bag.cpp and main.cpp files.
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. I expect you insert at least 50 numbers into your bag.
Thank you
Explanation / Answer
// 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 ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.