We're at the end of the section so we should be doing some practice now.
Answer problem one.
So how would you rate a two tier a using only a one tier right.
What extra functions might you need to compress this.
So it's asking instead of having something like this we have you know X ever
whatever these values are.
It doesn't matter.
This doesn't have to be the same as this one or he needs could be different values.
In this case this is the same.
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.
So and So.
So how would you kind of start this.
How many elements are in this or this 2d area.
Well in this case it would be max times max right.
So maybe 100 in this case because there is a max number of columns and max number of rows 100.
Just like here there's three columns and three rows so there's nine elements all together.
So to have the same amount for just one day you're able to do this.
So to multiply how many rows we have with how many columns we have.
OK.
So that will give us the same amount of space but now how do you index into it.
Right.
Because before we could just say OK rows 0 and column 1 would give us this top middle you know in this
case top middle element but we can't do that with a singler.
Well if we kind of think of it this way where this would be the first element.
And this would be the second element.
Third element right.
This would be the fourth
six
seven eight and nine.
And in terms of indices k then how would we take a row and column index and turn it into one of these
values.
We kind of readjusted the array and put three four or five right after this.
And 6 4 6 7 8.
Right after this one it be kind of one long contiguous array.
I just learned about four but how do we take a column index in a row index and turn it into one of these
indices.
Well we need a function for this and say in its turn the actual linear index and take in the row and
column index and we need one more thing which is the number of columns
so need this.
It's.
It's and how would we do this.
Well if we take a look at this say we wanted this one and we wanted yes.
The fourth element Well what would that be.
So the column would be 0 and the row would be 1.
So if we know how how kind why this is how many columns or three in this case three times the row would
give us three right.
But let's say we wanted this one for.
Wow.
Three times the row.
Three times one.
In this case would still give us this one but maybe we could add a column to it.
So since we wanted column 1 and row 1 which is 4 3 times the rho should be 3 in this case three times.
This one here plus one would give us four day.
So we wanted this one we'd say three times in a row.
Which is three plus two was five right.
So if you want to add seven here we say three times the row which is two times three which is six plus
the column is 1 right.
So essentially all this comes is C plus number of columns times the RHO may give us the index.
So it's in fact it's change this.
So now it's actually this example and say
water equals to zero eyes.
Max strange I'd be our row
and Arpels gloss
over it.
It's see columns X plus.
And we actually say Ray
and call the function here aren't C X equals to index cards.
Max
this Ouch he
really
Index.
SEE IT'S
can.
So if you run this you will see so in fact we probably do.
This a little bit differently.
Should say ouch.
R is
R C.
No
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.
2 1 7 right.
So this is one way you can make a 2D or a using a 1 year.
So I just want to show you how that worked.
So move on to the next one
and says Right a mini calculator program that takes in a character so add minus multiply divide and
two integers and outputs the results.
Use function pointers for this.
So let's do the first thing which is to let's make a may call the function.
So let's say add x y is just two kind of operands that operate on
Multiply
this way.
So these are your Medich functions I have.
And what's it's like to use typedef
and I'll show you something interesting the typedef function pointers.
Sean you say that it's a member function pointers we need
to have a proper signature.
Right.
So this will spel this will kind of be the same as these function signatures right.
So it turns it into it takes two ints as arguments that matches all these four functions.
Now a typedef.
You'll notice that it doesn't really follow the same typedef rule that we were used to.
And really this type def will make it so that this will be the type name.
So it kind of works a bit differently with function pointers and this will be taken as the type.
So that means that we can have this function pointer as our type.
So we say arithmetic is function pay is the return type of my function.
So get a medic in hand will take the character as our operations.
So this is what this is going to do is it's going to take whatever the inputs.
So one of these characters is going to return the function that should be used with that operation.
OK.
So it's implement this
sort of here.
And easily.
All right turn x y
and
turn minus Y
times Y or Z
in or turn X to find it.
We're not going to worry about divide by zero here.
OK.
And this one right here.
So for this one we're actually going to use a switch statement and switch on the operation and we're
going to say
it's case.
Plus you will return at
case minus
case multiply returned by
in case vide
and at the end say well this is never going to happen but say at the end just in case.
So now we need to actually
get all the input and the calculations are going to say see each
say Please put it's
two up or ants it's in this tween.
He will see in.
You need two operands.
Say x 1 and x 2 at an x one it's too much to worry about.
But there is this one and we also need the operation.
So
say send out these inter Krishna.
That's
that need a char operation.
So in the op
and then we actually say Medich function is the OP function or the function pointer and we'll say get
a medic
using the
and then say see out
x 1 space and then the OP Space X 2
goals to
function
x 1 x 2
Cance.
Let's run this.
So say five to six
in this
to do that anyway and or just plain that.
So five or six is luck.
Right.
Again
in you know 10 plots or 10 minus three say
3:7.
So it's seemingly working.
Right.
We're using the function pointers and use typedef to actually make a type called function pointer.
Right.
Or a medic function.
In this case.
So we can actually return that type.
So we don't have to do.
So that's one nice thing about typedef is it kind of simplifies function pointers.
You don't have to type this whole thing again.
Really just say the type that we want
so hopefully that made sense and I will see you in the next section when you will start our battleship
projects.