This is the last lecture of this section.
So that means we should be doing some practice problems.
So let's start with problem one.
What does result evaluate to us.
We have some int x is called the Zero and Y is equal to 20.
We have a boules head which is equal to true and W which is equal to false.
So what would results evaluate.
So we have w so w.
So not false is true or true.
So this whole thing is true here.
True or true is true and we're ending this with STR in inside these parentheses.
So why is greater than x y is 20 so 20 is definitely greater than zero.
And Zed is true.
So not Zed is false so true and false is false.
But then we negate it.
So true and true is true.
So if you run this should see this is true.
So Gallia result is 1.
So it's true.
So let's move on to the next problem.
So write a program that finds all the roots of the quadratic equation.
So a quadratic equation is in the form of X squared plus B X C equals zero.
And we'll remember at how you get the roots are B minus or negative be sorry plus square root of B squared
minus 4 A C all over today and negative b minus a square root of B squared.
Minus 4 A C over all over 2.
So let's try to unpack this a little bit so we know that the quadratic equation can have either 1 or
2 real or complex roots depending on the discriminant and the discriminant.
Is this Partch.
It's whatever's under underneath the square root side.
So say this was negative then this would be an imaginary number.
So this whole thing would be imaginary but if it's positive then this is a real number.
And if this is ZERO say these are exactly the same then this whole thing would be zero.
So we kind of have three different cases.
Case one
is discriminant.
Is positive meaning it's a real real Rich case to
is the discriminants is zero.
In case three is the in CH is
so we know now that we two have some kind of if statement and that condition will be whether the discriminant
is positive negative or zero.
So we will need a in this statement it's just the way.
So how will this work.
Well or you need some very well Seer's.
It's a
in the C.
So this will match with the A here B here and see here and we're going to need some floats for the roots.
One two
one
you
in say the imaginary right now
and we also need the discriminant is while
we know what the discriminant should be say it's imminent.
Sequel to the times b actually before I'll just finish this line four times.
A C before this we need to actually get a B and C S A C out integer values A E
and C.
The dreaded
equation.
A X power to us e times x plus c 0.
So we need to see in three different E's here now throw floats because Ewing's division.
So we get a B and C we determine the discriminant.
Now we have to check to see if the discriminant
is greater than zero.
That means positive positive case right.
So we have we know how to do this one.
It's Roueche of negative B
plus.
But we don't know how to square root something.
So this is kind of a problem.
So in fact we can actually do our very first extra include and often include something called C math
in C math has a bunch of math functions that we can use in those math functions is called s u r th square
root.
So we need the square root function to see her him and then each
hardware spill over
all over 2 times a.
This is just us following the formula that was outlined right here minus B plus square root the discriminant
which is the squared minus for a C over to.
Hey that's the very first one.
In ch
to
minus B minus where.
Is permanent.
Which over
Interesse C out.
To sting.
Real roots this
one and
two
okay in either case is house if it is permanent.
Is less than zero.
So it's negative here.
Which means you have some imaginary number.
Right.
So a square root some negative value.
You get an imaginary number.
So Roueche one equals two minus the over two times a.
It's just part of the root
Ch 1.
So they're both the same now and then this imaginary parch
called square root.
The positive version of the discriminant right a meta make negative mean discriminant is already negative
so we make that positive by by putting this minus sign in front of it.
So square the discriminant over two times.
Again we're just are really just following the formula.
No real difference.
And then we say C ouch
to this thing.
Complex roots
just
she is
one
and then last I
imaginary
number and
two
minus
the imaginary number.
So again we're just really just following the formula that was outlined here.
And we're just doing decisions based on the discriminant.
Otherwise it has to be zero.
Right.
So Roueche 1 equals
Nate e over to a a.
And that's it.
Because if this is zero this whole part is zero Roueche two equals one they're the same
so see Ouch two equal
Greil
reduce exists.
One
and
Two
you run this
it's bigger so you just give it some random values.
1 0.
So those are two real roots and it's zero point five
see could we make this zero.
B was
you
two two and one Verio two distinct complex roots negative two plus imaginary I 0.5 in needed two is.
I see your point but so this should be working.
Think so.
Move on to the next one.
Each program that enters and number and prints out whether it's even or not use a switch.
Get rid of this.
So we need a number of cells just say number zero say.
Enter any number.
Sure
if it's even more odd
that get that number
and then we'll see two results here.
Equals to number modulus 2.
So remember what modulus was it gets the remainder of the division.
So whatever the number is it'll be divided by two and then the remainder will get set to result.
So the remainder can only be two values here.
So it could either be 0 or 1.
So if you say switch on the results
and then you have case 0 this means it would be even right evenly divides there's no remainder number
is even
in break and you need those breaks.
Case 1 is out.
Number is odd because it had a remainder.
There is a one remaining brick on this.
So 31 numbers on it's right in say 20.
So the numbers even so memory that modulus gets the remainder of the division between the two numbers.
And this is an integral type.
It's just an integer.
So you can switch on it and you have cases which is consonant expressions.
So 0 and 1 are constant expressions and all match which one fits best.
And we break because we don't want to fall through and do the cases below.
And notice we don't have a default here because we don't really need it.
So that's it.
I hope you do the practice problems in the next lecture and I'll see you in the next section.