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:

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:
abc
1i1
-1-78
-1-77
-i-7i8i
-5+12i00
10-5+12i
16+4i5+12i
0-7i8
-1.2345+1.2345i-2.3456-2.3456i3.4567-3.4567i
-12345+23456i-23456-34567i34567-45678i


Lab home