WEBVTT

00:00.220 --> 00:03.910
This is the last lecture of this section.

00:03.960 --> 00:07.130
So that means we should be doing some practice problems.

00:07.470 --> 00:11.070
So let's start with problem one.

00:11.110 --> 00:13.540
What does result evaluate to us.

00:13.570 --> 00:22.290
We have some int x is called the Zero and Y is equal to 20.

00:22.320 --> 00:30.330
We have a boules head which is equal to true and W which is equal to false.

00:30.390 --> 00:34.570
So what would results evaluate.

00:35.160 --> 00:38.230
So we have w so w.

00:38.370 --> 00:41.960
So not false is true or true.

00:42.030 --> 00:45.960
So this whole thing is true here.

00:47.990 --> 00:57.110
True or true is true and we're ending this with STR in inside these parentheses.

00:57.360 --> 01:07.340
So why is greater than x y is 20 so 20 is definitely greater than zero.

01:08.030 --> 01:12.480
And Zed is true.

01:12.480 --> 01:19.150
So not Zed is false so true and false is false.

01:19.880 --> 01:21.160
But then we negate it.

01:21.170 --> 01:25.730
So true and true is true.

01:25.730 --> 01:33.440
So if you run this should see this is true.

01:33.540 --> 01:36.690
So Gallia result is 1.

01:36.770 --> 01:40.180
So it's true.

01:40.200 --> 01:43.400
So let's move on to the next problem.

01:43.400 --> 01:48.440
So write a program that finds all the roots of the quadratic equation.

01:48.440 --> 01:55.550
So a quadratic equation is in the form of X squared plus B X C equals zero.

01:56.000 --> 02:09.930
And we'll remember at how you get the roots are B minus or negative be sorry plus square root of B squared

02:10.830 --> 02:22.840
minus 4 A C all over today and negative b minus a square root of B squared.

02:23.790 --> 02:28.380
Minus 4 A C over all over 2.

02:28.770 --> 02:42.060
So let's try to unpack this a little bit so we know that the quadratic equation can have either 1 or

02:42.060 --> 02:50.070
2 real or complex roots depending on the discriminant and the discriminant.

02:50.070 --> 02:51.860
Is this Partch.

02:52.350 --> 02:56.630
It's whatever's under underneath the square root side.

02:56.660 --> 03:03.800
So say this was negative then this would be an imaginary number.

03:04.750 --> 03:13.690
So this whole thing would be imaginary but if it's positive then this is a real number.

03:14.400 --> 03:23.470
And if this is ZERO say these are exactly the same then this whole thing would be zero.

03:23.490 --> 03:27.810
So we kind of have three different cases.

03:27.840 --> 03:29.040
Case one

03:31.660 --> 03:33.410
is discriminant.

03:36.870 --> 03:47.690
Is positive meaning it's a real real Rich case to

03:50.670 --> 03:55.710
is the discriminants is zero.

03:57.510 --> 04:07.820
In case three is the in CH is

04:12.930 --> 04:23.890
so we know now that we two have some kind of if statement and that condition will be whether the discriminant

04:23.890 --> 04:27.080
is positive negative or zero.

04:27.370 --> 04:39.420
So we will need a in this statement it's just the way.

04:39.820 --> 04:43.410
So how will this work.

04:45.450 --> 04:48.340
Well or you need some very well Seer's.

04:48.410 --> 04:49.590
It's a

04:51.970 --> 04:56.110
in the C.

04:56.380 --> 05:07.130
So this will match with the A here B here and see here and we're going to need some floats for the roots.

05:07.560 --> 05:10.530
One two

05:15.370 --> 05:16.000
one

05:19.120 --> 05:19.790
you

05:22.100 --> 05:27.480
in say the imaginary right now

05:31.910 --> 05:37.390
and we also need the discriminant is while

05:44.240 --> 05:50.300
we know what the discriminant should be say it's imminent.

05:51.670 --> 06:01.630
Sequel to the times b actually before I'll just finish this line four times.

06:04.630 --> 06:20.500
A C before this we need to actually get a B and C S A C out integer values A E

06:23.480 --> 06:26.170
and C.

06:26.540 --> 06:29.250
The dreaded

06:32.540 --> 06:33.700
equation.

06:37.030 --> 06:49.220
A X power to us e times x plus c 0.

06:49.250 --> 07:02.550
So we need to see in three different E's here now throw floats because Ewing's division.

07:02.790 --> 07:06.750
So we get a B and C we determine the discriminant.

07:06.750 --> 07:11.170
Now we have to check to see if the discriminant

07:15.040 --> 07:16.320
is greater than zero.

07:16.330 --> 07:23.550
That means positive positive case right.

07:24.360 --> 07:28.820
So we have we know how to do this one.

07:28.980 --> 07:32.130
It's Roueche of negative B

07:35.920 --> 07:37.100
plus.

07:37.430 --> 07:40.780
But we don't know how to square root something.

07:40.950 --> 07:43.700
So this is kind of a problem.

07:43.860 --> 07:53.790
So in fact we can actually do our very first extra include and often include something called C math

07:54.920 --> 08:04.980
in C math has a bunch of math functions that we can use in those math functions is called s u r th square

08:04.980 --> 08:05.770
root.

08:06.150 --> 08:11.490
So we need the square root function to see her him and then each

08:15.920 --> 08:18.730
hardware spill over

08:24.050 --> 08:27.020
all over 2 times a.

08:27.280 --> 08:35.750
This is just us following the formula that was outlined right here minus B plus square root the discriminant

08:35.760 --> 08:41.790
which is the squared minus for a C over to.

08:42.150 --> 08:44.590
Hey that's the very first one.

08:47.400 --> 08:49.440
In ch

08:54.060 --> 08:54.580
to

08:57.390 --> 09:01.710
minus B minus where.

09:03.590 --> 09:05.020
Is permanent.

09:05.100 --> 09:08.270
Which over

09:20.130 --> 09:21.660
Interesse C out.

09:23.810 --> 09:28.030
To sting.

09:29.700 --> 09:33.310
Real roots this

09:39.160 --> 09:42.220
one and

09:46.790 --> 09:47.000
two

09:53.500 --> 09:59.860
okay in either case is house if it is permanent.

10:02.270 --> 10:05.330
Is less than zero.

10:05.750 --> 10:07.600
So it's negative here.

10:09.070 --> 10:12.800
Which means you have some imaginary number.

10:12.850 --> 10:13.350
Right.

10:13.490 --> 10:16.690
So a square root some negative value.

10:16.690 --> 10:18.360
You get an imaginary number.

10:18.370 --> 10:33.590
So Roueche one equals two minus the over two times a.

10:33.770 --> 10:35.230
It's just part of the root

10:38.760 --> 10:40.670
Ch 1.

10:40.790 --> 10:45.710
So they're both the same now and then this imaginary parch

10:48.720 --> 10:50.400
called square root.

10:55.300 --> 11:04.300
The positive version of the discriminant right a meta make negative mean discriminant is already negative

11:04.310 --> 11:10.130
so we make that positive by by putting this minus sign in front of it.

11:13.750 --> 11:18.720
So square the discriminant over two times.

11:20.540 --> 11:25.360
Again we're just are really just following the formula.

11:25.380 --> 11:26.810
No real difference.

11:28.070 --> 11:29.780
And then we say C ouch

11:33.010 --> 11:36.600
to this thing.

11:40.670 --> 11:42.330
Complex roots

11:46.410 --> 11:47.090
just

11:53.020 --> 11:54.190
she is

11:57.810 --> 11:58.340
one

12:01.140 --> 12:03.010
and then last I

12:05.690 --> 12:06.770
imaginary

12:09.710 --> 12:12.110
number and

12:16.540 --> 12:17.160
two

12:23.210 --> 12:24.020
minus

12:26.530 --> 12:29.860
the imaginary number.

12:37.170 --> 12:41.380
So again we're just really just following the formula that was outlined here.

12:43.360 --> 12:47.160
And we're just doing decisions based on the discriminant.

12:47.580 --> 12:49.470
Otherwise it has to be zero.

12:49.500 --> 12:50.050
Right.

12:50.920 --> 12:54.670
So Roueche 1 equals

12:58.620 --> 13:07.090
Nate e over to a a.

13:07.280 --> 13:08.660
And that's it.

13:08.900 --> 13:19.880
Because if this is zero this whole part is zero Roueche two equals one they're the same

13:22.440 --> 13:27.410
so see Ouch two equal

13:29.750 --> 13:30.510
Greil

13:32.670 --> 13:35.020
reduce exists.

13:39.810 --> 13:40.410
One

13:43.610 --> 13:44.030
and

13:47.320 --> 13:47.530
Two

13:51.930 --> 13:52.950
you run this

14:00.440 --> 14:05.840
it's bigger so you just give it some random values.

14:06.740 --> 14:09.210
1 0.

14:09.460 --> 14:16.330
So those are two real roots and it's zero point five

14:18.720 --> 14:26.040
see could we make this zero.

14:26.260 --> 14:27.160
B was

14:29.810 --> 14:30.500
you

14:35.200 --> 14:50.840
two two and one Verio two distinct complex roots negative two plus imaginary I 0.5 in needed two is.

14:51.100 --> 14:56.490
I see your point but so this should be working.

14:56.510 --> 14:59.760
Think so.

14:59.760 --> 15:02.260
Move on to the next one.

15:09.680 --> 15:20.170
Each program that enters and number and prints out whether it's even or not use a switch.

15:20.320 --> 15:26.540
Get rid of this.

15:32.550 --> 15:39.360
So we need a number of cells just say number zero say.

15:41.400 --> 15:44.850
Enter any number.

15:45.170 --> 15:45.540
Sure

15:48.740 --> 15:52.250
if it's even more odd

15:58.040 --> 15:59.250
that get that number

16:07.500 --> 16:10.770
and then we'll see two results here.

16:12.080 --> 16:16.930
Equals to number modulus 2.

16:17.510 --> 16:22.780
So remember what modulus was it gets the remainder of the division.

16:22.790 --> 16:30.650
So whatever the number is it'll be divided by two and then the remainder will get set to result.

16:30.650 --> 16:34.290
So the remainder can only be two values here.

16:34.520 --> 16:39.030
So it could either be 0 or 1.

16:39.390 --> 16:42.240
So if you say switch on the results

16:45.830 --> 16:56.060
and then you have case 0 this means it would be even right evenly divides there's no remainder number

16:56.260 --> 16:57.610
is even

17:01.150 --> 17:06.090
in break and you need those breaks.

17:06.090 --> 17:08.870
Case 1 is out.

17:10.520 --> 17:16.190
Number is odd because it had a remainder.

17:16.190 --> 17:23.910
There is a one remaining brick on this.

17:24.340 --> 17:33.120
So 31 numbers on it's right in say 20.

17:33.160 --> 17:43.000
So the numbers even so memory that modulus gets the remainder of the division between the two numbers.

17:43.490 --> 17:45.320
And this is an integral type.

17:45.320 --> 17:46.580
It's just an integer.

17:46.760 --> 17:52.270
So you can switch on it and you have cases which is consonant expressions.

17:52.420 --> 17:58.720
So 0 and 1 are constant expressions and all match which one fits best.

17:58.970 --> 18:04.030
And we break because we don't want to fall through and do the cases below.

18:04.400 --> 18:08.040
And notice we don't have a default here because we don't really need it.

18:09.200 --> 18:10.100
So that's it.

18:10.100 --> 18:16.880
I hope you do the practice problems in the next lecture and I'll see you in the next section.
