Question: We want to expand our ability to compute with complex numbers in C + + , specifically to divide complex numbers. Write a main.cpp file

We want to expand our ability to compute with complex numbers in C++, specifically to divide complex numbers. Write a main.cpp file containing a main function that will read in three complex numbers, z1, z2, and z3 from the shell. Calculate z4 from z4=(z1+z2)/z3
, output it to the shell, and also output its absolute value.
This will require writing functions to add, divide, and take the absolute value of complex numbers. For the absolute value, use |z|= sqrt(a^2+ b^2)
. For dividing two complex numbers, use the following formula for z3= z1/z2
:a3=((a1*a2)+(b1*b2))/(a^2+b^2)
b3=((a1*a2)-(b1*b2))/(a^2+b^2)
where a
and b
are the real and imaginary components respectively. Add the required code to the attached files. Generic operations on complex numbers, such as add, divide, and absolute value, should be contained in Lab5_Ex2_complex.cpp. The recipes / prototypes should be added to Lab5_Ex2_complex.h. Note that this file already contains the definition of the complex struct. Finally, the Lab4_Ex2_main.cpp function should contain everything else - reading inputs from the shell, calculating z4 and its absolute value, and printing the results to the shell. Your program should be able to exactly reproduce the following output:
Enter real and imag parts of z1: -12
Enter real and imag parts of z2: 23
Enter real and imag parts of z3: 3-4
z4=-0.68+0.76j
|z4|=1.0198
When your code can reproduce this example, the TA or I will test your code with a different set of numbers.
Note: Do not use the built-in complex library in this exercise.
Note: Use the following files as a start for this exercise. There are comments for where to add code. To compile the project with these files, run g++ Lab5_Ex2_*.cpp to create the executable. This will avoid name conflicts with the previous exercise.

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 Programming Questions!