CS13002 Programming and Data Structures | Section 3/C, Spring 2003--2004 |
Assignment 5
The exercise
Define a structure for storing complex variables. It should consist of two floating point (float or double) fields standing for the real and imaginary parts of a complex number. Define another structure to represent complex numbers in polar coordinates. Unless otherwise stated, a complex variable henceforth means one in its coordinate (real,imaginary) representation.
Click here to know what `structure' means and how to program structures.
Write the following functions on complex variables:
- A function for printing complex numbers in a neat format.
- A function that converts a complex number from coordinate to polar representation.
- A function that converts a complex number from polar to coordinate representation.
- A function that adds two complex numbers.
- A function that subtracts a complex number from another.
- A function that multiplies two complex numbers.
- A function that squares a complex numbers.
- A function that divides a complex number by another.
- A function that computes a square root of a complex number. (This is where polar coordinates may be helpful.)
Read three complex numbers a, b and c from the user. Use the above functions to compute and print the two roots of the polynomial
ax2+bx+c.You may use the Bhaskaracharya formula[-b ± V(b2-4ac)]/(2a)to compute square roots.Sample output
Enter a (coefficient of x²) : 1 0 Enter b (coefficient of x) : -2 0 Enter c (constant term) : 1 0 The input polynomial is : (1.000000+i0.000000)x² + (-2.000000+i0.000000)x + (1.000000+i0.000000) The two roots of this polynomial are : 1.000000+i0.000000 and 1.000000+i0.000000. Enter a (coefficient of x²) : 1 0 Enter b (coefficient of x) : 2 0 Enter c (constant term) : -3 0 The input polynomial is : (1.000000+i0.000000)x² + (2.000000+i0.000000)x + (-3.000000+i0.000000) The two roots of this polynomial are : 1.000000+i0.000000 and -3.000000+i0.000000. Enter a (coefficient of x²) : 2.1 -3.2 Enter b (coefficient of x) : -4.3 5.4 Enter c (constant term) : 6.5 -7.6 The input polynomial is : (2.100000-i3.200000)x² + (-4.300000+i5.400000)x + (6.500000-i7.600000) The two roots of this polynomial are : 0.830050+i1.423087 and 0.965854-i1.257900.
Test input
Show the output of your program on the following inputs:
a b c 1 i 1 -1 -7 8 -1 -7 7 -i -7i 8i -5+12i 0 0 1 0 -5+12i 1 6+4i 5+12i 0 -7i 8 -1.2345+1.2345i -2.3456-2.3456i 3.4567-3.4567i -12345+23456i -23456-34567i 34567-45678i