CS13002 Programming and Data Structures | Section 3/C, Spring 2003--2004 |
Assignment 5 : Solution
The program
#include <stdio.h> #include <math.h> typedef struct { double real; double imag; } complex; typedef struct { double r; double theta; } polar; char *cprint ( complex z ) { char *c; c = (char *)malloc(100*sizeof(char)); sprintf(c,"%lf%ci%lf", z.real, (z.imag==0)?'+':((z.imag>0)?'+':'-'), (z.imag==0)?0:((z.imag>0)?z.imag:-(z.imag))); return(c); } polar ctop ( complex z ) { polar w; w.r = sqrt((z.real*z.real)+(z.imag*z.imag)); w.theta = (w.r==0) ? 0 : atan2(z.imag,z.real); return(w); } complex ptoc ( polar z ) { complex w; w.real = (z.r) * cos(z.theta); w.imag = (z.r) * sin(z.theta); return(w); } complex cadd ( complex z1 , complex z2 ) { complex z; z.real = z1.real + z2.real; z.imag = z1.imag + z2.imag; return(z); } complex csub ( complex z1 , complex z2 ) { complex z; z.real = z1.real - z2.real; z.imag = z1.imag - z2.imag; return(z); } complex cmul ( complex z1 , complex z2 ) { complex z; z.real = z1.real*z2.real - z1.imag*z2.imag; z.imag = z1.real*z2.imag + z1.imag*z2.real; return(z); } complex csqr ( complex z1 ) { complex z; z.real = z1.real*z1.real - z1.imag*z1.imag; z.imag = 2*z1.real*z1.imag; return(z); } complex cdiv ( complex z1 , complex z2 ) { complex z; double r; r = z2.real*z2.real + z2.imag*z2.imag; if (r == 0) { fprintf(stderr,"Error: Division by zero\n"); z.real = z.imag = 0; } else { z.real = (z1.real*z2.real + z1.imag*z2.imag) / r; z.imag = (z1.imag*z2.real - z1.real*z2.imag) / r; } return(z); } complex csqrt ( complex z1 ) { polar w; w = ctop(z1); w.r = sqrt(w.r); w.theta /= 2; return(ptoc(w)); } int findRoots ( complex *z1, complex *z2, complex a, complex b, complex c) { complex t, u; t = csqr(b); u = cmul(a,c); u.real *= 4; u.imag *= 4; t = csub(t,u); t = csqrt(t); u.real = a.real * 2; u.imag = a.imag * 2; (*z1).real = (*z2).real = -(b.real); (*z1).imag = (*z2).imag = -(b.imag); *z1 = cadd(*z1,t); *z1 = cdiv(*z1,u); *z2 = csub(*z2,t); *z2 = cdiv(*z2,u); } int main () { complex a, b, c; complex z1,z2; printf("Enter a (coefficient of x²) : "); scanf("%lf%lf",&(a.real),&(a.imag)); printf("Enter b (coefficient of x) : "); scanf("%lf%lf",&(b.real),&(b.imag)); printf("Enter c (constant term) : "); scanf("%lf%lf",&(c.real),&(c.imag)); printf("The input polynomial is :\n"); printf("(%s)x² + (%s)x + (%s)\n", cprint(a), cprint(b), cprint(c)); findRoots(&z1,&z2,a,b,c); printf("The two roots of this polynomial are :\n"); printf("%s and %s.\n", cprint(z1),cprint(z2)); }Output
Enter a (coefficient of x²) : 1 0 Enter b (coefficient of x) : 0 1 Enter c (constant term) : 1 0 The input polynomial is : (1.000000+i0.000000)x² + (0.000000+i1.000000)x + (1.000000+i0.000000) The two roots of this polynomial are : 0.000000+i0.618034 and -0.000000-i1.618034. Enter a (coefficient of x²) : -1 0 Enter b (coefficient of x) : -7 0 Enter c (constant term) : 8 0 The input polynomial is : (-1.000000+i0.000000)x² + (-7.000000+i0.000000)x + (8.000000+i0.000000) The two roots of this polynomial are : -8.000000+i0.000000 and 1.000000+i0.000000. Enter a (coefficient of x²) : -1 0 Enter b (coefficient of x) : -7 0 Enter c (constant term) : 7 0 The input polynomial is : (-1.000000+i0.000000)x² + (-7.000000+i0.000000)x + (7.000000+i0.000000) The two roots of this polynomial are : -7.887482+i0.000000 and 0.887482+i0.000000. Enter a (coefficient of x²) : 0 -1 Enter b (coefficient of x) : 0 -7 Enter c (constant term) : 0 8 The input polynomial is : (0.000000-i1.000000)x² + (0.000000-i7.000000)x + (0.000000+i8.000000) The two roots of this polynomial are : 1.000000+i0.000000 and -8.000000-i0.000000. Enter a (coefficient of x²) : -5 12 Enter b (coefficient of x) : 0 0 Enter c (constant term) : 0 0 The input polynomial is : (-5.000000+i12.000000)x² + (0.000000+i0.000000)x + (0.000000+i0.000000) The two roots of this polynomial are : 0.000000+i0.000000 and 0.000000+i0.000000. Enter a (coefficient of x²) : 1 0 Enter b (coefficient of x) : 0 0 Enter c (constant term) : -5 12 The input polynomial is : (1.000000+i0.000000)x² + (0.000000+i0.000000)x + (-5.000000+i12.000000) The two roots of this polynomial are : 3.000000-i2.000000 and -3.000000+i2.000000. Enter a (coefficient of x²) : 1 0 Enter b (coefficient of x) : 6 4 Enter c (constant term) : 5 12 The input polynomial is : (1.000000+i0.000000)x² + (6.000000+i4.000000)x + (5.000000+i12.000000) The two roots of this polynomial are : -3.000000-i2.000000 and -3.000000-i2.000000. Enter a (coefficient of x²) : 0 0 Enter b (coefficient of x) : 0 -7 Enter c (constant term) : 8 0 The input polynomial is : (0.000000+i0.000000)x² + (0.000000-i7.000000)x + (8.000000+i0.000000) Error: Division by zero Error: Division by zero The two roots of this polynomial are : 0.000000+i0.000000 and 0.000000+i0.000000. Enter a (coefficient of x²) : -1.2345 1.2345 Enter b (coefficient of x) : -2.3456 -2.3456 Enter c (constant term) : 3.4567 -3.4567 The input polynomial is : (-1.234500+i1.234500)x² + (-2.345600-i2.345600)x + (3.456700-i3.456700) The two roots of this polynomial are : -1.377513-i0.950020 and 1.377513-i0.950020. Enter a (coefficient of x²) : -12345 23456 Enter b (coefficient of x) : -23456 -34567 Enter c (constant term) : 34567 -45678 The input polynomial is : (-12345.000000+i23456.000000)x² + (-23456.000000-i34567.000000)x + (34567.000000-i45678.000000) The two roots of this polynomial are : -0.967105-i0.633814 and 1.708995-i0.756646.