So let's use our takeaways we learned from The Last Lecture we really formalize our language into what
is called pseudo.
So it's starch.
So we'll supreme one get two numbers from the user.
One is the length of the base of a triangle.
The other is the height of the trying to calculate the area of the triangle and output so let's say
we use B for base.
This is our let's call the variable and want to use the call sign as assignment and so use a signed
the user inputs
or the base trying.
So this gets the input from the user and assigns it to our new variable The is just a shorthand that
we understand as assignment in storing the value.
So we do the same for each We'll call the heights.
Each user inputs for the base or heights or the trying
next.
You say A is equal to A is the area.
So do simple calculation times each then all over 2.
So that will calculate the area.
China and then simply all we have to do now is it.
So output the.
And that's our algorithm for calculating the area of trying pretty simple.
And now notice we're really trying to formalize and make a shorthand of our algorithm.
So this is what's called pseudo code.
It's a combination of English because we don't know how to get input from a user yet.
So we just use English and actual code that you might see in a C++.
So here is the value that we get for the base of the triangle.
Each is the height and a is the area.
And again we'll use equals as the sign that.
So this whatever this value evaluates to gets assigned to a
A will have that value after this is over.
And we'll be doing a lot more examples.
Let's move on to problem to.
So given the given three side lengths the triangle A B and C output whether the triangle is equal lateral
which means all sides of the same Isosceles which means two sides of the same or scaling meaning none
of the so
here we know we need some kind of condition right.
So we need to say are all of the sides equal.
If so then it's an equilateral triangle.
So how would you do that.
Well let's just say if a
is in parentheses here is equal to.
We'll just use English here.
He could to the
and the is equal to see
then we know that a is also equal to see because it be equal C and A equals B then A also has to equal
C then we'll say one a is out put
that it's lateral
or All right.
OK.
So this whole thing is all line valuate to True or false and it evaluates true if B is equal to A and
B is also a C and then if it is true then it will do this line.
But that it's an equal try
so.
Otherwise
if
a is equal to B or
A is equal to C or B is equal to C
then
it's at it's an sauce so it's right.
So no.
So notice here is that space.
What does this saying it says if a is equal to B meaning these two sides are the same or azy could see
meaning those two sides of the same or Veazey could see which means those two sides of the same.
Any one of these conditions if they're true means that it's an isosceles triangle and there is here
that they can't all be true because otherwise it would have done this.
This first line right here
so we know that they can't all be the same because it would have said with equal lateral trying.
So we know only two or less are equal here.
And there was also that we can you can actually change conditions together.
So this is a condition.
This artery here.
Right there also.
This is condition and this is also a condition in chaining.
So either one of the either one of these is true then we'll do this again don't worry too much will
go much more formally into into conditions later.
But I just want to give you a little bit of exposure to it now so you'll have a better understanding
later.
So otherwise
an output such output at it's scaley
shreiking
because now we know that none of them could be the same because otherwise if one or more were the same
it would have been it would have done this condition great.
So stips this if it's not all equal skips this if two are equal otherwise they don't just say it's descaling
because none are equal.
Hope you're sort of understanding now how how we're going to go about writing these algorithms and solving
these problems.
So let's move on to three
Supremes three says output all even numbers from 1 to 100.
So we need to go through all of the numbers somehow and see if they are even in if they are we have
the output that number so for this we'll need what we talked about last time which is a so we're some
say loop from 1 to 100.
And notice here that this is also a condition but this is a bit of a different condition than the one
we had here.
This is actually the stopping condition
for our if he didn't have this condition or you wrote it incorrectly it could loop forever.
Remember what I said about computers and them being really dumb computer only does what you tell it
to do.
So if you told them to do or told it to loop infinitely it will loop infinity which is not what you
want.
So reading this condition is very important.
And the conditions for loops are always the stopping condition to do it.
Until this is false.
So now what would we need here.
So we need the current number that were at.
So this is seen.
Will just say is the current number.
Not sure how we get that yet.
But it's whatever the next number is.
So it will start at 1 and they go that 2 in and so on.
So we have the current number and then we'll say
if C-n is exactly
divisible by 2 meaning if it's a whole the whole number it's an actual number so 10 divided by two is
five but nine divided by two is 4.5 great which is not a whole number.
So you want it to exactly divisible by two just to divide something by two you know.
3a then
we'll be pre-suppose output.
The current number of your kids we know the current number is divisible by two.
In this case we don't need it Kate another case because we just want to continue on with the loop.
It's not divisible by two so hopefully you're understanding how this is how this is working.
So you have your stopping condition for you.
Luke So we're going to loop until we get to 100.
We somehow get the current number that we're at and then you say if that current number that we're at
is divisible by 2 then we just output that number.
That's all there is to that.
Sue let's move on to problem for
so get the number from the user and output output.
Run up put the sum of all the numbers from one up to and including the inputted number
so we understand the problem first.
We'll get a number from the user and then we'll loop from one up to that number.
And create some from 1 to say and say so in is the number from the user.
The user will input a number to us.
So let's say the sum is something and the sum will start at zero.
So we'll sign our new variable sum to be zero.
Next he will say the from 1 to up to including.
This is our stopping condition for stopping just like we have in problem 3.
Then it's pretty simple.
Say some is equal to
never.
Well for this we need again the
hurt number and Boole's hurt never
like we had in problem three.
Some will be equal to some equals some plus the number.
So whatever the sum was for just add the current number to this up and then sign that value back to
some so we're just adding on CN to this every time.
And those also didn't mention four but I'm indenting four and five here from three.
This means that four and five are inside this loop so the loop only pertains to 4 and 5 steps sporran
by.
And this is important because in Step Five are outside the loop.
Say we're done now.
And then we'll say out.
But some
in that's it
we're done with this.
So I hope this is starting to set in a little bit bring you some more examples and in the next lecture
to hopefully solidify what we've been learning so far.