WEBVTT

00:04.640 --> 00:08.470
We're at the end of the section so we should be doing some practice now.

00:09.310 --> 00:10.900
Answer problem one.

00:10.930 --> 00:16.360
So how would you rate a two tier a using only a one tier right.

00:16.660 --> 00:20.680
What extra functions might you need to compress this.

00:20.780 --> 00:29.780
So it's asking instead of having something like this we have you know X ever

00:35.690 --> 00:37.890
whatever these values are.

00:38.130 --> 00:39.030
It doesn't matter.

00:39.080 --> 00:44.670
This doesn't have to be the same as this one or he needs could be different values.

00:44.660 --> 00:46.890
In this case this is the same.

00:47.180 --> 00:55.820
So how could I make it how could I make it an array kind of operate in a 2D array only using a 1 year.

00:56.340 --> 00:57.400
So and So.

00:57.540 --> 01:01.770
So how would you kind of start this.

01:01.870 --> 01:06.310
How many elements are in this or this 2d area.

01:06.880 --> 01:09.720
Well in this case it would be max times max right.

01:09.730 --> 01:19.710
So maybe 100 in this case because there is a max number of columns and max number of rows 100.

01:20.020 --> 01:24.780
Just like here there's three columns and three rows so there's nine elements all together.

01:24.940 --> 01:32.490
So to have the same amount for just one day you're able to do this.

01:32.550 --> 01:39.070
So to multiply how many rows we have with how many columns we have.

01:39.520 --> 01:40.690
OK.

01:40.710 --> 01:49.960
So that will give us the same amount of space but now how do you index into it.

01:50.170 --> 01:50.860
Right.

01:50.860 --> 02:01.630
Because before we could just say OK rows 0 and column 1 would give us this top middle you know in this

02:01.630 --> 02:08.700
case top middle element but we can't do that with a singler.

02:09.130 --> 02:17.040
Well if we kind of think of it this way where this would be the first element.

02:17.710 --> 02:21.070
And this would be the second element.

02:22.660 --> 02:25.160
Third element right.

02:25.390 --> 02:26.920
This would be the fourth

02:30.310 --> 02:31.380
six

02:34.390 --> 02:38.410
seven eight and nine.

02:38.670 --> 02:50.500
And in terms of indices k then how would we take a row and column index and turn it into one of these

02:50.500 --> 02:51.390
values.

02:51.870 --> 02:59.920
We kind of readjusted the array and put three four or five right after this.

02:59.950 --> 03:02.720
And 6 4 6 7 8.

03:02.740 --> 03:07.940
Right after this one it be kind of one long contiguous array.

03:07.960 --> 03:15.880
I just learned about four but how do we take a column index in a row index and turn it into one of these

03:15.880 --> 03:16.980
indices.

03:17.380 --> 03:30.790
Well we need a function for this and say in its turn the actual linear index and take in the row and

03:30.790 --> 03:39.270
column index and we need one more thing which is the number of columns

03:45.120 --> 03:49.240
so need this.

03:49.460 --> 03:50.490
It's.

03:50.750 --> 03:55.150
It's and how would we do this.

03:55.160 --> 04:05.260
Well if we take a look at this say we wanted this one and we wanted yes.

04:05.310 --> 04:09.170
The fourth element Well what would that be.

04:09.170 --> 04:14.540
So the column would be 0 and the row would be 1.

04:14.840 --> 04:28.410
So if we know how how kind why this is how many columns or three in this case three times the row would

04:28.410 --> 04:31.270
give us three right.

04:31.650 --> 04:34.230
But let's say we wanted this one for.

04:34.450 --> 04:34.930
Wow.

04:34.950 --> 04:37.900
Three times the row.

04:38.130 --> 04:39.150
Three times one.

04:39.150 --> 04:45.630
In this case would still give us this one but maybe we could add a column to it.

04:45.630 --> 04:56.400
So since we wanted column 1 and row 1 which is 4 3 times the rho should be 3 in this case three times.

04:56.400 --> 05:01.460
This one here plus one would give us four day.

05:01.490 --> 05:05.750
So we wanted this one we'd say three times in a row.

05:07.590 --> 05:11.360
Which is three plus two was five right.

05:12.520 --> 05:20.950
So if you want to add seven here we say three times the row which is two times three which is six plus

05:20.950 --> 05:22.590
the column is 1 right.

05:22.600 --> 05:36.060
So essentially all this comes is C plus number of columns times the RHO may give us the index.

05:36.060 --> 05:40.070
So it's in fact it's change this.

05:40.230 --> 05:45.690
So now it's actually this example and say

05:50.350 --> 05:56.730
water equals to zero eyes.

05:56.970 --> 06:01.960
Max strange I'd be our row

06:05.480 --> 06:07.830
and Arpels gloss

06:10.540 --> 06:11.240
over it.

06:11.240 --> 06:17.950
It's see columns X plus.

06:18.490 --> 06:21.790
And we actually say Ray

06:24.760 --> 06:34.760
and call the function here aren't C X equals to index cards.

06:36.020 --> 06:36.670
Max

06:39.690 --> 06:43.790
this Ouch he

06:47.450 --> 06:48.200
really

06:52.930 --> 06:54.520
Index.

06:54.790 --> 06:56.410
SEE IT'S

07:15.590 --> 07:16.480
can.

07:16.750 --> 07:26.980
So if you run this you will see so in fact we probably do.

07:28.940 --> 07:30.170
This a little bit differently.

07:30.260 --> 07:33.270
Should say ouch.

07:33.320 --> 07:35.630
R is

07:39.070 --> 07:46.050
R C.

07:48.060 --> 07:48.510
No

07:51.080 --> 08:15.630
an s s rs are 0 0 is array of 0 0 0 are 0 see 1 8 1 1 8 1 and we go down the last two and 2 8 right.

08:17.660 --> 08:20.780
2 1 7 right.

08:20.840 --> 08:26.810
So this is one way you can make a 2D or a using a 1 year.

08:27.170 --> 08:31.240
So I just want to show you how that worked.

08:32.610 --> 08:36.240
So move on to the next one

08:39.510 --> 08:50.010
and says Right a mini calculator program that takes in a character so add minus multiply divide and

08:50.010 --> 08:52.530
two integers and outputs the results.

08:52.530 --> 08:55.260
Use function pointers for this.

08:55.350 --> 09:04.150
So let's do the first thing which is to let's make a may call the function.

09:04.150 --> 09:13.420
So let's say add x y is just two kind of operands that operate on

09:20.750 --> 09:21.250
Multiply

09:35.490 --> 09:37.190
this way.

09:37.320 --> 09:40.570
So these are your Medich functions I have.

09:40.650 --> 09:44.720
And what's it's like to use typedef

09:48.380 --> 09:54.600
and I'll show you something interesting the typedef function pointers.

09:55.510 --> 10:02.240
Sean you say that it's a member function pointers we need

10:06.150 --> 10:11.310
to have a proper signature.

10:11.400 --> 10:11.950
Right.

10:12.040 --> 10:24.800
So this will spel this will kind of be the same as these function signatures right.

10:24.870 --> 10:31.940
So it turns it into it takes two ints as arguments that matches all these four functions.

10:32.250 --> 10:34.370
Now a typedef.

10:34.650 --> 10:40.790
You'll notice that it doesn't really follow the same typedef rule that we were used to.

10:41.910 --> 10:52.230
And really this type def will make it so that this will be the type name.

10:52.320 --> 10:58.700
So it kind of works a bit differently with function pointers and this will be taken as the type.

10:59.180 --> 11:07.020
So that means that we can have this function pointer as our type.

11:07.040 --> 11:20.230
So we say arithmetic is function pay is the return type of my function.

11:20.470 --> 11:30.810
So get a medic in hand will take the character as our operations.

11:30.810 --> 11:35.860
So this is what this is going to do is it's going to take whatever the inputs.

11:35.880 --> 11:43.400
So one of these characters is going to return the function that should be used with that operation.

11:43.560 --> 11:44.180
OK.

11:45.680 --> 11:47.500
So it's implement this

11:50.390 --> 11:52.680
sort of here.

11:54.670 --> 11:55.550
And easily.

11:55.590 --> 11:58.210
All right turn x y

12:02.890 --> 12:03.240
and

12:06.820 --> 12:09.400
turn minus Y

12:14.890 --> 12:15.970
times Y or Z

12:21.040 --> 12:23.160
in or turn X to find it.

12:23.440 --> 12:25.920
We're not going to worry about divide by zero here.

12:26.010 --> 12:26.640
OK.

12:27.880 --> 12:30.890
And this one right here.

12:35.610 --> 12:43.400
So for this one we're actually going to use a switch statement and switch on the operation and we're

12:43.400 --> 12:44.420
going to say

12:49.830 --> 12:54.550
it's case.

12:55.520 --> 13:01.030
Plus you will return at

13:10.570 --> 13:13.060
case minus

13:19.570 --> 13:24.080
case multiply returned by

13:31.400 --> 13:35.030
in case vide

13:44.220 --> 13:54.510
and at the end say well this is never going to happen but say at the end just in case.

13:55.220 --> 14:02.900
So now we need to actually

14:05.890 --> 14:15.230
get all the input and the calculations are going to say see each

14:19.530 --> 14:23.210
say Please put it's

14:25.780 --> 14:38.710
two up or ants it's in this tween.

14:38.910 --> 14:40.500
He will see in.

14:40.630 --> 14:43.030
You need two operands.

14:43.050 --> 14:55.070
Say x 1 and x 2 at an x one it's too much to worry about.

14:55.470 --> 15:03.850
But there is this one and we also need the operation.

15:03.850 --> 15:04.920
So

15:07.170 --> 15:19.140
say send out these inter Krishna.

15:19.610 --> 15:20.680
That's

15:34.480 --> 15:41.230
that need a char operation.

15:43.070 --> 15:44.830
So in the op

15:48.530 --> 16:04.590
and then we actually say Medich function is the OP function or the function pointer and we'll say get

16:05.380 --> 16:06.580
a medic

16:10.850 --> 16:11.450
using the

16:14.560 --> 16:16.750
and then say see out

16:19.830 --> 16:30.890
x 1 space and then the OP Space X 2

16:34.530 --> 16:35.580
goals to

16:37.890 --> 16:38.670
function

16:41.210 --> 16:42.710
x 1 x 2

16:48.150 --> 16:48.740
Cance.

16:48.800 --> 16:52.500
Let's run this.

16:52.540 --> 16:57.310
So say five to six

16:59.910 --> 17:03.360
in this

17:06.410 --> 17:11.430
to do that anyway and or just plain that.

17:11.620 --> 17:13.820
So five or six is luck.

17:14.050 --> 17:15.710
Right.

17:15.750 --> 17:16.140
Right.

17:16.160 --> 17:16.720
Again

17:20.500 --> 17:27.520
in you know 10 plots or 10 minus three say

17:29.950 --> 17:31.250
3:7.

17:31.540 --> 17:33.870
So it's seemingly working.

17:33.880 --> 17:34.650
Right.

17:34.710 --> 17:45.030
We're using the function pointers and use typedef to actually make a type called function pointer.

17:45.330 --> 17:45.990
Right.

17:46.030 --> 17:47.110
Or a medic function.

17:47.110 --> 17:49.010
In this case.

17:49.300 --> 17:52.120
So we can actually return that type.

17:52.300 --> 17:53.410
So we don't have to do.

17:53.410 --> 17:59.250
So that's one nice thing about typedef is it kind of simplifies function pointers.

17:59.290 --> 18:02.460
You don't have to type this whole thing again.

18:02.530 --> 18:05.320
Really just say the type that we want

18:07.940 --> 18:16.700
so hopefully that made sense and I will see you in the next section when you will start our battleship

18:17.350 --> 18:18.420
projects.
