In order to solve the problems we're going to come across.
We need to come up with a language or writing algorithm as a way to write down or algorithms so it's
logical and easy to follow.
Last time I said we should first write her algorithms in a way that's easy for us humans to understand
and we could write them in English or whatever language is easiest for you and we will do this in some
parts.
But I want to start transitioning into a real computer language and get used to how it will really work.
We're going to go through some problems and examples and come up with the constructs we'll need for
our algorithms language.
So here's our first problem.
It says writing algorithm that gets Celsius value from the user and outputs that Celsius value in Fahrenheit.
So like I said before the first thing we need to do is understand the problem.
So the first thing it says is get the Celsius value.
So we need some kind of way to get things from the user.
So our very first part of the algorithm will be it's going to say we're just going to start in English
here and get the Celsius.
Now you
user.
So that's the very first thing we should do.
Now a bit more subtly We need to somehow save that Celsius value so we can actually use it.
So to say store
Celsius
now you in two
and sign it two values see.
So we need some kind of way of both naming our values and giving them some and giving them value.
So that's what the sign is it gives it the value and sees just the name of our value that we're going
to use.
This is just like variables in math.
Let's say so.
Next we need to calculate the Fahrenheit value as we know we know see now we have it.
So we need to actually do the calculation so we know that see times 9
over 5 plus 32 will give us the Fahrenheit that.
So now we know that we need some kind of semblance of doing arithmetic operations like multiply divide
addition and subtraction.
So the next thing we need to do is sign store
value from three
step three in which to say the value
right.
So we're kind of doing what we did in step 2 but now we're going to make a new value called afferent
a sign at 3s.
Step three.
So the last thing we'll do is output
Okay.
Now we have to ask yourself does this solve the problem.
So let's go through one more time let's get the Celsius value from user in store that sells its value
and assign it to some value.
See then actually do the calculation and assign and store the value from our previous step into the
value.
And then finally output so does this solve the problem.
I think so.
I think we're I think this works and it's good enough for us to have as an algorithm so we can translate
it to a computer.
So what are the takeaways.
At are needed or that we can add to our language well we need some way
of getting input.
Right now we'll just say
something like get input as our construct number one.
Number two we need some way of
naming and storing values.
That's what we didn't stop to remember.
And step four and we need
arithmetic
expressions
like we did in part 3.
These are just the math symbols
and they'll be a few more that we'll need but for now this is this works.
And lastly we need
some way of outputting
results.
So if you remember in the mental model lecture this works exactly.
The way that I outlined.
So we have the input that's that's one here.
One this this line and then we actually go into the algorithm which is step two step three and step
four and then we have the output which is the last part of our mental model.
So see how this mental model really applies here.
So let's go to a new problem.
So say rates an algorithm
that takes two numbers from the user and outputs the bigger number
so using what we have from one problem one is to
you know how to start.
So get a number from the user an sign that each to call it and one we have to choose that.
And then again get a number from the user and sign it
into.
So those are the first two things we need to numbers so we have going into those numbers.
Now we need some way of comparing two values.
So if you make a comparison between one and 2 let's say we checked this comparison.
This gives you well it's either true or false.
This evaluates either true or false.
Either one is greater than two or it's not.
So this is our very first condition and conditions either evaluate teacher or false.
Like I said just like in math we can we can do this kind of comparison with in this case the greater
than sign is the greater than sign.
Not only that though we need some kind of control flow in this case.
It can either be an one that's bigger than two or it can be in two that's greater than in one in thus
we have two different conditions.
So in this case let's say we have and one is greater than two.
So we would output
and want but there's a different condition that says and 2 is greater than 1 and then we would output
in two so see how we have two different paths of execution.
This is called so or two different cases.
This is case one
and this is case two.
Two.
So we need to somehow have a way of deciding which one it is and how we're going to simplify this is
I'm going to say if
anyone is greater than two then what output and one and this will be all part of the same expression.
So to say otherwise
output into should have then output in one.
So this is one of the tougher things to explain actually actually but just know that there's there's
different paths.
If you have a condition like this this is our condition.
In one it's created into
so that's really it that's our outrage.
So let's see.
Does this work.
So get the number from the user and one and then get another number from the user say and two and then
we'll spell check if and one is greater than and two then output and one.
Otherwise I'll put into this I think solves the problem that is that was asked of us.
So what are the takeaways from here.
So rhenium some conditional
statements that's evaluate to TRUE or FALSE.
That's the condition.
Take away five say take away six.
We need some kind of
control flow
for the different cases.
That we can have
based on condition.
Don't worry too much if you're not completely following this yet we'll be doing a lot of examples.
So you'll you'll get it.
So let's move on to the last problem.
Do this lecture this problem re
consumer space
here.
So given the word
Mississippi
count how many
S's here.
In the word
create algorithm.
So you do.
So of course we know just by looking how many S's there should be it should be only for but that's using
our eyes and computers don't have eyes so we need to somehow have an algorithm that will actually go
through and count how many S's that appear.
So how would we start this well.
We would need some kind of counter to actually count.
So let's say counter jurors see will assign value zero for now.
Right.
Because we started zero.
You don't know how many is here yet.
So counters see assigned to value zero.
So the second step we will start at the beginning
and sign current search letter.
This will be of value to be the first letter in the word.
Right.
So the first letter in the word is M. in we will name the current letter to me.
Let's just call it El.
The first letter is L..
So then we'll have to go through the entire word
in check
if current word or current letter is an --.
Right.
So this is the condition just like we had in problem too.
So we have a condition here.
So if you have a condition then you may have multiple cases so let's just say if current letter
now
is as
then what you should increase the count right.
Crease C by 1 so you add one in as we see in X
and then I guess go to the next letter
and sign it to L. right.
Else you can get the next letter in the word.
So
m so started em good eye or count would not have increased yet.
CS The first asks the count goes to one second as the count goes to season I doesn't count anything
at all.
And then sees an excess of 3 4 and then nothing else right.
Because none of these letters are in S So the takeaway from this is not some kind of way to weigh you
do you know steps multiple times in programming.
Call this a
so we loop through each letter of the word and runs some some steps.
So in this case really the step that we're going to be doing is doing this case in this case doesn't
have an otherwise
there's no otherwise do something we don't really care if it's not an X we just want to keep going.
Great.
That's really all we need for now.
In Don't worry about if you're not really understanding this we'll we'll be going over this a lot and
doing a lot of practice.
So in the next lecture will dive a bit even deeper and really start to formalize this language.
Given the takeaways that you've learned.