|
|
CS 2073, Spring 2006
Program 2
Roots of a Quadratic Equation
Week 2: Jan 23-27
Due (on time):
2006-02-03 23:59:59
Due (late):
2006-02-06 23:59:59
|
Program 2 must be emailed to:
nrwagner@cs.utsa.edu
following directions for: running
and submitting a C program, with deadlines:
- 2006-02-03 23:59:59 (that's Friday, 3 February 2006, 11:59:59 pm)
for full credit.
- 2006-02-06 23:59:59 (that's Monday, 6 February 2006, 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.
The principal difficulty of this program is that it needs
a fairly complicated collection of if-else statements.
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: -c/b, and if b does equal zero, there is no root.
Print the value of this root or print that there is no root.
In this case, your program should not do any of the following items.
- Otherwise, assuming a is not zero,
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 are 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.
(It is important for you to realize that the C language has no built-in
facilities to handle complex numbers. We are simply working with
real numbers, and calculating real and imaginary parts (both reals)
of the roots as complex numbers.)
Sample input and output:
You may use a separate run for each input triple of numbers,
since we haven't studied loops yet.
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.
Input 3 numbers for a, b, and c: 0.0 2.0 4.0
This is a linear equation (no quadratic term)
Root: -2.000000
Input 3 numbers for a, b, and c: 0.0 0.0 -2.0
This is a linear equation (no quadratic term)
There is no root
Input 3 numbers for a, b, and c: 1.0 -4.0 4.0
There is a single repeated root
Root: 2.000000
Input 3 numbers for a, b, and c: 1.0 -4.0 -12.0
There are two real roots
Root 1: 6.000000
Root 2: -2.000000
Input 3 numbers for a, b, and c: 1.0 -6.0 25.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
Input 3 numbers for a, b, and c: 0.5 0.0 -3.0
There are two real roots
Root 1: 2.449490
Root 2: -2.449490
Input 3 numbers for a, b, and c: 3.5 -4.0 -2.75
There are two real roots
Root 1: 1.626059
Root 2: -0.483202
Input 3 numbers for a, b, and c: 1.0 -3.0 3.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: 2006-01-08.
(Please use ISO
8601, the International Standard.)