Question: Part 1: Given the following class; write the implementation code of its member functions. class Complex { friend ostream& operator>(istream& in, Complex& theComplex); friend Complex

Part 1: Given the following class; write the implementation code of its member functions. class Complex { friend ostream& operator>(istream& in, Complex& theComplex); friend Complex operator+(const Complex& lhs, const Complex& rhs); friend Complex operator-(const Complex& lhs, const Complex& rhs); friend Complex operator*(const Complex& lhs, const Complex& rhs); friend Complex operator/(const Complex& lhs, const Complex& rhs); public: Complex(double re = 0.0, double im = 0.0); double getReal(void) const; // return real part double getImaginary(void) const; // return imaginary part void setReal(double re); // sets the real part void setImaginary(double im); // sets the imaginary part void convertStringToComplex(const string& complexString); private: double real; double imag; }; Complex numbers are added by adding the real and imaginary parts of the summands. That is to say, assuming a + bi is the first complex number and c + di is the second complex number: (a + bi) + (c + di) = (a + c) + (b + d)i Similarly, the subtraction is defined by: (a + bi) - (c + di) = (a - c) + (b - d)i The multiplication of the two complex numbers is defined by the following formula: (a + bi)(c + di) = (ac - bd) + (b - d)i The division of the two complex numbers is defined in terms of complex multiplication, which is described above, and real division. Where at least one of c and d is non-zero: Finally, two complex numbers are equal if (a == c) and (b == d). Create a program to test the class. For example:

Enter first complex number: 4.2+3.0i

Enter second complex number: 2.0+2.1i

No spaces between the real and imaginary number in each string. Make sure you read the entry at once as a string.

The output should test all the operators; for example, the addition operator will give the result: (4.2 + 3.0i) + (2.0 + 2.1i) = 6.2 + 5.1i

Please upload the following:

The class .cpp file

The main program

The class .h file

Output File

Part 2: For a random list of integers, the maximum number of comparisons required to find a target value by using the binary search is 2(1+ int(log2n)). This result can be tested experimentally. Modify the function binarySearch( ) to return the number of comparisons the algorithm executes in a successful search and the negative of the number of comparisons required for an unsuccessful search. We provide the prototype for binarySearch( ):

template int binarySearch(const T arr[], int first, int last, const T& target); Write a program that declares an integer array table of ARRSIZE integers and two integer variables sumBinSearchSuccess and sumBinSearchFail.

const int ARRSIZE = 50000; const int RANDOMVALUES = 100000, RANDOMLIMIT = 200000; int table[ARRSIZE]; int sumBinSearchSuccess = 0, sumBinSearchFail = 0, success = 0; After initializing table with ARRSIZE random integers in the range from 0 to RANDOMLIMIT - 1, apply the selection sort to table. In a loop that executes RANDOMVALUES times, generate a random target in the range from 0 to RANDOMLIMIT 1, and search for it in table using the modified binary search.

If the search is successful, increment the integer counter success, and increment sumBinSearchSuccess by the number of comparisons returned from binarySearch( ); otherwise, increment sumBinSearchFail by the negative of the number of comparisons returned from binarySearch( ). At the conclusion of the loop, output the following:

Empirical average case: sumBinSearchSuccess / static_cast (success) Empirical worst case: sumBinSearchFail / static_cast (RANDOMVALUES success)) Theoretical bound for worse case: 2.0 * (1.0 + int(log(static_cast(ARRSIZE)) / log(2.0)))

Please upload the following:

The class .cpp file

The main program

The class .h file

Output File

Study your results:

By how many iterations do the average and worse cases differ?

What is the difference between Empirical and Theoretical worst cases?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!