|
|
CS 2073, Fall 2005
Program 2
Roots of a Quadratic Equation
Week 2: Aug 29-Sep 2
Due (on time):
2005-09-09 23:59:59
Due (late):
2005-09-12 23:59:59
|
Program 2 must be emailed to:
nrwagner@cs.utsa.edu
following directions for: running
and submitting a C program, with deadlines:
- 2005-09-09 23:59:59 (that's Friday, 9 September 2005, 11:59:59 pm)
for full credit.
- 2005-09-12 23:59:59 (that's Monday, 12 September 2005, 11:59:59 pm)
for 75% credit.
|
Introduction:
For this program you are to find the roots of a quadratic equation.
Specifically, start with an equation:
a*x2 + b*x + c = 0 ,
for specific doubles a, b, and
c.
Then calculate and print the values of the roots,
r1 and r2.
Details about what to do:
- Use scanf to read values for double
variables a, b, and
c, which are assumed to be the coefficients of a quadratic equation.
- Print the input numbers in the form of a quadratic equation, using
the form shown in the example section below.
- If a is equal to zero, print a message that this is
linear equation. In case b is not zero, there is a single
root, and if b does equal zero, there is no root.
Print the value of this root or print that there is no root.
- Otherwise, calculate the discriminant d given by
d = b2 - 4*a*c
- If the discriminant is greater than zero, then there
are two real roots.
Print a message that says this, use the quadratic formula to calculate their values,
r1 and r2, and print these two values.
- In case the discriminant is equal to zero, there is a single repeated
real root. Print a message that says this, use the quadratic formula
to calculate the value, and print this value.
- If the discriminant is less than zero, then there two imaginary roots.
Print a message that there are two imaginary roots and then print the
real and imaginary part of each root. (The real part of both roots is the
same: -b/(2*a), while the imaginary parts are
sqrt(-d)/(2*a) and -sqrt(-d)/(2*a).
Notice that we have d < 0, so -d > 0,
and it makes sense to take the square root of -d.)
Sample input and output:
You may use a separate run for each input triple of numbers.
Below in bold are the three numbers that you enter for each separate
run. Everything else is what your program should output.
You should try each of these 8 sets of inputs and your answers should be the same.
For full credit, your output should look exactly the same as the output shown.
(However, don't worry about the number of spaces before an output number.)
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 0.0 2.0 4.0
Solving 0.000000 x^2 + 2.000000 x + 4.000000 = 0
This is a linear equation (no quadratic term)
Root: -2.000000
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 0.0 0.0 -2.0
Solving 0.000000 x^2 + 0.000000 x + -2.000000 = 0
This is a linear equation (no quadratic term)
There is no root
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 1.0 -4.0 4.0
Solving 1.000000 x^2 + -4.000000 x + 4.000000 = 0
There is a single repeated root
Root: 2.000000
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 1.0 -4.0 -12.0
Solving 1.000000 x^2 + -4.000000 x + -12.000000 = 0
There are two real roots
Root 1: 6.000000
Root 2: -2.000000
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 1.0 -6.0 25.0
Solving 1.000000 x^2 + -6.000000 x + 25.000000 = 0
Roots are complex conjugates
Root 1: Real part: 3.000000, imaginary part: 4.000000
Root 2: Real part: 3.000000, imaginary part: -4.000000
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 0.5 0.0 -3.0
Solving 0.500000 x^2 + 0.000000 x + -3.000000 = 0
There are two real roots
Root 1: 2.449490
Root 2: -2.449490
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 3.5 -4.0 -2.75
Solving 3.500000 x^2 + -4.000000 x + -2.750000 = 0
There are two real roots
Root 1: 1.626059
Root 2: -0.483202
Solving a*x^2 + b*x + c = 0
Input 3 numbers for a, b, and c: 1.0 -3.0 3.0
Solving 1.000000 x^2 + -3.000000 x + 3.000000 = 0
Roots are complex conjugates
Root 1: Real part: 1.500000, imaginary part: 0.866025
Root 2: Real part: 1.500000, imaginary part: -0.866025
What you should email:
Refer to the submissions directions and to
deadlines at the top of this page. The text file that you submit
should first have Your Name, the Course Number,
and the Program Number. The rest of the file
should have the following in it, in the order below, and clearly labeled,
including at the beginning the appropriate item letters:
a, b, c, etc.
|
Contents of email submission
for Program 2:
Last Name, First Name; Course Number; Program Number.
- C source for your program, quadratic.c. (Or whatever you wish to name it.)
- Results of a run or runs of the program, including at least all the 8 sets of
data above.
|
Revision date: 2005-09-03.
(Please use ISO
8601, the International Standard.)