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.
Related
This question already has answers here:
Preventing console window from closing on Visual Studio C/C++ Console application
(24 answers)
Closed 5 months ago.
#include <stdio.h>
#include <math.h>
int main(){
float a;
float b;
float c;
printf("this program will solve you any equation from this type: ax^2+bx+c=0.\n");
printf("First of all enter a, b and c:\n");
printf("a:"); scanf("%f", &a);
printf("b:"); scanf("%f", &b);
printf("c:"); scanf("%f", &c);
if (a==0){
printf("now your equation is:\n");
printf("%.1fx+", b); printf("%.1f=0\n", c);
float x3 = -c/b;
printf("the solution for this equation is: %f", x3);
}
else{
printf("now your equation is:\n");
printf("%.1fx^2+", a); printf("%.1fx+", b); printf("%.1f=0\n", c);
float d = b*b-4*a*c;
printf("yy");
float x1;
float x2;
if(d > 0){
printf("delta is greater than 0! the equation have 2 solutions!\n");
float x1 = (-b+sqrt(d))/(2*a);
float x2 = (-b-sqrt(d))/(2*a);
printf("the first solution is: %.2f\n", x1);
printf("the second solution is: %.2f\n", x2);
}
else if (d == 0)
{
printf("delta equals 0 the equation have a doubled solution!\n");
float x1 = -b / 2*a;
printf("the doubled solution is: %f", x1);
}
else if (d < 0)
{
printf("delta is less than 0! the equation have no solutions in R :(");
}
}
return 0;
}
it works perfectly when i run it in vs code terminal thing but when i open the batch file it exits after entering the value of c
edit: the code i posted is only the start and the remaining code is 100% correct because i test it many times in terminal in vs code and it works
The file is correctly executed. When you open the batch file, the program reaches the end at the return 0 istruction (immediatly after you enter c). This operation is so fast that apparently is invisible, but the program is executed correctly. The reason why you can see it into the terminal, is because the terminal doesn't clear the prints.
I'm new to C language and coding and I encountered a question asking me to change the function header of:
float RealRoot_1(float a, float b, float c);
float RealRoot_2(float a,float b,float c);
to become:
void RealRoot_1(void);
void RealRoot_2(void);
I was told that it has something to do with Global Variables but I still couldn't figure it out after trying quite some time. Can anyone please explain on how to do it? Thanks a lot.
The source file is as below:
#include<stdio.h>
#include<math.h>
int main()
{
float RealRoot_1(float a, float b, float c); // Prototype declaration
float RealRoot_2(float a, float b, float c);
// Defining Input Variables
float x, y, z;
// Defining Output Variables
float Root_1, Root_2;
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
Root_1 = RealRoot_1(x,y,z);
Root_2 = RealRoot_2(x,y,z);
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
float RealRoot_1(float a, float b, float c)
{
float x;
x = (-1*b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
float RealRoot_2(float a, float b, float c)
{
float x;
x = (-1*b - sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
This can be done by using global variables. You need to ensure that the variable names used in the function are the same as the ones used in the main code.
#include<stdio.h>
#include<math.h>
void RealRoot_1(void); // Prototype declaration
void RealRoot_2(void);
float x, y, z;
float Root_1, Root_2;
int main()
{
// Defining Output Variables
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
RealRoot_1();
RealRoot_2();
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
void RealRoot_1(void)
{
Root_1 = (-1*y + sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
void RealRoot_2(void)
{
Root_2 = (-1*y - sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
Please note that this is a worse way of doing things than was given in the initial problem. In the initial exercise. You are loosing modularity and using too many globals is in general a bad idea.
You can also see Are global variables bad?
This should be self explanatory:
float RR_a, RR_b, RR_c;
float RR_d; // store result here(like a return value)
void RealRoot_1(void); // prototypes
void RealRoot_2(void);
void main(void)
{
printf("Please enter the factor of X^2: ");
scanf("%f",&RR_a);
printf("Please enter the factor of X: ");
scanf("%f",&RR_b);
printf("Please enter the free factor: ");
scanf("%f",&RR_c);
RealRoot_1();
printf("the First Root is: %f \n", RR_d);
RealRoot_2();
printf("the Second Root is: %f \n", RR_d);
system("pause");
}
void RealRoot_1(void)
{
float x;
x = (-1*RR_b + sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
void RealRoot_2(void)
{
float x;
x = (-1*RR_b - sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
Notice that after calling RealRoot_1 we now print the result before calling RealRoot_2. That's because the result of RealRoot_1 which is stored in RR_d is overwritten by RealRoot_2, thus it is lost.
You can circumvent this by declaring a second return variable, RR_d_2 and storing the result of RealRoot_2 in it.
We do not need duplicates for RR_a, RR_b or RR_c because their values are not modified within the functions.
This way of writing functions has limitations, which will be obvious when faced with recursion or multi-threading.
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;
}
I need to make a program in C that takes 3 coefficients, a, b, c, then solves for Delta. It then takes Delta and decides what Function to send it to determine it's output.
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: Find Delta, solve for roots.
*
* Input: Coefficients a, b, c.
*
* Output: Roots
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
//Statements
printf("Input coefficient a.\n");
scanf("%.2f", &a);
printf("Input coefficient b.\n");
scanf("%.2f", &b);
printf("Input coefficient c.\n");
scanf("%.2f", &c);
printf("%fx^2 + %fx + %f\n", &a, &b, &c);
//Process
delta = (b * b) - (4 * a * c);
if (delta > 0) twoRoots(a, b, c, delta);
else if (delta = 0) oneRoot(a, b, c, delta);
else if (delta < 0) noRoots();
return;
} // End main
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To solve for the two roots.
*
* Input: None
*
* Output: Root one, Root two.
*/
#include <stdio.h>
#include <math.h>
int twoRoots ()
{
//Local Declarations
float xOne;
float xTwo;
float delta;
float deltaRoot;
float a;
float b;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", &xOne);
printf("%.2f", &xTwo);
return;
} // End twoRoots
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To solve for the one root.
*
* Input: None
*
* Output: Root one.
*/
#include <stdio.h>
#include <math.h>
int oneRoot ()
{
//Local Declarations
float xOne;
float xTwo;
float deltaRoot;
float a;
float b;
printf("There is exactly one distinct root./n");
xOne = -b / (2*a);
printf("%.2f", &xOne);
return;
} // End oneRoot
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To inform the roots are complex.
*
* Input: None
*
* Output: Statement.
*/
#include <stdio.h>
#include <math.h>
int noRoots ()
{
//Local Declarations
printf("There are two distinct complex roots./n");
return;
} // End noRoots
When I run it, I get the following Output:
Input coefficient a.
1
Input coefficient b.
Input coefficient c.
0.000000x^2 + 882156984598706310000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000.000000x + 0.000000
Process returned 16384 (0x4000) execution time : 10.641 s
Press any key to continue.
I only input 1, for a, and then it spits outt he remainder of the main method.
Few things that jump out first:
printf("Input coefficient a.\n");
scanf("%f", &a); // you were scanning for 0.2f .. any reason why?
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
your printf is also wrong .. change it to this:
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c); // you were printing the addresses of a,b,c .. printf just needs the name of variables not their addresses
Output after doing above changes:
$ ./test
Input coefficient a.
1.5
Input coefficient b.
2.5
Input coefficient c.
3.5
1.50x^2 + 2.50x + 3.50
Fixed code: ( ask me if you have questions about any part )
#include <stdio.h>
#include <math.h>
// function declarations
void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
float solution;
printf("Input coefficient a.\n");
scanf("%f", &a);
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);
delta = (float)(b*b) - (float)(4.0 * a * c);
printf("delta = %0.2f\n",delta);
if (delta > 0){
twoRoots(a,b,delta);
}else if (delta == 0) {
oneRoot(a,b,delta);
}else if (delta < 0.0){
printf("There are no real roots\n");
}
return 0;
}
void twoRoots (float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", xOne);
printf("%.2f", xTwo);
}
void oneRoot(float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There is exactly one distinct root\n");
xOne = -b / (2*a);
printf("%.2f", xOne);
}
Output1:
$ ./test
Input coefficient a.
1.1
Input coefficient b.
5.5
Input coefficient c.
2.2
1.10x^2 + 5.50x + 2.20
delta = 20.57
There are two distinct roots.
-0.44-4.56
Output2:
$ ./test
Input coefficient a.
1
Input coefficient b.
4
Input coefficient c.
4
1.00x^2 + 4.00x + 4.00
delta = 0.00
There is exactly one distinct root
-2.00
Output3:
$ ./test
Input coefficient a.
1
Input coefficient b.
3
Input coefficient c.
9
1.00x^2 + 3.00x + 9.00
delta = -27.00
There are no real roots
I optimised the code and made it a whole lot more efficient here :
http://pastebin.com/GS65PvH6
I think that the problem comes from the fact that 1 is recognized as an int and not a float.
When you write %.2f scanf expects you to input a float. If it detects something else it fails and does not read any other scanf requests as specified in the man page.
Your immediate problem lies here:
scanf ("%.2f", &a);
You can put a length limiter on the value to be scanned but you probably shouldn't be trying to limit what's input anyway.
In any case, the .2 option you're using is not valid for scanf, it's a printf thing controlling the precision of the output.
The ISO standard states that scanf needs "an optional decimal integer greater than zero that specifies the maximum field width (in characters)". So there's no way using just scanf to limit how many digits are allowed after the decimal point.
Use this instead:
scanf ("%f", &a);
including for the other scanf calls.
As to further problems, there are a few, some of which are below. I haven't provided an exhaustive list as the problem specific to your question is the scanf format string.
First, you want to print the values of those variables rather than their addresses:
printf ("%fx^2 + %fx + %f\n", a, b, c);
Second, you pass the variables a/b/c/delta to your functions but you do not receive them. You need to declare them something like:
int twoRoots (float a, float b, float c, float delta)
and ensure you remove any local variable declarations for those names so that they don't hide the passed-in ones (or cause compilation errors).
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)