[go: up one dir, main page]

0% found this document useful (0 votes)
396 views3 pages

Quadratic

This document discusses a C program for solving quadratic equations. It begins by presenting the original program code and explaining how it calculates the two roots of a quadratic equation given coefficients a, b, and c. The comments then discuss improvements and edge cases, like adding error handling for a negative discriminant. An upgraded program is provided that prints a message if the discriminant is negative instead of producing NaN values. The comments also explain concepts like linking libraries and if statements.

Uploaded by

Lorena Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
396 views3 pages

Quadratic

This document discusses a C program for solving quadratic equations. It begins by presenting the original program code and explaining how it calculates the two roots of a quadratic equation given coefficients a, b, and c. The comments then discuss improvements and edge cases, like adding error handling for a negative discriminant. An upgraded program is provided that prints a message if the discriminant is negative instead of producing NaN values. The comments also explain concepts like linking libraries and if statements.

Uploaded by

Lorena Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C PROGRAMMING: SOLVING THE QUADRATIC EQUATION

/* This program solves the quadratic equation */


#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c,root1,root2;
printf(" Please enter a \n");
scanf("%lf",&a);
printf(" Please enter b \n");
scanf("%lf",&b);
printf(" Please enter c \n");
scanf("%lf",&c);
root1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a);
root2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a);
printf("\n First

root is %lf ",root1);

printf("\n Second root is %lf ",root2);


printf("\n ");
return 0;
}
Comments:
[1] When you compile with gcc you need to link to the math library
gcc -o quadratic.e quadratic.c -lm
Otherwise, the compiler does not know about the square root function sqrt . You will also
need to use the -lm compiler switch if you use other special functions: exp, sin, log, abs
[2] You should always check your codes as much as possible. Often all you can do is special
cases because you cannot solve the whole problem. In this case we dont really need a
computer. Try, for example, a = 2, b = 5, c = 3 and then solve the problem with pencil
and paper (either with the quadratic equation or by factoring).

[3] Can anyone see a flaw in the program? What happens if you run the code with a =
2, b = 5, c = 4? What does nan mean? (Answer: not a number).
[4] What is the geometric interpretation of the discriminant being negative? Answer: the
parabola y = ax2 + bx + c doesnt intersect the x-axis.
We can write a better program to deal with the case when the discriminant is negative.
/* This program solves the quadratic equation more completely*/
#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c,root1,root2;
printf(" Please enter a \n");
scanf("%lf",&a);
printf(" Please enter b \n");
scanf("%lf",&b);
printf(" Please enter c \n");
scanf("%lf",&c);
if (b*b-4.*a*c>0)
{
root1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a);
root2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a);
printf("\n First

root is %lf ",root1);

printf("\n Second root is %lf ",root2);


}
else
{
printf("\n Discriminant is negative!
}
printf("\n ");
return 0;
}

No roots!");

[5] This upgraded program introduces to a new element of the C language: the if statement.
The syntax of the if statement is fairly straightforward: within the parentheses (

) is an

arithmetic expression. If the expression is true, the lines within the first curly brackets { }
are executed. If it is false, the lines within the second curly brackets (following the else. )
are performed. More complex if statements are also available.
[6] Extra task for those interested: Could you write a program which can deal with negative
discriminants and print out a complex number answer like 2 + 3i? Hint: you do not need to
know how to use complex numbers in C to write the program.
[7] Can you prove the quadratic formula? Hint: Take ax2 + bx and complete the square
ax2 + bx

b
b2
b2
b
= a(x2 + x) = a(x2 + x + 2 2 )
a
a
4a
4a
2
2
b
b
b 2 b2
b
2
= a(x + x + 2 )
= a(x + )
a
4a
4a
2a
4a

You might also like