Hello, everyone. Welcome back.
This is the solution to the section 11 challenge.
And I'm in the section 11 workspace and the challenge solution project.
I'd like to go over one possible solution to this project.
This is a solution that I've done, very modular,
all the functions are there.
they're all small,
they do one thing, and they do them well.
So let's go through this.
Let's look at our main first.
Let's start there.
Main defines numbers, which is the vector of integers,
just like we've had before.
And it's got a character variable called selection, that's where
the user is going to type in their selection, their menu selection.
Now we've got our do loop.
Our do loop, now there's -- I've folded the code here.
There's obviously more code here in the switch statement, but
I'm just going to show you the overall structure of this loop.
So here we're doing do display the menu.
Notice that now we're calling a function to display the menu.
Then I'm going to get the selection from the user.
So whatever they type in, I'm going to store in selection.
But I'm not just going to store it in selection, I want to make it uppercase
and then store it in selection.
That way I don't have to check for lowercase and uppercase every
time I check for a selection.
And I want to do this loop while the selection is not
equal to an uppercase q.
Okay, so that's pretty much it.
Now let's take a look at our display menu function.
And it's right down here.
And I'll expand it right now.
And you can see that that's the function display menu.
And all it is is the output statements, just like we
had before inside the main.
So in this case whenever I need to display the menu, I
can call this function and I'm doing that exactly here.
Okay, now let's take a look at the get selection function.
Here's the get selection function, and you can see the name of
the function is get selection.
It expects nothing but it does return a character.
So in this case, I'm defining another character variable in here.
Remember, this variable here is local to this function.
I'm simply reading from the keyboard their selection.
And I'm converting it to uppercase and returning it.
Okay, so that's that one.
So at this point, I've displayed the menu.
And remember, the menu has at the very bottom of the menu, enter your choice.
So I don't really need to prompt the user for anything, it's already there.
So I'm displaying the menu.
I'm getting their selection, and selection will now contain
an uppercase character.
Okay, perfect.
So now we're going to switch off of that character.
And let me expand the switch statement here.
So in this case, if the selection is a p, I'm going to call a
function called handle display.
Because remember, p is to print out all the integers in the vector.
And I'm going to pass in the vector to that function.
If it's an a, I'm going to do a handle add.
M is handle mean, s is handle smallest, l is handled
largest, q is handle quit.
In default, I'm just going to handle an unknown.
So you can see now that the logic in main is exactly like it was before
except we're calling functions.
We don't have all that code written in here.
Also this allows us to think modularly, right.
Now I've got functions doing those defined tasks.
So again, it's that boss worker analogy at work
here, it's perfect for that.
Let's take a look at handle display.
So in this case, so the user selects p.
Notice I don't have to check uppercase and lowercase anymore
because the get selection function already made everything uppercase.
So I'm going to call handle display, and I'm going to
pass in the vector to it.
Because obviously, it needs to display the vector.
Let's come over here to handle display, and you can
see the function right here.
Now notice handle display, it expects a vector, and I'm passing that
vector in by reference and const, just as we've spoken about before.
This handle -- this display method really has no business
changing that vector.
So I'm not going to even take the chance that it will, I'm passing it.
By reference so, I don't have to copy it and const.
So let's take a look at that.
All right, so we've got our vector here.
So I'm checking to see if the size of the vector is 0.
In which case, it's empty, and I'm displaying the list is empty.
Otherwise, I'm calling a function called display the list.
And I'm passing in that vector, the same vector I got,
I'm passing to the function.
I could have looped in here and displayed the list
right in here of course.
But I decided to create another function because you know we
could tweak the behavior of what the display looks like.
And I don't want all that code in here unnecessarily.
So let's take a look at display list.
It's toward the bottom.
I'm going to scroll down here.
There's the display list function right here.
Again, it expects the same vector that was sent to the function prior.
And now if you look at the code, it's going to print out a left bracket.
Then it's going to loop through that vector and print out
each number, just like before.
But you can see that this list displays just the list.
That's all it does.
And whenever I need to display a list of integers, I could just
send it right into this function.
Okay, so that's display list.
I'll scroll back up.
And let's talk about handle add.
So now we're here handle add, it's another function.
Whenever the user presses a, they want to add a number to the list, right.
So I want to call handle add and I want to pass the list, the vector.
So let's see the handle add method.
I'll scroll down slightly, and we'll see handle add is right here.
Now notice that in this case, I'm still passing the vector
by reference because I want to add an integer to it, right.
So I need to have the reference to it but not const
because I need to change it.
I need to ask the user for an integer and store it in the vector.
So let's look for that code.
We're doing everything right in here.
We're saying there's an integer called num to add.
Remember your scope rules here that integer is local to this function.
I'm asking the user to enter an integer to add to the list.
I'm getting that integer from them from the keyboard.
And then I'm simply calling the pushback method of the vector
class with the number to add and displaying the message.
That's it.
Really, really easy.
Okay, so let me go back up to the main.
And let's see handle mean, how does that work?
Well, we'll go to the handle mean function, which is right here.
Again, it expects a reference to a vector and it's constant.
I don't want my handle mean modifying the vector.
So what do we do?
We check to see if the vector is empty.
If it is, we just say unable to calculate the mean the list is empty.
Otherwise, we call calculate mean, and we pass in the vector.
It's going to return a double to me which I'm going
to display right in here.
So it's going to say the mean is whatever number I get back.
All right, so let's take a look at calculate me.
And I'll scroll down here.
This is calculate mean, notice again, it's the same vector, same
syntax here by reference constant.
There's my total.
I'm looping through those numbers, adding them all up to total.
And then I'm casting one of these to a double and dividing by the size.
Exactly like we had before.
It's pretty much the same code that we had in the prior challenge, the
section nine challenge except we're breaking it up into modules here.
So that's it.
That's calculate mean.
And I think you can see where we're going with the rest of this.
Here is let me close up handle mean, we've got handle smallest, handle
largest work exactly the same way.
And then handle quit and handle unknown, we're really
doing very little in there.
But I decided to make them functions because you never know
what you need to do in there.
I mean suppose you're working with files and the user quits
and you want to you know close up files and so forth.
I'd like to do that in a function and not here in the main.
I want to keep my main really, really simple, easy
to read and straightforward.
So my handle quit kind of looks like this.
Right here, you can see handle quit, it's all it's saying is goodbye.
And my handle unknown is saying unknown selection, I guess
I should spell that right, unknown selection try again.
Okay, so that's pretty much the solution.
I've got -- let me scroll up to the top, and I've got all the
function prototypes at the top. So there are my function prototypes
here the prototypes for displaying the menu and getting the user
selection, display the menu expects nothing, returns nothing.
Git selection expects nothing, returns a character.
Then I've got my menu handling function prototypes.
Notice that all of them expect a reference to the vector that's
constant except handle add because handle add needs to change the
vector, so it can't be constant.
Then I've got handle quit, handle unknown.
And then I've got the functions that actually do the work right the
display list, displays the list.
Calculate mean, give me the vector, I'll give you back the double that's the mean.
Get smallest, same idea.
Get larger, same idea except we're returning an integer.
Okay, so what do we want to do?
Let's let's modify this program a little bit.
Let's add some functionality to it.
Suppose the user wants to see if a specific integer
is in the list or not.
Okay, so how do we do that?
Well, there's a couple of things we need to do.
Obviously, we need to do some function prototyping here.
So let's do that.
Let's put it right under here, under handle largest.
We'll say void, how about just handle find?
We're going to find an integer in that vector, right.
It's not going to change the vector, but I still need the vector.
So I'm going to say const a vector of integers, and I want it by
reference and we'll use the v.
Again, we don't have to use the parameter names in here, but
I'm going to just use the v.
So that's my handle find.
Okay, now here we need to know -- so here we need a
function that's actually going to do the finding, right.
And it's going to return a Boolean, true or false.
I really don't want to know how many times it's there.
You could certainly write a function like that.
But let's just call it find.
And what does it expect?
Well, it expects that vector to look through, right.
And again, it's a vector of integers, and it's by reference.
And what else does it expect?
Well, it expects the integer that you're looking for.
So in this case, let's just call it target.
That's the integer I'm looking for.
So those are my prototypes.
But before we even get to those prototypes, let's fix up the menu so
that it displays that - that f, right.
So I'm going to go down to display menu.
And right after display largest I'm just going to say cout.
And we'll put an f in there, and we'll say find a number.
All right, so that should display now.
Now what do we do?
Well, we need to handle that case in main, right.
So we'll do it right after the largest right here.
So we'll say case f in this case and we don't have to worry about
the lower case because it's going to be converted to an upper case.
And we'll say handle find.
And we've got numbers.
All right, perfect.
So now what we need to do is we need to write these two
functions that we prototype.
We need to write handle find and we need to write the
find function, these two.
So let's do handle find first.
And we'll put it right at the bottom of the handle functions, right
here right after handle largest.
And we could write a comment in here, but I'm not going to
right now just to save time.
So there's my handle find.
Okay, now what does the handle find function have to do?
Well, it has to ask the user to enter the integer they want to find, right.
So let's just call it target.
And we'll initialize it to 0.
And we'll just say something like cout, enter the number
to find or to search for whatever you want to call it.
And we'll read that into target.
Okay, now what do we do?
Well, we've got that function we still need to write the find
function that returns a Boolean.
So we're going to use that right
now without even writing it because we know what it looks like. We
wrote the prototype for it, right.
So I'm going to say if find, and I'm going to pass into
it the vector and the target.
Let me get rid of some of those parens, that's it.
So if find that target and this vector returns true,
then the integer's in there.
So we could just write something like target was
found, else target wasn't found.
And I'll put some new lines at the end, some endlines.
That's pretty much handles that function.
So what do we have left to implement?
Well, we need to implement -- let me scroll up a little bit here.
We need to implement this function, right here, the one
that returns the true false value.
And I'm going to put that all the way at the bottom with all
my other good functions that doing that are doing work.
Do it right after it gets smallest right here.
Let's write it right here.
And there's the prototype, right.
So what am I going to do here?
Well, I'm going to loop through the vector and compare
each element to the target.
If it's there, I'm out.
If it's not, I keep looping.
And if I get to the end, I never found it, right.
So we can do something like for, again, auto num for each element
through the loop will be called num.
And there's my collection, the vector.
And we'll just say if num is equal to my target,
then I'm done. I found it.
I'm going to return true.
Now if we get all the way to the end of the loop, we don't want to put
this here, you want to put this here.
We'll just simply say return false, right.
We never found it.
So let's run this.
Okay, we've got an error that says number was not declared in this
scope because it should be numbers.
All right, hopefully, you see that error right there, which is the typo.
And let's try it again.
There's our run.
Here you see the f in the menu
now so let's find a number.
Let's find a 100.
Notice how it asks me for the number to find.
Now the list is empty, right.
So I should not find it.
100 was not found, goodbye.
What happened.
That's an error, right.
I should have never printed goodbye.
Let's quit and see what's going on.
Need a break statement, that's why we need to test our code,
forgot the break statement.
Remember, with the switch it goes to the next one, so handle quit fired.
Let's try this one more time.
Okay, so let's find 100.
100 was not found.
We get the menu again.
Let's add 100.
And let's look for it again. So I'm pressing f for find.
Let's find 100.
Now it says 100 was found.
And let's print the numbers.
There you go, 100.
So we've modularized our system into a series of functions.
We easily added a new behavior to this which is the find.
And you can see now the code is longer, for sure, because you will
also have a lot of comments in here.
But you've got these really well-defined logical functions
that are much easier to debug now.
If there's something wrong, you can come in here and tweak it.
You can also extend the behavior of the add rather
than having just one big main.
And if you make any changes, you're going to put all your
logic in there and that main gets out of control very quickly.
Okay, so hopefully, your solution it's probably going to be different
from mine, and that's perfectly fine.
Hopefully, it worked out okay.
And if you have any comments on it, please leave them in the course forum.