Complete the complex number class and confirm it by displaying the result: z1z2
ID: 2247000 • Letter: C
Question
Complete the complex number class and confirm it by displaying the result:
z1z2 + (z1)/(z2-z1) = -14.1834-0760117i for z1= 1.4 +2. 1i and z2 -3.2+4.6i
using cout and the insertion operator <<.
Create an array of 20 instances of the Complex class all with the same -1.2 imaginary part but with real parts varying between -1.5 and 0.5. Use the index-to-value mapping presented earlier to vary the real part for index values from 0 to 19. For each complex number, compute z2 + z and write its magnitude to a text file with the 20 magnitudes on a single row separated by spaces with a single endl at the end. Take the program created in the previous paragraph, and place it inside a loop iterating 24 times. Have the imaginary part of z depend on this loop with values between -1.2 and 1.2. This requires a 2-dimensional array, or matrix, of complex instances who size is 24x20. Again, write the magnitudes of each z2 + z to a text file where each row in the file corresponds to complex numbers with the same imaginary part. Load the text file into Excel by selecting spaces as delimiters. Select all the data and display as a surface plot, similar to the figure below
Explanation / Answer
Given below are the files needed by the program. The program generates a text file that can be viewed in Excel using a space as delimiter. Hope the answer helps. If it does, please don't forget to rate the answer. Thank you very much.
complex.h
#ifndef complex_h
#define complex_h
#include <iostream>
class complex
{
private :
double real;
double imaginary;
public :
complex();
complex(double real, double imag);
double getRealPart() const;
double getImaginaryPart() const;
complex operator +(const complex &other);
complex operator -(const complex &other);
complex operator /(const complex &other);
complex operator *(const complex &other);
friend std::ostream& operator << (std::ostream &out , const complex &cnum);
};
#endif /* complex_h */
complex.cpp
#include "complex.h"
complex::complex()
{
real = imaginary = 0;
}
complex::complex(double real, double imag)
{
this->real = real;
this->imaginary = imag;
}
double complex::getRealPart() const
{
return real;
}
double complex::getImaginaryPart() const
{
return imaginary;
}
complex complex::operator +(const complex &other)
{
return complex(real + other.real, imaginary + other.imaginary);
}
complex complex::operator -(const complex &other)
{
return complex(real - other.real, imaginary - other.imaginary);
}
complex complex::operator /(const complex &other)
{
//given 2 complex numbers a+bi and c+di,
// the result of division (a+bi)/(c+di) has
// resultant real part = (ac + bd)/ (c^2+d^2)
//resultant imaginary part = (bc - ad)/ (c^2+d^2)
double denominator = (other.real * other.real) + (other.imaginary * other.imaginary);
//calculate the real and imaginary parts for hte result
double real1 = (real * other.real + imaginary * other.imaginary) / denominator;
double imag1 = (imaginary * other.real - real * other.imaginary) / denominator;
return complex(real1, imag1);
}
complex complex::operator *(const complex &other)
{
//given 2 complex numbers a+bi and c+di,
// the real part of their product is ac-bd
// the imaginary part of their product is ad+bc
double real1 = real * other.real - imaginary * other.imaginary;
double imag1 = real * other.imaginary + imaginary * other.real;
return complex(real1, imag1);
}
std::ostream& operator << (std::ostream &out , const complex &cnum)
{
out << cnum.getRealPart();
if(cnum.getImaginaryPart() != 0)
{
if(cnum.getImaginaryPart() < 0)
out << cnum.getImaginaryPart() << "i";
else
out << "+" << cnum.getImaginaryPart() << "i";
}
return out;
}
complextest.cpp
#include "complex.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//create 2 complex numbers z1 and z2
complex z1(1.4, 2.1);
complex z2(-3.2, 4.6);
cout << "z1 = " << z1 << endl;
cout << "z2 = " << z2 << endl;
complex z3 = z1 * z2 + (z1 / (z2 - z1));
cout << "z3 = z1 * z2 + (z1 / (z2 - z1)) = " << z3 << endl;
string filename = "complex.txt";
ofstream outfile(filename.c_str());
if(!outfile.is_open())
{
cout << "could not open " << filename << " for output" << endl;
return 1;
}
//generate 20 complex numbers with same imaginary part -1.2, but different real parts
for(double real = -1.5; real <= 0.5; real += 0.1)
{
complex z = complex(real, -1.2); //same imaginary part but different real parts
complex result = z * z + z;
outfile << result << " ";
}
outfile << endl << endl;
complex cnumbers[24][20];
int i, j;
i = 0;
//generate a matrix of 24x20 complex numbers
for(double imag = -1.2; imag < 1.2; imag += 0.1, i++)
{
j = 0;
for(double real = -1.5 ; real < 0.5; real += 0.1, j++)
{
cnumbers[i][j] = complex(real, imag);
}
}
for(i = 0; i < 24; i ++)
{
for(j = 0; j < 20; j++)
{
complex z = cnumbers[i][j];
complex result = z * z + z;
outfile << result << " ";
}
outfile << endl;
}
outfile.close();
cout << "generated output file " << filename << " to be viewed in excel" << endl;
}
output file: complex.txt
-0.69+2.4i -0.88+2.16i -1.05+1.92i -1.2+1.68i -1.33+1.44i -1.44+1.2i -1.53+0.96i -1.6+0.72i -1.65+0.48i -1.68+0.24i -1.69-4.44089e-16i -1.68-0.24i -1.65-0.48i -1.6-0.72i -1.53-0.96i -1.44-1.2i -1.33-1.44i -1.2-1.68i -1.05-1.92i -0.88-2.16i
-0.69+2.4i -0.88+2.16i -1.05+1.92i -1.2+1.68i -1.33+1.44i -1.44+1.2i -1.53+0.96i -1.6+0.72i -1.65+0.48i -1.68+0.24i -1.69-4.44089e-16i -1.68-0.24i -1.65-0.48i -1.6-0.72i -1.53-0.96i -1.44-1.2i -1.33-1.44i -1.2-1.68i -1.05-1.92i -0.88-2.16i
-0.46+2.2i -0.65+1.98i -0.82+1.76i -0.97+1.54i -1.1+1.32i -1.21+1.1i -1.3+0.88i -1.37+0.66i -1.42+0.44i -1.45+0.22i -1.46-4.44089e-16i -1.45-0.22i -1.42-0.44i -1.37-0.66i -1.3-0.88i -1.21-1.1i -1.1-1.32i -0.97-1.54i -0.82-1.76i -0.65-1.98i
-0.25+2i -0.44+1.8i -0.61+1.6i -0.76+1.4i -0.89+1.2i -1+1i -1.09+0.8i -1.16+0.6i -1.21+0.4i -1.24+0.2i -1.25-4.44089e-16i -1.24-0.2i -1.21-0.4i -1.16-0.6i -1.09-0.8i -1-1i -0.89-1.2i -0.76-1.4i -0.61-1.6i -0.44-1.8i
-0.06+1.8i -0.25+1.62i -0.42+1.44i -0.57+1.26i -0.7+1.08i -0.81+0.9i -0.9+0.72i -0.97+0.54i -1.02+0.36i -1.05+0.18i -1.06-4.44089e-16i -1.05-0.18i -1.02-0.36i -0.97-0.54i -0.9-0.72i -0.81-0.9i -0.7-1.08i -0.57-1.26i -0.42-1.44i -0.25-1.62i
0.11+1.6i -0.08+1.44i -0.25+1.28i -0.4+1.12i -0.53+0.96i -0.64+0.8i -0.73+0.64i -0.8+0.48i -0.85+0.32i -0.88+0.16i -0.89-3.33067e-16i -0.88-0.16i -0.85-0.32i -0.8-0.48i -0.73-0.64i -0.64-0.8i -0.53-0.96i -0.4-1.12i -0.25-1.28i -0.08-1.44i
0.26+1.4i 0.07+1.26i -0.1+1.12i -0.25+0.98i -0.38+0.84i -0.49+0.7i -0.58+0.56i -0.65+0.42i -0.7+0.28i -0.73+0.14i -0.74-3.33067e-16i -0.73-0.14i -0.7-0.28i -0.65-0.42i -0.58-0.56i -0.49-0.7i -0.38-0.84i -0.25-0.98i -0.1-1.12i 0.07-1.26i
0.39+1.2i 0.2+1.08i 0.03+0.96i -0.12+0.84i -0.25+0.72i -0.36+0.6i -0.45+0.48i -0.52+0.36i -0.57+0.24i -0.6+0.12i -0.61-2.22045e-16i -0.6-0.12i -0.57-0.24i -0.52-0.36i -0.45-0.48i -0.36-0.6i -0.25-0.72i -0.12-0.84i 0.03-0.96i 0.2-1.08i
0.5+1i 0.31+0.9i 0.14+0.8i -0.01+0.7i -0.14+0.6i -0.25+0.5i -0.34+0.4i -0.41+0.3i -0.46+0.2i -0.49+0.1i -0.5-2.22045e-16i -0.49-0.1i -0.46-0.2i -0.41-0.3i -0.34-0.4i -0.25-0.5i -0.14-0.6i -0.01-0.7i 0.14-0.8i 0.31-0.9i
0.59+0.8i 0.4+0.72i 0.23+0.64i 0.08+0.56i -0.05+0.48i -0.16+0.4i -0.25+0.32i -0.32+0.24i -0.37+0.16i -0.4+0.08i -0.41-1.66533e-16i -0.4-0.08i -0.37-0.16i -0.32-0.24i -0.25-0.32i -0.16-0.4i -0.05-0.48i 0.08-0.56i 0.23-0.64i 0.4-0.72i
0.66+0.6i 0.47+0.54i 0.3+0.48i 0.15+0.42i 0.02+0.36i -0.09+0.3i -0.18+0.24i -0.25+0.18i -0.3+0.12i -0.33+0.06i -0.34-1.11022e-16i -0.33-0.06i -0.3-0.12i -0.25-0.18i -0.18-0.24i -0.09-0.3i 0.02-0.36i 0.15-0.42i 0.3-0.48i 0.47-0.54i
0.71+0.4i 0.52+0.36i 0.35+0.32i 0.2+0.28i 0.07+0.24i -0.04+0.2i -0.13+0.16i -0.2+0.12i -0.25+0.08i -0.28+0.04i -0.29-8.32667e-17i -0.28-0.04i -0.25-0.08i -0.2-0.12i -0.13-0.16i -0.04-0.2i 0.07-0.24i 0.2-0.28i 0.35-0.32i 0.52-0.36i
0.74+0.2i 0.55+0.18i 0.38+0.16i 0.23+0.14i 0.1+0.12i -0.01+0.1i -0.1+0.08i -0.17+0.06i -0.22+0.04i -0.25+0.02i -0.26-4.16334e-17i -0.25-0.02i -0.22-0.04i -0.17-0.06i -0.1-0.08i -0.01-0.1i 0.1-0.12i 0.23-0.14i 0.38-0.16i 0.55-0.18i
0.75+5.55112e-17i 0.56+4.996e-17i 0.39+4.44089e-17i 0.24+3.88578e-17i 0.11+3.33067e-17i -3.33067e-16+2.77556e-17i -0.09+2.22045e-17i -0.16+1.66533e-17i -0.21+1.11022e-17i -0.24+5.55112e-18i -0.25-1.2326e-32i -0.24-5.55112e-18i -0.21-1.11022e-17i -0.16-1.66533e-17i -0.09-2.22045e-17i 1.94289e-16-2.77556e-17i 0.11-3.33067e-17i 0.24-3.88578e-17i 0.39-4.44089e-17i 0.56-4.996e-17i
0.74-0.2i 0.55-0.18i 0.38-0.16i 0.23-0.14i 0.1-0.12i -0.01-0.1i -0.1-0.08i -0.17-0.06i -0.22-0.04i -0.25-0.02i -0.26+4.16334e-17i -0.25+0.02i -0.22+0.04i -0.17+0.06i -0.1+0.08i -0.01+0.1i 0.1+0.12i 0.23+0.14i 0.38+0.16i 0.55+0.18i
0.71-0.4i 0.52-0.36i 0.35-0.32i 0.2-0.28i 0.07-0.24i -0.04-0.2i -0.13-0.16i -0.2-0.12i -0.25-0.08i -0.28-0.04i -0.29+8.32667e-17i -0.28+0.04i -0.25+0.08i -0.2+0.12i -0.13+0.16i -0.04+0.2i 0.07+0.24i 0.2+0.28i 0.35+0.32i 0.52+0.36i
0.66-0.6i 0.47-0.54i 0.3-0.48i 0.15-0.42i 0.02-0.36i -0.09-0.3i -0.18-0.24i -0.25-0.18i -0.3-0.12i -0.33-0.06i -0.34+1.11022e-16i -0.33+0.06i -0.3+0.12i -0.25+0.18i -0.18+0.24i -0.09+0.3i 0.02+0.36i 0.15+0.42i 0.3+0.48i 0.47+0.54i
0.59-0.8i 0.4-0.72i 0.23-0.64i 0.08-0.56i -0.05-0.48i -0.16-0.4i -0.25-0.32i -0.32-0.24i -0.37-0.16i -0.4-0.08i -0.41+1.66533e-16i -0.4+0.08i -0.37+0.16i -0.32+0.24i -0.25+0.32i -0.16+0.4i -0.05+0.48i 0.08+0.56i 0.23+0.64i 0.4+0.72i
0.5-1i 0.31-0.9i 0.14-0.8i -0.01-0.7i -0.14-0.6i -0.25-0.5i -0.34-0.4i -0.41-0.3i -0.46-0.2i -0.49-0.1i -0.5+2.22045e-16i -0.49+0.1i -0.46+0.2i -0.41+0.3i -0.34+0.4i -0.25+0.5i -0.14+0.6i -0.01+0.7i 0.14+0.8i 0.31+0.9i
0.39-1.2i 0.2-1.08i 0.03-0.96i -0.12-0.84i -0.25-0.72i -0.36-0.6i -0.45-0.48i -0.52-0.36i -0.57-0.24i -0.6-0.12i -0.61+2.22045e-16i -0.6+0.12i -0.57+0.24i -0.52+0.36i -0.45+0.48i -0.36+0.6i -0.25+0.72i -0.12+0.84i 0.03+0.96i 0.2+1.08i
0.26-1.4i 0.07-1.26i -0.1-1.12i -0.25-0.98i -0.38-0.84i -0.49-0.7i -0.58-0.56i -0.65-0.42i -0.7-0.28i -0.73-0.14i -0.74+3.33067e-16i -0.73+0.14i -0.7+0.28i -0.65+0.42i -0.58+0.56i -0.49+0.7i -0.38+0.84i -0.25+0.98i -0.1+1.12i 0.07+1.26i
0.11-1.6i -0.08-1.44i -0.25-1.28i -0.4-1.12i -0.53-0.96i -0.64-0.8i -0.73-0.64i -0.8-0.48i -0.85-0.32i -0.88-0.16i -0.89+3.33067e-16i -0.88+0.16i -0.85+0.32i -0.8+0.48i -0.73+0.64i -0.64+0.8i -0.53+0.96i -0.4+1.12i -0.25+1.28i -0.08+1.44i
-0.06-1.8i -0.25-1.62i -0.42-1.44i -0.57-1.26i -0.7-1.08i -0.81-0.9i -0.9-0.72i -0.97-0.54i -1.02-0.36i -1.05-0.18i -1.06+4.44089e-16i -1.05+0.18i -1.02+0.36i -0.97+0.54i -0.9+0.72i -0.81+0.9i -0.7+1.08i -0.57+1.26i -0.42+1.44i -0.25+1.62i
-0.25-2i -0.44-1.8i -0.61-1.6i -0.76-1.4i -0.89-1.2i -1-1i -1.09-0.8i -1.16-0.6i -1.21-0.4i -1.24-0.2i -1.25+4.44089e-16i -1.24+0.2i -1.21+0.4i -1.16+0.6i -1.09+0.8i -1+1i -0.89+1.2i -0.76+1.4i -0.61+1.6i -0.44+1.8i
-0.46-2.2i -0.65-1.98i -0.82-1.76i -0.97-1.54i -1.1-1.32i -1.21-1.1i -1.3-0.88i -1.37-0.66i -1.42-0.44i -1.45-0.22i -1.46+4.44089e-16i -1.45+0.22i -1.42+0.44i -1.37+0.66i -1.3+0.88i -1.21+1.1i -1.1+1.32i -0.97+1.54i -0.82+1.76i -0.65+1.98i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.