not working-roots of a quadratic equation - c

I can't even take b and c from the keyboard. it stops after enter a value and gives the result always like 0.0000. What's wrong with this code ?
#include <iostream>
#include <math.h>
int main() {
float a,b,c,d;
float x1,x2;
printf("enter a,b,c\n");
scanf("%f %f %f", &a, &b, &c);
d = b*b - 4*a*c;
if (d<0) {
printf ("no real roots");
}
else
{
x1= (-b + sqrt (d)) / 2*a;
x2= (-b - sqrt (d)) / 2*a;
printf ("the roots are\n");
printf ("x1=%f x2=%f",x1,x2);
}
return 0;
}

Related

The output always comes a garbage value in periods of pendulum question in C

Reference Image
The output always comes: 6.35 (I think it's a garbage value)
the code is
#include<stdio.h>
#include<math.h>
float formula(float,float);
int main()
{
float l=0,a=0;
printf("enter the length of the pendulum(l)\n");
scanf("%f",&l);
printf("Enter the angle of displacemnt(a)\n");
scanf("%f",&a);
printf("the length is %0.2f\n",l);
printf("the angle of displacemnt is %0.2f\n",a);
printf("the period of pendulum is %0.2f",formula(l,a));
return 0;
}
float formula(float l, float a)
{
float P=0,ran=0,g = 9.8;
ran = (l/g) * (1 + ((1/4)*(pow((float)(sin((double)a/2)),2))));
P = 2 * M_PI * ((float)sqrt((double)ran));
return P;
}
I don't know what is happening 😐
You can simplify this implementation by using doubles or floats exclusively. The 1/4 integer division yields 0. Here's an example:
#include <stdio.h>
#include <math.h>
float formula(float l, float a)
{
float ran = l / 9.8f * (1.0f + powf(sinf(a/2.0f), 2.0f) / 4.0f);
return 2.0f * (float) M_PI * sqrtf(ran);
}
int main(void)
{
float l;
float a;
printf("Enter the length of the pendulum(l): ");
scanf("%f", &l);
printf("Enter the angle of displacemnt(a): ");
scanf("%f", &a);
printf("The length is %0.2f\n", l);
printf("The angle of displacemnt is %0.2f\n", a);
printf("The period of pendulum is %0.2f\n", formula(l,a));
return 0;
}

Can't find the reason why value stored to 'delta' isn't read

While analyzing can't seem to avoid the value stored to 'delta' not being read... what part of my loop isn't working and why?
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float delta;
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
float x, x1, x2;
delta=((b*b)-(4*a*c));
if("delta==0")
{
x=((-b)/(2*a));
printf("x=%f \n", x);
}
else if("delta>0")
{
x1=((-b+sqrt(delta))/(2*a));
x2=((-b-sqrt(delta))/(2*a));
printf("x1=%f i x2=%f \n", x1, x2);
}
else printf("Nie ma w zbiorze liczb rzeczywistych rozwiazania tego rownania.\n");
return 0;
}
1st thing, if("delta==0") inside "" whatever is present that will be treated as a address so if(address) means true, remove the double quotation.
if(delta==0)
// some code
else if (delta > 0)
// some code
2nd thing, you are comparing(==) a float & an integer, so be aware of behaviour of comparing different operands.

C program not printing out correct output

I have written the following code to compute the roots of a quadratic equation:
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp);
int main (void) {
int ap,bp,cp;
int dp, root1p, root2p;
quadroots(&ap, &bp, &cp, &root1p, &root2p, &dp);
printf("The solutions are: %f, %f", root1p, root2p);
}
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp){
int a, b, c, d, root1, root2;
printf("Enter a, b, c \n");
scanf("%d, %d, %d", &a, &b, &c);
if (a==0) {
printf ("The system is linear. The roots cannot be computed using this program: a cannot be 0. Please recompile");
return 0;
}
int b_sqared = b*b;
d = b_sqared - (4 * a * c);
if (d<0) {
d=-d;
printf("The roots of this equation are the complex numbers: \n");
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
printf(", %.3f%.3fi", (-b / (2*a)), (-sqrt(d) / (2*a)));
}
else if (d==0) {
printf("The root of this equation are real and equal. \n");
root1= (-d / (2*a));
printf("The roots of this equation are: %.3f, %.3f", root1, root1);
}
else {
printf ("The roots of the quadratic equation are real numbers. \n");
root1 = (-b + sqrt(d)) / (2*a);
root2 = (-b - sqrt(d)) / (2*a);
printf("Roots of the quadratic equation are the real numbers %.3f, %.3f", root1,root2);
}
return 0;
*root1p=root1;
*root2p=root2;
}
This is based off a code I had previously written which worked, but back then, I wasn't using functions.
As it is now, it compiles and runs fine (ie. it takes in the numbers and performs a calculation), but the answer it prints out is totally incorrect.
Eg. for the input "1 5 6" (corresponding to the equation x^2 +5x + 6, it should print out " The roots are real numbers.
The roots are the real numbers 6 and 1"
since those are the roots of the equation. However, it doesn't. What is printed are some absurdly huge numbers (Enter a, b, c
1 5 6
The roots of this equation are the complex numbers:
-2719010580126301300000000000.000+0.000i, -2719010580126301300000000000.0000.000iThe solutions are: 0.000000, 0.000000)
Any help would be much appreciated.
Thank you very much! Best.
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
You are using integer division in ((-b) / (2*a)) So you will get incorrect values for some numbers.
You can use.
printf("%.3f+%.3fi", ((-b) / (2.0*a)), (sqrt(d) / (2 * a)));
to force a conversion to a double before the division. You need to do this for all division between two integers in the code.
The design of this programm is horrific, but if you
add #include <math.h> (so sqrt is known)
replace all ints by floats (you don't want integer maths here)
replace scanf("%f, %f, %f", &a, &b, &c); by scanf("%f %f %f", &a, &b, &c); (correct format string for scanf).
it should work more or less.
I didn't dig further, so there may be other problems though.

Calculate distance between 2 coordinates with function in c programing (x,y,z)

I'm trying to calculate the distance between 2 coordinates in 3D, however, when complied, and entered (1,1,1), (1,1,1), output returns 2 instead of 0.
Code here
#include <stdio.h>
#include <math.h>
int get_dist(int x_i,int y_i,int z_i,int x_o,int y_o,int z_o){
int coord_dist = 0;
coord_dist = sqrt(((x_o - x_i)^2) + ((y_o - y_i)^2) + ((z_o - z_i)^2));
return coord_dist;
}
int main(void){
int x0,y0,z0,x1,y1,z1;
int dist0_1 = 0;
printf("coord 0:");
scanf("%d %d %d", &x0, &y0, &z0);
printf("coord 1:");
scanf("%d %d %d", &x1, &y1, &z1);
dist0_1 = get_dist(x0,y0,z0,x1,y1,z1);
printf("%d\n", dist0_1);
return 0;
}
^ is XOR, not exponent. You want the pow() function.

Types In C Displaying Imaginary Numbers

I have a question about types in C and I think it has to do with "real's"... This program is a quadratic equation solver and the user inputs a, b, and c in terms of ax^2 + bx + c = 0. My program works and I am sorry for lake of comments in the code so I will try to be very specific in my question. If you enter say 2, 2, 2 the discriminate of the quadratic is negative meaning no real answers or "imaginary numbers" (oh good old algebra days). So when you do this you get something like this
Specific part in code where this happens:
(First else in the while loop)
discriminate = b*b - 4 * a*c;
if (discriminate < 0)
{
root1 = (-b + sqrt(discriminate)) / (2 * a);
root2 = (-b - sqrt(discriminate)) / (2 * a);
printf("\nNOTE: Roots are not real.\n");
printf("The roots are, %.3f, and %.3f\n", root1, root2);
break;
}
So my question is two parts.
1) What is -1.#IO, and -1.#IO mean? (I know what it means) but what is #IO
2) How can I display the number properly? Is there a way?
FULL CODE:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int main(void)
{
//declarations
float a;
float b;
float c;
float root1, root2;
int count;
float discriminate;
//Initialization
count = 0;
//starting propmts
printf("\nHello, this program will compute the real roots");
printf("of a quadratic equation.\n");
printf("In terms of a(x)^2 + b(x) + c = 0\n");
printf("\nPlease enter in the \"a\" value: ");
scanf("%f", &a);
printf("Please enter in the \"b\" value: ");
scanf("%f", &b);
printf("Please enter in the \"c\" value: ");
scanf("%f", &c);
while (count == 0)
{
if (a == 0)
{
if (a == 0 && b == 0)
{
printf("There is no soultion...\n");
break;
}
else
{
root1 = (-c / b);
printf("\nNOTE: Input is not quadratic but uesing \"(-c / b)\" ");
printf("the root is %.3f\n", root1);
break;
}
}
else
{
discriminate = b*b - 4 * a*c;
if (discriminate < 0)
{
root1 = (-b + sqrt(discriminate)) / (2 * a);
root2 = (-b - sqrt(discriminate)) / (2 * a);
printf("\nNOTE: Roots are not real.\n");
printf("The roots are, %.3f, and %.3f\n", root1, root2);
break;
}
else
{
root1 = (-b + sqrt(discriminate)) / (2 * a);
root2 = (-b - sqrt(discriminate)) / (2 * a);
if (root1 == root2)
{
printf("The root is, %.3f.\n", root1);
break;
}
else
{
printf("The roots are, %.3f, and %.3f.\n", root1, root2);
break;
}
}
}
}
printf("Goodbye.\n");
return 0;
}
MY CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x,y,i,j;
clrscr();
printf("\t\t\t QUADRATIC EQUATION SOLVING\n");
printf("Enter the co-efficients of x^2,x and constant \n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>=0)
{
x=(-b+sqrt(d))/(2*a);
y=(-b-sqrt(d))/(2*a);
printf("The roots of the equation are %.2f %.2f",x,y);
}
else
{
d*=-1;
i=b/(2*a);
j=sqrt(d)/(2*a);
printf("The roots are %.2f+%.2fi and %.2f-%.2fi",i,j,i,j);
}
getch();
}
If the discriminant is less than zero, then you have some extra work to do. With the current code, you take the square root of a negative number, and the result should be not-a-number (NAN). I'm not sure why the printf doesn't just say that.
To fix the problem, you need to take the square root of the negative of the discriminant. Then you need to calculate the real and imaginary parts of the answer and display them as a complex number. Note that printf doesn't have any built-in support for complex numbers, so you have format the number yourself, e.g.
printf( "%f + %f i", realpart, imagpart );
If the discriminant is less than zero, then you have 2 complex roots. If the discriminant is greater than zero, then you have 2 real roots. If the discriminant is zero, then you have one real root.
if (discriminate < 0)
{
float rootr = -b / (2 * a);
float rooti = sqrt(-discriminate) / (2 * a);
printf("\nNOTE: Roots are not real.\n");
printf("The roots are, %.3f + %.3f i, and %.3f - %.3f i\n",
rootr, rooti,
rootr, rooti);
break;
}
else if(discriminate > 0)
{
float s = sqrt(discriminate);
float root1 = (-b + s) / (2 * a);
float root2 = (-b - s) / (2 * a);
printf("The roots are, %.3f, and %.3f.\n", root1, root2);
break;
}
else
{
float root = -b / (2 * a);
printf("The root is, %.3f.\n", root);
break;
}

Resources