Assignment (3)

CS1440 Due by 11:59 PM, Thursday September 6

 

You are required to write a C++ code to solve the quadratic equation. The first thing you want to do is to design the algorithm for the program. I think, if you take the following steps, you will have a good program at the end and less difficulty in writing your code.

 

Step 1.

Design the algorithm and draw the flowchart to solve the quadratic equation:

 

y = ax2+bx+c

The solution to a 2nd order quadratic equation, y = ax2+bx+c, is:

 

In our case, a, b, and c are input real values.

 

Your algorithm must consider all possible solutions and must be able to solve all possible cases and/or display appropriate message for the cases that is unable to solve.

 

As you may have noticed, there are at least two things you need to have in mind when you develop your algorithm. Can you tell me what they are?

 

1)      You cannot take the square root of a negative value. Thus, b2 4ac must always be larger or equal 0.

 

2) You cannot divide by 0. That means a must be a non-zero value. Actually if a = 0, then your quadratic equation will simply be a line: y = bx+c = 0. In this case, you do not need to solve the equation using the quadratic formula and can simply find the answer as:

if bx+ c = 0 then x = -c/b.

 

Now, you know what your algorithm should do.

 

Step 2:

Once you set up your algorithm, test it for few cases. I gave you three examples on the quadratic worksheet. See if it works for all of them.

 

Step 3:

Now try to translate your thoughts to a C++ code. Your algorithm must be precise. If you have a correct algorithm, you should be able to translate it line by line to a C++ code.

 

Remember your input values are, a, b, and c all real values. In return you will display three different things depending on the values of a, b, and c. Here are the possibilities.

 

1 ) If a, b, and c make part under the square root to be negative, you will display:

 

For the equation: ax^2 + bx + c = 0, there is no real answer.

 

Please print the values for a, b, and c in the above statement.

 

2) a = 0, you will display:

 

This bx + c = 0 is not a quadratic equation. It is a line and x = -c/b.

You will print the equation with the values for c and b.

 

 

3) Your equation: ax^2 + bx + c = 0 has two roots:

x1 = ?

x2 = ?

 

Step 4) Make sure the answers are correct and the same as what you have gotten in step (2).

 

Here is the grading procedure:

 

15 points) Correct heading and programming style (heading, indentation, comments)

45 points) Correct design of the algorithm

40 points) Correct results for all cases

 

Here is an example of the display that you will get once you run the code:

 

This program solves the quadratic equation: ax^2 + bx + c = 0.

Please provide three values for a, b, and c separated by a blank space.

a, b, and c are : 2 3 7

 

Your equation is : 2x^2 3x + 7 = 0

 

For the equation: 2x^2 + -3x + 7 = 0, there is no real answer.

Thank you for using this code.

 

What do you need to submit: You will use the submit program to submit your program by midnight on Thursday September 6. All programs received by midnight Tuesday will receive an extra 10 points. All late programs will receive 20 points penalty for each 24 hours late submission. After 48 hours, I will not grade your assignment any more.

 

Submit command:

~rt/bin/submit1440_103 assgn3 assgn3.C

 

You will submit a hard copy of your code in class on Friday September 7.