Welcome back to the continuation of the solution.
We stopped at mean right here.
And the last 2 parts that we've got to do are display the smallest number
and display the largest number.
And then of course we have to handle quit which is really easy to do.
All right, so let me scroll back down here and clean up a
little bit of this white space.
All right, so this is where I right here.
So now the user hasn't typed in a p or an a or an m, maybe they've
typed in an s, which is the smallest.
So we'll do another else if ladder here.
We'll say else if the selection is equal to a capital s or
selection is a lowercase s.
In this case, we need to calculate the smallest number in the List.
There's a lot of ways to do this.
But again let's deal with the case where the list is empty, right.
So we'll say if numbers just like we have before.
If numbers.size is 0, then we've got an empty list.
We could just say cout and we could say unable to determine the smallest,
list is empty or something like that, any kind of message that makes sense.
And we'll put an endline here.
Now we're in the l section.
At this point, obviously, there is some data in that vector so
what we can do is, we can again iterate through the entire vector
and look for the smallest number.
Well, how do we do that.
It's pretty simple just pick the first number and assume it's the
smallest and then iterate through the vector and if you see anything
smaller just replace that 1 with the smallest 1 yeah so it's again
it's pretty straightforward.
So let's do that.
Let's say we've got a smallest, we'll call it smallest.
And what we'll do is we'll initialize that guy to numbers at 0.
All right, so let's think about what's going on here.
What we're doing here is we're saying that the smallest integer
is the first integer in the vector right now, reasonable assumption.
We know that there's at least 1 integer in there.
So we'll do that.
Then what we'll say is we'll do again arrange base for loop.
We'll say auto, we'll call it num and the collection is numbers.
So we'll loop through there and what we'll say is if the number
we just read is less than the smallest that we've already seen
then what do we do then we make the smallest the number we just read.
That's it.
Really straightforward.
Now we don't do an output statement but look where we're going to do it
we're not going to do it here, we're going to do it here because at this
point, we're out of that for loop.
So we're going to say cout the smallest number is a
colon and its smallest, and we'll provide an endline.
That's it.
Let's test this out, and we could put data in here up top,
it's right up here just put a bunch of data and we know what the smallest is.
But I really want to test it all as we go.
So I'm going to run it now.
And let's add 10, and let's add 20.
And we'll select s.
We expect 10.
That's exactly what we got back.
Let's add another number.
Let's add negative 20.
Let's print the numbers, and we've got 10, 20 negative 20.
If we want the smallest at this point I'll press s again,
we should get negative 20.
And there it is, negative 20.
So a whole bunch of other test cases you could do,
but this works just fine.
I'll press q to quit.
And next is the largest, right.
The logic is exactly the same as this except it's going the other way.
So in this case let me clean up that white space.
Right here, we're going to say else if the selection is equal
to a capital l or the selection is equal to a lowercase l.
Now the user wants to find the largest number in there.
I'm going to use another block statement.
And the same logic I'm going to say in largest and I'm going to
assume that largest is the number at the zeroth location, right.
The first number in the vector.
Oops, let me back up here.
I forgot to check to make sure that there's numbers in there, right.
So if numbers.size, I got a little bit ahead of myself there.
If numbers.size is equal to zero, then in this case, there
is nothing to check, right.
So we'll just do the same thing we did out here.
We'll say cout unable to determine largest, list is empty.
Here's the else part.
That's the part where I got ahead of myself.
Let's assume that the largest is that first number.
I know there is a first number, right because the size was not 0.
So at this point, we're going to loop through the collection again.
So I'm going to say auto num, just like we did before.
And we're going to check if the number we just read is greater
than the largest number we've seen, then what we're going to do is
we're going to say that the largest number is now known, simple as that.
And our output statement now says the largest number
is, and we'll say largest.
Okay, so that should take care of the largest case.
So let's test this out.
So let's add some numbers.
We'll add negative 10.
We'll add a 1000.
We'll add 200, and we'll add 300.
So let's display our list.
So you've got negative 10, 1000 200 , 300.
We expect the largest to be a 1000, so I'll press l, and
the largest number is a 1000.
Let's check the smallest again.
S, the smallest is negative 10.
Cool.
So we're almost done, right.
All we need to handle is the q case.
I'll quit here.
And the q case is real simple because all we want to do is just say goodbye.
So let's do that here.
We'll say else if the selection in this case, right, just like before is
an uppercase q or if the selection is equal to a lowercase q, in this case
what we want is to simply say cout.
And let's do that in a block as well.
You really want to do these in blocks because you never know
when you're going to come back to add code, and it's already
set up to add code this way.
So we'll say see out goodbye, endline.
Perfect.
And while we're here we may as well handle that other case right.
That's the catch catch-all else, right here.
That's going to be the end of the else if else ladder.
What happens here.
Well, they typed something in that I have no idea what to do with.
So at this point what do we say,
cout unknown selection.
Please try again.
That's as simple as that.
Okay, seems like a lot of code and it sort of is.
When we talk about functions very soon, we're going to make this
so much easier because instead of all of this code in here,
we're just going to have one line that calls a function, and one line that
calls a function here and so forth.
So we'll get to there soon. But let's finish this up now.
Building and running.
Let's press q, we expect a goodbye message now.
There it is, goodbye. And we quit.
And let's type in something that we don't know what to do with an o,
unknown selection, please try again.
A 1, unknown selection, please try again.
And that's it.
That's the solution.
Of course, you could have done this with a switch statement here in which
case it might have looked something like switch off of selection.
And then here you would have case no Q case lowercase q you know
cout goodbye, and then break here.
And then you'd have all your other cases, just
like you saw in the videos.
But in this case, I think the switch statement is is okay
if we're using functions.
We could just call functions here.
But otherwise, the switch statement 10ds to get even longer.
And I don't know how many lines of code we wrote for this example.
Let's see, we started on about line 65 because the rest was just
the you know the stuff at the top.
So we went from about 65 down to about 140.
So I don't know about 70 lines of code or so.
It would be obviously much less when we do functions.
And with the switch statement, I think it would be even longer the
amount of code we'd written just because of the syntax of the switch.
But the if else if ladder works great here.
Obviously, if i decide to add another option here, it's just
a matter of coming in here.
And you could do it anywhere really because it's all sort
of mutually exclusive, right.
And you can just do it right here.
Else if at it between this one and this one and there you've got your
other option just as easy as that.
Okay, so I hope you enjoyed this challenge.
This was a challenging challenge because it's using everything
that we've learned so far.
But there's a lot of logic going on here, right.
It's a simple program but there's a lot of logic going on here.
You've got you know a do while loop.
You've got for loops looping through the iteration, and thank goodness that
c++11 gave us this range-based for loop because that makes it so simple.
We don't have to do things like for int I equals 0, i less than
you know the size of the vector and then increment I along the way.
We don't have to deal with any of that, so it makes it
really really straightforward.
Okay, so I hope you enjoyed this section.