Skip to content
Snippets Groups Projects
Commit 2ca58725 authored by cprutean's avatar cprutean
Browse files

Update file Checkpoint2.ipynb

parent e6e79952
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:75c1fe44 tags:
## Checkpoint 2
### Aim
To write a Python program to read in the coefficients of a quadratic equation and print out the roots for all conditions.
This program demonstrates how to use a simple function, the use of conditional statements, mainly the `if : elif : else:` construct, and how to deal with the various input conditions.
### Task
Write a Python program to read in the three floating point coefficients a, b and c of the quadratic equation:
$a x^2 + b x + c = 0$
then calculate and display the roots using the standard formula for roots of quadratic being,
$r_i = \frac{ −b± \sqrt{b^2−4ac}}{2a}$
The program is expected to deal with conditions of
1. two real roots
2. two complex roots
3. single real root
Your program must contain a function to calcuate the discriminant, d=$b^2$−4ac
and must have a main(): method. All variables must be declared inside a function or main().
### Background
In addition to the sections for Checkpoint 1, you will need to have read and studied the following additional sections:
1. [Basic Functions](../CourseNotes/basicfunctions.ipynb)
2. [Conditionals](../CourseNotes/conditionalstatements.ipynb)
Note: the structure of this program is more complex than it initially looks; plan out what you are going to do before you start coding and also check it a bit at a time.
### Checkpoint
- Check your program with the following values:
- a=2.0, b=4.0 and c=−8.0
- a=2.0, b=−4.0 and c=8.0
- a=2.0, b=4.0 and c=2.0
- a=0.0, b=6.0 and c=8.0
Ensure you get sensible answers for the four cases. Remember when a=0.0 you will get a linear equation with a single root. You should handle this correctly.
Ensure you get sensible answers for the four cases. **Remember when a=0.0 you will get a linear equation with a single root. You should handle this correctly.**
> #### Note:
> You should think about using the `cmath.sqrt` and `math.sqrt` functions to deal explicitly with square roots of complex and real numbers respectively.
- Call a demonstrator, show them your code and run your program with the four sets of inputs above and any additional ones requested by the demonstrator.
- Ensure that the demonstrator finds you in Learn. Please check that your pass/fail (0/1) has been entered correctly (you should be able to see this in Learn).
### Assessment
To pass this checkpoint:
- The program must work correctly for at least three of the above four cases.
- The prompting is sensible.
- The code structure uses a function to calculate the discriminant and has a main(): function.
- The code uses conditional statements `(if : elif : else:)` to test for the various cases.
### Next steps
Once you have completed this checkpoint, please continue with the [Week2](../WeeklyTasks/Week2.ipynb) tasklist.
%% Cell type:code id:883eb6c3 tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment