Types In C Displaying Imaginary Numbers - c

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;
}

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;
}

divide a 4-digit integer into 2-digit integers and calculate by c

//Enter a 4-digit integer n from the keyboard, and write a program to divide it into two 2-digit integers a and B. Calculate and output the results of the addition, subtraction, multiplication, division and redundancy operations of the split two numbers. For example, n=-4321, if the two integers after splitting are a and b, then a=-43 and b=-21. The result of division operation requires that it be precise to 2 decimal places, and the data type is float. Redundancy and division operations need to take into account the division of 0, that is, if the split B = 0, then output the prompt information "The second operator is zero!"
//Failure to pass the test,how should i fix
#include<stdio.h>
#include<math.h>
int main()
{
int x, a, b;
printf("Please input n:\n");
scanf("%d", &x);
a = x / 100;
b = x % 100;
printf("%d,%d\n", a, b);
printf("sum=%d,sub=%d,multi=%d\n", a + b, a - b, a*b);
if (b == 0)
printf("The second operater is zero!");
else
printf("dev=%.2f,mod=%d\n", (float)a / b, a%b);
}
You forgot to check that x is a 4-digits number. So if the input is 12345 or 123 you don't satisfy the requirement.
#include <stdio.h>
int main()
{
int x, a, b;
int passed = 0;
// Enter a 4 digits number: ABCD
do {
printf("Enter X = ");
scanf("%d", &x);
passed = (x >= 1000 && x <= 9999) || (x >= -9999 && x <= -1000);
} while (!passed);
a = x / 100;
b = x % 100;
printf("Numbers: %d %d \n", a, b);
printf("Sum = %d \n", a + b);
printf("Sub = %d \n", a - b);
printf("Mul = %d \n", a * b);
if (0 == b) {
printf("Div by Zero \n");
} else {
printf("Div = %f \n", (double)a / b);
printf("Mod = %d \n", a % b);
}
return 0;
}

not working-roots of a quadratic equation

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;
}

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.

Simple Multiplication and Arithmetic in C

I have an assignment to compute the roots of quadratic equations in C, should be pretty simple and I know what I need to do with the program but I am having a problem nonetheless.
It works fine when the roots are imaginary and when the term inside the square root is zero.
But when I enter coefficients a, b and c which would give real roots it gives me the wrong answer, and I cant figure out whats wrong. (I am testing it with a=2, b = -5 and c=1)
This is my code, it compiles and runs, but gives the wrong answer.
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, D, x, x1, x2, y, xi;
printf("Please enter a:\n");
scanf("%f", &a);
printf("Please enter b:\n");
scanf("%f",&b);
printf("Please enter c:\n");
scanf("%f", &c);
printf("The numbers you entered are: a = %f, b = %f, c = %f\n", a, b, c);
D = b*b-4.0*a*c;
printf("D = %f\n", D);
if(D > 0){
x1 = (-b + sqrt(D))/2*a;
x2 = ((-b) - sqrt(D))/2*a;
printf("The two real roots are x1=%fl and x2 = %fl\n", x1, x2);
}
if(D == 0){
x = (-b)/(2*a);
printf("There are two identical roots to this equation, the value of which is: %fl\n", x);
}
if (D<0){
y = sqrt(fabs(D))/(2*a);
xi = (-b)/(2*a);
printf("This equation has imaginary roots which are %fl +/- %fli, where i is the square root of -1.\n", xi, y);
}
return 0;
}
You don't calculate the result correctly:
x = y / 2*a
is actually parsed as
x = (y / 2) * a
so you have to put parentheses around 2*a.
you want this:
x = y / (2 * a)

Resources