OK, so let's continue on with our discussions about functions and how they work.
This is part two of us talking about functions.
So first off, we are going to do a practice problem, and this might take a while.
You can take as long as you'd like.
We're stepping things up a bit.
So things in the course are about to get a little bit harder on a little bit harder to understand what's
going to push you to try and challenge you to solve more difficult problems.
And this one might be significantly more challenging than the previous problems that you've encountered.
So I will go ahead and read the description.
I might give a few hints.
And then, of course, I will.
As always, I will go over a solution.
There are multiple solutions, but I will go over my solution for the problem.
So without further ado, make a program that reads the three words which are going to be strings.
Each word is a string.
So three words from user input.
The program should then find the sum of the decimal values of each character in the string and in talking
about each string for each one of the strings.
So that's what this means.
Basically, you're going to sum up all of the decimal values when I say decimal values, think back
to that SCIRI, the ASCII table.
So remember there are numeric integer values for each character, so there's multiple characters that
make up a string, right?
Each one of those characters has an ASCII decimal value.
That can be gotten by indexing a string in a particular position to get the character, so remember
back to those lectures, if you need to head back to those lectures, go ahead and go to those lectures
when I tell you to pause the video so you can refresh yourself on these concepts.
But we are referring to the ASCII table and talk about this.
You're summing up all of those decimal value, so each no value that goes with each character, you
will add those together for the whole string and that will give you a sum for that particular string,
except you must do this for each one of the strings.
And then what you should do after computing the sum for each one of the three strings, you should print
out the string that has the biggest sum, as well as the sum itself.
All right.
So the sum that goes along with the string and the string that has the biggest.
So here's an example I run this program.
I call it func p.
So it asks for three words, right?
And says entire word and press enter.
And so I enter ball and then same thing as a cat.
And then I say zebra.
And it says the string with the largest, some string with largest thumb is the zebra with a sum of
five hundred thirty two.
So this 532 comes from adding up the decimal values of each one of the characters.
So when you have a character, you can actually add it to an integer and like, let's say you have an
integer, the zero, if you add.
A character to it, that integer will now hold the decimal value of that character.
OK, so go ahead and pause the video and see if you can solve this problem.
It is, like I said, quite quite a difficult problem, except you have all the tools that you need
to solve the problem.
So with that, I will give you a few hints of things that you might or might not want to use.
But something that you should definitely use is a function because we're trying to practice practice
functions, OK?
So you should be using functions at least tests have one function.
You don't have to have multiple functions because we haven't talked about using multiple functions yet.
It's really the same thing.
You could just do the same thing we've learned multiple times, so it's not any harder to make another
function.
But I'm not expecting you to do that.
You can just make one function.
You also are going to probably want to use a loop or even potentially multiple loops.
You may or may not want to use an array you don't have to, but you could.
You are probably also going to want to use a conditional statement or multiple conditional statements.
Multiple are not needed, you can use one, but it can also be done by using multiple conditional statements
so you can just use an air for you can use an IS if LCF and else multiple combinations of those things.
So there's a few ways to do it, but I do.
I recommend that you think about using a for loop.
Definitely using a function because we're trying to practice functions and probably using a conditional
statement as well.
Also keeping track of a total kind of variable for the sum, right?
Remember those accumulator variables that we were talking about where you add to them and you can keep
track of a sum?
Also, think about updating a variable variable, right?
So.
You might want to make a variable that can be overwritten with a new value.
OK, so those are my hints, so if if you weren't able to figure it out without the hints, go ahead
and try and figure that out now.
And then I will go ahead and go over.
One of many potential solutions.
So let's head over to the editor to look at a solution.
So I'm going to head into Visual Studio here.
So let's check this out.
I, of course, still have this wrist injury on, so I typed it out already.
I won't keep doing this.
I do have some videos already recorded where I do type stuff out along with me explaining it.
I believe that's a better way to do it.
But right now I just type too slow, so I am going to just go over the code that I've already written.
So I went ahead and made a function here.
I called it get some.
Its purpose is just to get the sum of whatever word is passed to it.
I'm gonna go ahead and put a comment here for that.
So I'm just going to say gets some of the perimeter word and I'm going to say some of the character
tests or values.
Someone put a comment there to show what it does.
So let's leave this function alone for a second, you might have already kind of understood what was
going on here, but I'm going to start down with main because that's where we're kind of starting.
So what I did first was I declared a variable string for a word, and this is representing each one
of the words I'm going to read into, except I'm going my plan was to overwrite the word.
Over and over, just use one variable, she'll see how I do that down here, and this is the second.
I also make a variable called larger string because we care about printing out the largest string,
right?
I made a variable called sum.
That way, I can record the sum coming from the function that I want to do the sum calculation for a
given word.
And then I make a largest variable, and I started out with the value of zero.
So I declare this variable in initialize it with the value of zero.
I do that because I want to be zero is basically the smallest value, and my goal with this variable
is to keep track of the largest thing as we encounter new words and new sums.
This variables purpose is to be able to compare the new sum that we have to see if it's larger than
the previous.
If so, it will update it right?
So keeping track of a maximum value?
That's what this is for.
I chose to do a for loop to be able to cycle through the entering of each word so it would see in over
and over again for exactly three times because the problem statement says that the user should enter
three words.
That's why I started zero and go for I less than three, so I will be zero, then one, then two, and
then it will stop once it is three, so it won't run for the third time.
So it will have three iterations, so this year out just says enter word and press enter, then what
I do is I read in a string.
Basically, it's doing C in into this string variable I created, so it'll take everything that it sees
up until whitespace the first whitespace character.
And then what happens is I call to get some and pass it the string variable.
So let's head up there.
Let's not pay attention to this quite yet, although you probably know what's going on here.
This will get cold.
So I pass the word that just got injured to the user to this function.
So let's head up here.
Here's the variable.
So what I do here is I make a new variable.
Call it some assert equal to zero to start out with as an initialization.
I loop for the length of the word because it's a string, right, so I do string the the word variable
length for the length function, and I say, let's go as long as it's less than length, because remember,
the last character is the word length minus one.
That's why I say, let's go as long as it's less than the word length.
I started zero so we can still capture position zero and I do an A-plus plus, so we're incrementing.
Then what I do is I use this plus equal operator.
So remember, this could also be some equal sum plus word index I use.
The word index I hear is that we're trying to grab each character.
Of the word that was passed to the function.
And add it to are some variable, it's kind of like another total, we keep accumulating things to it.
So it's like, this is that accumulator variable I'm talking about.
It starts off as zero, let's say, for example, we have the word car.
I'm going to find the well, I'm not going to find C++ will find the ASCII decimal value of C and add
it to this integer.
It sees that this is an integer and it's going to add the integer representation of the character C
to some.
So it will be zero plus whatever the value is of C, we will go back for a second iteration.
So now I, as one one, is still less than the length of the car cars three, right?
That's the length.
So then we grab position one of car, which is a right.
I'm using this car example again.
Here I can write it out for you.
Like this?
Now we're on the A.
So we get the value of a and we add it to some.
Same thing we go back again now as to it still is less than the length of the word because of the length
of the word is three.
Now we add the decimal value of our to our sum, we go back up.
Now I is three is the same as the length.
We don't do anything else.
We have ourselves.
We return the sum, right?
So let's head back down now that we have our sum, so we come back to here.
The value is here.
We then store the value in this other some variable that is local to the scope of the main function.
This is not the same as this in memory.
This sum right here is local to the scope of this function, so only this function gets knows about
this variable.
Maine is the only function out of two of these that knows about this variable.
So despite the fact that they have the same name, C++ doesn't see it that way.
It sees them as totally separate variables, except now they will have the same value, right?
This sum was calculated here.
It was passed back down to where we called the function.
So it's right here and then we took that value and stored it in this other some variable.
Then what we do is we have an if statement.
The statement asks the question is some greater than largest?
So it's a condition, right?
If this is true, if some is greater than largest, then we will run the code in here.
Well, what is the largest?
Largest is zero.
So far, we haven't changed it.
This is our first iteration through this for a loop.
I is zero.
We have read in this one first word, we've only encountered one word so far.
And we're asking if the some of that word is greater than zero.
Well, if we entered anything for the word, it's going to be greater than zero, right?
Because every character has a value greater than zero.
Every character in the alphabet has a value greater than zero.
You can look at the ASCII table for that.
So, yes, some will most likely be greater than largest if we entered car.
It definitely will be greater than largest.
So this condition will be true then.
So what do we do?
We say largest is no longer zero.
It's now we'll hold the value of some.
Why are we doing this, because we're keeping track of the largest thing?
Each time we find something bigger, we overwrite our largest variable with that biggest thing that
we just found.
So that's what we do largest is now going to hold the some of the characters of car.
We also know that since we found the largest sum so far, that means that the current word that we just
looked at is the biggest word so far.
So what we do is we update our string, our largest string variable.
This variable keeps track of the largest words so far by largest word.
We don't mean necessarily length, although a longer word normally leads to a bigger sum.
But what we're really saying is the sum of the characters, the number right, the number was larger.
So therefore we should now update the largest string to have this word.
And this word is the actual word that we've sent just recently, right in this iteration of the Loop,
that was the word that now has the largest sum if we entered here.
We record the largest word.
We record that words sum.
These are the current largest things.
We do this for each one of the words, if we find that the next word after car has a bigger sum, then
this will be true once again and we will update it.
We will overwrite.
These some four car with the sum for this new word.
We will also overwrite.
The largest string, which previously contained car now is going to contain this latest word that we
looked at.
So hopefully that makes sense.
I'm trying to explain it as clearly as possible.
I tried to do this in a somewhat efficient manner.
You may have to have a totally different approach to this.
It could have involved a lot more conditional statements.
It may have not involved a loop right here.
It may have only had a loop right here.
It may have somehow had no loops and it was just like totally hardcoded saying that's fine as long as
you solved it.
That's great.
If you didn't solve it, don't worry.
Just like I say every time, this was actually a really hard one.
So this is like an extra hard one this time.
So if you surprised it, if you saw that, I'm actually really surprised and I give you ultra props
for solving, this is how I did it.
You do not have to do it like this, but I wanted to show you this way of doing it.
Of course, once we're done going through all three words by using this for loop, we now have them
stored and we have the answers stored in this variable and this variable.
So therefore we have a see out statement that prints both of these variables because they both have
the answers.
OK, so hopefully that was enough of an explanation for you.
So like I said, if you did it a different way, that's totally fine.
Just wanted to show you this way.
So you had a solution in case you didn't figure it out.
And also to show you a different way of doing it if you did it one of those other ways.
So let's head back to the presentation.