Question
Hello, this is for C++. I can't figure out how to finish my .cpp file. These are the instructions for the assignemnt: Write a class
Hello, this is for C++. I can't figure out how to finish my .cpp file.
These are the instructions for the assignemnt:
Write a class called Complex that encapsulates a complex number with the following functionality:
-It should have two double variables for the real and imaginary coefficients of the number.
-Make a constructor that takes two double values for the real and imaginary coefficients of the number.
-Make a length method that returns the distance of the number from the origin (length(a+bi) = sqrt(aa + bb))
-Overload the +, =, and * operators for your class to operate properly on complex numbers.
-Overload the >> operator so you can print out complex numbers.
Write a program that will take the real and imaginary coefficients of a complex number, store them using your Complex number class, and then perform the Mandelbrot iteration on it.
-The program should terminate the iterations when the length of the z is greater than 2, or when 255 iterations are completed.
-When the program stops iterating, it should print how many iterations were performed along with the value of the iterate (z in the pseudocode below). For your output of the final z value, print 4 digits after the decimal point. Hint: use the following to print 4 digits after the decimal point (or use printf).
For a given complex number c, the iterated function is as follows:
f_0(c) = (0 + 0i)
f_n(c) = f_{n-1}(c)^2 + c
where the squaring in step 2 is done by complex number multiplication. It is known that if at any point during the iteration |f_n(c)|becomes greater than 2, that the function will eventually diverge. Thus, a common visualization technique is to iterate until |f_n(c)| is greater than two, up to some maximum number of iterations, say 255. Pseudocode for this is given below:
int mandelbrotIteration(Complex c, maxIterations) { Complex z = 0 + 0i; for i=0 to maxIerations z = z*z + c; if |z| > 2 return i; return maxIterations; }
----------------------------------------------------------------------------
This is my .h file
//Complex.h
#include
using namespace std;
class Complex{ public: double a; double b; double length; double getLength(); //constructor Complex(double r, double i); const Complex operator +(const Complex &C){ return Complex(this->a + C.a, this->b + C.b); } const Complex operator *(const Complex &C){ return Complex(this->a * C.a, this->b + C.b); } const Complex operator =(const Complex &C){ return Complex(this->a = C.a, this->b = C.b); } friend ostream & operator << (ostream& out, const Complex& C){ out << setprecision(4) << fixed << "(" << C.a << ", " << C.b << ")"; return out; } };
------------------------------------------------------------------
This is my .cpp file
//Complex.cpp
#include "Complex.h" #include
Complex::Complex(double, double){ };
---------------------------------------------------------
And this is my main .cpp file
//main.cpp
#include
#include "Complex.h"
int main(void) { double a, b; const int MAX_ITERATIONS = 255;
cout << "Enter the coefficients of a complex number: "; cin >> a; cin >> b;
Complex c(a,b); Complex r(0,0);
int i = 1; while (true) { r = r*r; r = r+c; //cout << i << " " << r << endl; if (r.a*r.a + r.b*r.b > 4) break; if (i == MAX_ITERATIONS) break; i++; }
cout << "Num iterations: " << i << endl; cout << "Final value: " << r << endl; }
The main method can't be changed and I'm pretty sure the .h file is at least mostly correct. I just need help finishing up the Complex.cpp file. Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started