Please, using C, not C+ or C++.Thanks. Time it took Matthew to complete: 5 minut
ID: 3593377 • Letter: P
Question
Please, using C, not C+ or C++.Thanks.
Time it took Matthew to complete: 5 minutes
Files to submit: quad.c
Requriements
Program must compile with both -Wall and -Werror options enabled
Submit only the files requested
Use doubles to store real numbers
Print all doubles to 2 decimal points unless stated otherwise
Restrictions
No global variables may be used
Your main function may only declare variables and call other functions
Description
Depending on the values of a, b, and c there may be 0, 1, or 2 real solutions. Write a program called quad.c that asks the user for a, b, and c and then displays the real roots of x if there are any.
Assumptions
Input will always be valid
a will never be 0
Tips
When there are 2 solutions the + solution is printed first
You may need to compile with the -lm flag on the command line to use the functions inside of math.h
Examples
In the examples below user input has been underlined. You don't have to do any underlining in your program. It is just there to help you differentiate between what is input and what is output.
Example 1
Given a quadratic equation of the form a*x^2 + b * x + c
Please enter a: 1
Please enter b: 2
Please enter c: 1
There is one real solution: -1.00
Example 2
Given a quadratic equation of the form a*x^2 + b * x + c
Please enter a: 4
Please enter b: 3
Please enter c: 7
There are no real solutions
Example 3
Given a quadratic equation of the form a*x^2 + b * x + c
Please enter a: 3
Please enter b: 11
Please enter c: 8
There are 2 real solutions
Solution 1: -1.00
Solution 2: -2.67
Explanation / Answer
Compile command : gcc -Wall -Werror quad.c -lm
Run Command : ./a.out
quad.c
#include<stdio.h>
#include<math.h>
int main(){
double a,b,c;
double root1, root2;
double disc;
printf("%s ","Given a quadratic equation of the form a*x^2 + b * x + c");
printf("Please enter a: ");
scanf("%lf", &a);
printf("Please enter b: ");
scanf("%lf", &b);
printf("Please enter c: ");
scanf("%lf", &c);
disc = ((b*b) - 4*a*c);
if (disc<0){
printf("%s ", "There are no real solutions");
} else if (disc == 0){
root1=(-b) / (2*a);
printf("%s %.2f ", "There is one real solution:",root1);
} else {
root1=((-b) + sqrt(disc)) / (2*a);
root2=((-b) - sqrt(disc)) / (2*a);
printf("%s ", "There are 2 real solutions");
printf("%s %.2f ","Solution 1:", root1);
printf("%s %.2f ","Solution 2:", root2);
}
}