How to print the imaginary root of quadratic equation in c? - c

I need to print the imaginary root in the quadratic equation.
but when I execute my code the result shows me that the imaginary root is 0.00000i.
even I use to <complex.h> also same.
Can everybody help me to check the code that I bold?
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + sqrt( disc ) ) / 2 * a;
x2 = ( -b - sqrt( disc ) ) / 2 * a;
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
The output are shown as below:
Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0) execution time : 3.914 s
Press any key to continue.

You should print the roots as complex numbers using the computed real and imaginary parts. No need for <complex.h> nor complex types:
double xr = - b / ( 2 * a );
double ximg1 = -sqrt( -disc ) / (2 * a);
double ximg2 = -ximg1;
printf("x1 = %lf%+lfi, x2 = %lf%+lfi\n", xr, ximg1, xr, ximg2);

As you have used complex header file you just need to use the csqrt() instead of sqrt(),
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + csqrt( disc ) ) / 2 * a;//changed the function here
x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}

Complex types not needed
When code reaches the below, disc < 0. Find the square root of the negation.
// ximg1 = sqrt( disc ) / 2 * a;
ximg1 = sqrt( -disc ) / 2 * a; // Use -disc
// ximg2 = - sqrt( disc ) / 2 * a;
ximg2 = -ximg1; // Simply negate ximg1
xr = - b / ( 2 * a );
printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi",
// crealf(xr), cimagf(ximg1), cimagf(ximg2));
xr, ximg1, ximg2);
Tip: use "%e", it is more informative.
printf("xr = %le, ximg1 = %lei, ximg2 = %lei",
xr, ximg1, ximg2);
Bug
Instead of / 2 * a;, in various places, certainly you want / (2 * a);.

Related

Read the coefficients a,b,c of the quadratic equation ax^2+bx+c and print it roots nicely for imaginary roots print in x+iy form

#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}
Is this code alright for the given question, i had a bit confusion at that printing imaginary roots. could you please help
Use proper main prototype.
Use floating point numbers instead of integers
pow(b,2) == b*b
/ 2 * a -> / (2 * a)
int main(void) {
double a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
printf("\nThe quadratic equation is %f*x^2+%f*x+%f=0\n", a, b, c);
if (b*b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
printf("the roots of the equation are x1=%f,x2=%f\n", x1, x2);
}
else
{
double r = -b / (2*a);
double z = sqrt(fabs(b*b - 4 * a * c));
printf("the roots of the equation are x1 = %f + i%f, x2 = %f - i%f", r,z,r,z);
}
}
https://gcc.godbolt.org/z/Ys1s8bWY7

Finding Third Coordinate Of a Triangle

i have an assignment for Programming class.
i have finished 80 percent of it, i am not able to complete the last part.
We have to find the distance between the shore and the boat(Point C) And Coordinates Of The Boat (Point C).
We are given (X,Y) coordinates And Angles of Points A and B.
Given A(76,316) B(57,516) Angle A = 17° and B = 17° Angle.
So Far i found AB= 200.9 AC= 105.04 BC = 105.04 C = 146° And the distance between the shore and the boat d = 30.71.
I tried everything but i am not able to find the Coordinates of Point C (Boat).
The Output is also Given : d = 30.71, Coordinates of C = C(97.07,418.90)
Please help me to find coordinates of Point C.
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n",a,b,c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
Simpler approach to find d distance. We can express lengths of the left and right parts of base through d and cotangents
aa = d * ctg(alpha)
bb = d * ctg(beta)
aa + bb = l
d * (ctg(alpha) + ctg(beta)) = l
d = l / (ctg(alpha) + ctg(beta)) =
= l * sin(alpha) * sin(beta) / sin(alpha+beta)
Now you can find C coordinates using normalized vector ab and perpendicular component
abx = (b.x - a.x) / l
aby = (b.y - a.y) / l
aalen = d / tg(alpha)
c.x = a.x + abx * aalen - aby * d
c.y = a.y + aby * aalen + abx * d
There you go I have written some code for your question..
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Full Code:
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
get_coordinates(76,316,57,516,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n", a, b, c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Output:
0.30 0.30 2.55
AC Distance : 105.04 , BC distance : 105.04
Boat Distance : 30.71
Alpha Angle in Degrees = 17.00
Beta Angle in Degrees = 17.00
Delta Angle in Degrees = 146.00
u = -19
v = 200
Distance between A and B: 200.90
Distance between B and C: 105.04
Distance between A and C: 105.04
Coordinates of Boat at point C is = 97.07:418.90 (x:y)
To find the distance d directly without determining point C, first, you can calculate the area S of the triangle in two ways.
S = 0.5 * l * d
S = 0.5 * l * l * sin(a) * sin(b) / sin(a + b)
Equating the two and canceling out 0.5 * l gives d = l * sin(a) * sin(b) / sin(a + b).

Double automatic rounding

I'm developing a program that calculates the roots of a cubic equation with Tartaglia's method. The problem is when I print the value of the imaginary part, the program rounds the value of 1.54686889 to 1.50.
I have tried to use float instead of double, but, without success.
I'm using Code::Blocks 17.12
My code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
#define PI 3.14159265359
int main(void)
{
setlocale(LC_ALL,"");
double a, b, c, d, p, q, r, t, u, u3, v, v3, A, B, C, D, M, x1, x2, x2i, x3, x3i;
printf("Insira os coeficientes (a b c d): ");
scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
//Verifica se o coeficiente a é válido
if(a != 0){
printf("\nForma geral:\n");
printf("\n (%.2lf)x³ + (%.2lf)x² + (%.2lf)x + (%.2lf) = 0 \n", a, b, c, d);
A = b/a;
B = c/a;
C = d/a;
p = B - (pow(A, 2.0)/3.0);
q = C - ((A*B)/3.0) + (2.0*pow(A, 3.0))/27.0;
D = (pow(q, 2.0)/4.0) + (pow(p, 3.0)/27.0);
if(D < 0){
M = pow(-1.0*D, 1.0/2.0);
r = sqrt(pow(q, 2.0)/4.0 + pow(M, 2.0));
t = acos(-1.0*q/2.0/r);
x1 = 2.0 * pow(r, 1.0/3.0) * cos(t/3.0) - A/3.0;
x2 = 2.0 * pow(r, 1.0/3.0) * cos((t + 2.0 * PI)/3.0) - A/3.0;
x3 = 2.0 * pow(r, 1.0/3.0) * cos((t + 4.0 * PI)/3.0) - A/3.0;
printf("\nAs raízes são: <%lf> <%lf> <%lf>\n", x1, x2, x3);
}
else{
u3 = (-1.0*q/2.0) + pow(D, 1.0/2.0);
if(u3 < 0)
u = -1.0 * pow(-1.0*u3, 1.0/3.0);
else
u = pow(u3, 1.0/3.0);
v3 = (-1.0*q/2.0) - pow(D, 1.0/2.0);
if(v3 < 0)
v = -1.0 * pow(-1.0*v3, 1.0/3.0);
else
v = pow(v3, 1.0/3.0);
x1 = u + v - A/3.0;
D = pow((A + x1), 2.0) + (4.0*C/x1);
x2 = -1.0 * (A + x1)/2.0;
x2i = pow(abs(D), 1.0/2.0)/2.0;
x3 = x2;
x3i = x2i;
if(D < 0){
printf("\nAs raízes são: <%lf>, <%lf + %lfi>, <%lf - %lfi>\n", x1, x2, x2i, x3, x3i);
}
else{
x2 = x2 + x2i;
x3 = x3 - x3i;
printf("\nAs raízes são: <%lf>, <%lf>, <%lf>\n", x1, x2, x3);
}
}
}
else{
printf("O coeficiente <a> não pode ser igual a 0!\n");
}
printf("Fonte de pesquisa: http://www.matematicando.net.br/metodo-de-tartaglia-para-obter-raizes-de-equacao-do-3o-grau/\n");
system("pause");
return 0;
}
The console after printing:
Change the use of abs to fabs and the result is:
As raízes são: <-1.650629>, <-0.174685 + 1.546869i>, <-0.174685 - 1.546869i>
The function abs returns an integer, whereas fabs works on doubles.

Why does program output x1 = Nan, x2 = Nan?

I can't work out why my program isn't doing what it's supposed to do. The task was to make a c program that can solve quadratic using two c files. This is the code called interface.c that we got to work with, nothing has to be changed on this one:
#include <stdio.h>
void abc (void);
int a, b, c;
extern double x1real, x1imag, x2real, x2imag;
static void get_parameters (void)
{
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
}
void print_solution(void)
{
printf("The roots of %dx^2 + %dx + %d are:\n",a,b,c);
if(x1imag == 0 && x2imag == 0)
{
if(x1real == x2real)
{
printf("x = %.4f\n", x1real);
}
else
{
printf("x1 = %.4f, x2 = %.4f\n", x1real, x2real);
}
}
else
{
printf("x1 = %.4f+%.4fi, x2 = %.4f-%.4fi\n", x1real, x1imag, x2real, x2imag);
}
}
int main (void)
{
int runs, run;
scanf("%d",&runs);
for(run=0; run < runs; run++)
{
get_parameters();
abc();
print_solution();
}
return 0;
}
Next is the code I made which isn't working, there seems to go something wrong with the integer types I think. With every quadratic formula it will output x1 = nan x2 = nan. There is something wrong with the integer type but can't figure out which.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
extern int a, b, c;
double x1real, x1imag, x2real, x2imag;
int discriminant(void)
{
int discriminant;
discriminant = (pow(b,2)-4*a*c);
return discriminant;
}
void abc (void)
{
if (discriminant() > 0)
{
x1real = (-b - sqrt(discriminant()))/(2*a);
x2real = (-b + sqrt(discriminant()))/(2*a);
x1imag = 0;
x2imag = 0;
}
else
{
x1real = x2real = (-b) / (2*a);
x1imag = (-b - sqrt(-discriminant())) / (2*a);
x2imag = (-b + sqrt(-discriminant())) / (2*a);
}
return;
}
input:
4
2 0 0
1 3 2
3 4 9
1 0 1
output:
The roots of 2x^2 + 0x + 0 are:
x = 0.0000
The roots of 1x^2 + 3x + 2 are:
x1 = -1.0000, x2 = -2.0000
The roots of 3x^2 + 4x + 9 are:
x1 = -0.6667+-2.2653i, x2 = -0.6667-0.9319i
The roots of 1x^2 + 0x + 1 are:
x1 = 0.0000+-1.0000i, x2 = 0.0000-1.0000i
suspected output:
The roots of 2x^2 + 0x + 0 are:
x = 0.0000
The roots of 1x^2 + 3x + 2 are:
x1 = -1.0000, x2 = -2.0000
The roots of 3x^2 + 4x + 9 are:
x1 = -0.6667+1.5986i, x2 = -0.6667-1.5986i
The roots of 1x^2 + 0x + 1 are:
x1 = 0.0000+1.0000i, x2 = 0.0000-1.0000i
Quadratic formula?
conditional expression of「b^2-4ac≧0」 is not called, is it?
int discriminant(void)
{
int discriminant;
discriminant = (pow(b,2)-4*a*c);
return discriminant;
}
void abc (void)
{
// if (discriminant > 0) // it doesn't call [int discriminant(void)]
if (discriminant() >= 0)
{
x1real = (-b - sqrt(discriminant()))/(2*a);
x2real = (-b + sqrt(discriminant()))/(2*a);
x1imag = 0;
x2imag = 0;
}
else
{
x1real = x2real = (-b) / (2*a);
x1imag = (-b - sqrt(-discriminant())) / (2*a);
x2imag = (-b + sqrt(-discriminant())) / (2*a);
}
return;
}
(An addition)
static void get_parameters (void)
{
do {
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
} while(a == 0)
}

Why doesn't this code for calculating the solution of quadratic equation not work?

Whenever I try running this, it returns the wrong solution, for example:
A: 303
B: 405
C: 50
Real solution: −0.13762776465722773
My solution : -110079.531250
#include <stdio.h>
#include <math.h>
int main(){
float a;
float b;
float c;
float solution;
float d;
printf("A: ");
scanf("%f", &a);
printf("B: ");
scanf("%f", &b);
printf("C: ");
scanf("%f",&c);
d = b * b - 4 * a * c;
solution = (-b - sqrt(d))/ 2*a;
printf("%f", solution);
}
You forgot BODMAS. Replace (-b - sqrt(d))/ 2*a by (-b - sqrt(d))/ (2*a)
solution = (-b - sqrt(d))/ (2*a);
Two things.
You need to watch out for the order of operations.
solution = (-b - sqrt(d)) / (2*a);
And depending on your customer you need to consider the accuracy of your result.
See "Avoiding loss of significance" for more information
And finally - i had a bit of fun writing my own version of your program:
#include <stdio.h>
#include <math.h>
void printLineSolution( double a, double b, double c );
int main()
{
printLineSolution(303,405,50);
printLineSolution(1,2,0);
printLineSolution(1,2,-1);
printLineSolution(1,-2,-3);
printLineSolution(1,-6,9);
printLineSolution(1,3,3);
getchar();
}
void printLineSolution( double a, double b, double c )
{
double d = (b * b) - (4 * a * c);
printf("(%lg)x^2 + (%lg)x + (%lg) = 0 ", a, b, c);
if( a == 0 )
{
printf("=> not quadratic");
}
else
{
if( 0 > d )
{
double r = - b / (2*a);
double i = sqrt( -d ) / (2*a);
printf("=> 2 complex: %lg + %lgi ; %lg - %lgi", r, i, r, i);
}
else if ( 0 == d )
{
double solution = - b / (2*a);
printf("=> 1 real: %lg", solution);
}
else
{
double s1 = (- b + sqrt( d ) ) / (2*a);
double s2 = (- b - sqrt( d ) ) / (2*a);
printf("=> 2 real: %lg ; %lg", s1, s2);
}
}
printf("\n");
}

Resources