Quadratic Equations Due 2/13/2013

advertisement
Computer Programming
CS1 – Spring 2013
Assignment #2 – Expressions, Output Formatting, Control statements.
Introduction
In this assignment you will write a small C++ program to a) read the coefficients and solve the
quadratic equation and b) read a range of x values (xmin, xmax) and make a table like the one
shown below. The C++ programs should be submitted as a standard C++ source code file. Also
submit a Text/Word/PDF document with instruction to compile/run and use your program as
well as the answer to the questions (if any). You can either submit these files separately or
zipped together into one file. There are no specific naming conventions.
Please note that the computer program should comply with the commenting and formatting rules
as has been done in class. For example, there should be a header for the whole program that
gives the author’s name, class name, date, and description. There are alignment and indenting
requirements as discussed. Please ask if you have any questions.
Program #1
Your first program is a simple mathematical calculation program. A quadratic (second order
polynomial) equation ax2 + bx + c = 0; can be solved by the formulas:
𝑥1 =
If 𝑏 2 − 4𝑎𝑐 < 0 then:
−𝑏 − √𝑏 2 − 4𝑎𝑐
−𝑏 + √𝑏 2 − 4𝑎𝑐
𝑎𝑛𝑑 𝑥2 =
2𝑎
2𝑎
−𝑏 √4𝑎𝑐 − 𝑏 2
−𝑏 √4𝑎𝑐 − 𝑏 2
𝑥1 =
−
𝑖 𝑎𝑛𝑑 𝑥2 =
+
𝑖
2𝑎
2𝑎
2𝑎
2𝑎
A) Write a C++ program that shows a menu to get 3 double values from the user, namely, a, b,
and c, and an option to calculate and return the values for x1 and x2 to the above formula.
Your program should prompt the user for these values, accept input from the keyboard, and
then print out the result. Include a report in the Text/Word/PDF file that shows the results for
the following values:
1)
2)
3)
(2,3.5,5)
(3,6,5.1)
(1.0,9,2)
Program #2
B) Extend the C++ program in A), to give the option of entering a range of x values, namely xmin
and xmax, and display a table like the one shown below:
CS1 – Introduction to Programming
page: 2
Assume that the values for a, b and c were given as 3, 4.7 and 2 respectively. The user selects
a range for x values from -3 to 5. The program will display a table like:
x
y
-3
14.90
-2
4.60
-1
0.30
0
2.00
1
9.70
2
23.40
3
43.10
4
68.80
5 100.50
Where y = ax2 + bx + c, or in this case y = 3x2 + 4.7x + 2
Hint:
You may use the functions double sqrt (double), and double pow (double
base, double exponent) found in <cmath>, in your calculations.
Download