Hello, welcome back.
I hope you enjoyed the challenge and I hope it was successful.
In this video, I'd like to go over that challenge and
give you one solution to it.
So I'm in the section 9 workspace in the challenge solution project.
In your case, you're going to have this solution already, but we'll
work through it together here.
So let's say where are we.
Well, we need some sort of data structure.
We need a list, we need a collection that the user can type integers
and we can store it into, right.
So in this case, we're going to use a vector.
It's the only one we know right now, and it's a really
easy and logical one to use.
So in order to use a vector, we need to include vector.
So I'll do that right now.
And obviously, we need to declare a vector that's going to hold
the numbers and we also need to declare a character that's where
we're going to store whatever they typed on the keyboard.
So let's declare those 2 variables right now.
We could call this vector whatever we like.
I'll just call it vector.
Remember, it's a vector of integers.
And I'll call it numbers.
We could call it data. You could call it whatever you like.
And I'm initializing it to empty.
And then I've got a character, which I'll call selection.
And I'll also initialize that to empty.
What we're going to do is we're going to read the user's
choice into that variable.
Okay, so where are we now.
Remember, what we're going to do is we're going to
display the menu to the user.
We're going to ask them for a choice, depending on what they choose.
We'll do something or other.
And then at the end of this do loop, we want to keep looping
while they haven't pressed q, right either uppercase or lowercase.
So let's write that logic first.
Let's start with baby steps.
Here's the do loop, and then we want to do while the
selection is not equal to.
Remember, in this case, they can type in an upper case
or a lowercase q, right.
So while it's not equal to a lowercase q.
And again, those are characters, so make sure they're single quotes
not double quotes and the selection is not equal to an uppercase q.
There's a semicolon here at the end of this while loop right
there, this do while loop.
That should take care of our logic.
Now what we want to do in here is we want to display
that menu right, right away.
That's the first thing we want to do unconditionally.
So what I'm going to do is I'm going to copy and paste my menu
code right here which is just a bunch of output statements.
And we're reading the selection.
So we can test this right now.
We're going to display print a number, add a number, all the menu
choices that you saw in the demo.
And then we're going to ask them to enter a choice, and we're going to
read that choice into selection, which is that character variable right here.
So what do I expect here.
Well, I expect that if the user types lower or uppercase
q, the program will end.
If they type anything else, we should display the menu again
and again and again, asking for enter another choice, right.
So let's try that.
We'll build and run.
And there's the menu, print numbers, add a number,
display number and so forth.
So let's say we enter p, we get the menu again.
Uppercase p same thing.
A 1, a 3, a 5, it doesn't matter, anything.
Now what we're really interested in is we want the program to quit when
I press both qs, upper and lower.
So I'm going to press lowercase q, program stops.
Perfect.
I'm going to test this again with an uppercase q, program stops again.
Perfect.
Make sure you test both of them because a lot of times.
I've seen typos like this.
And the little test with a lowercase q, and it quits correctly but
then the uppercase q doesn't work.
So make sure that you test everything really well.
Now this is where we're at right here.
We know the user has selected something, right.
So what do we need here.
Well, we need some selection logic.
We need to say well if you picked p, I want to do some stuff.
If you picked a, I want to do something else.
We have a choice.
We could use a switch statement really easily because these are
going to be character literals.
This is a character so.
I could do a switch off of selection and they say case p, p
cast case a, a case m and m and by that i mean uppercase lowercase.
And that'll be fine.
What I'm going to do is I'm going to use an if else if ladder
because it's usually what you'll see out there in this kind of
example over the case statement.
Okay, so let's try that.
First thing, let's deal with the print.
So we've got the selection here.
So here I want to say if the selection is equal to, let's say,
an uppercase p or the selection is equal to a lowercase p.
And I'm going to do a block statement here because I'm going
to write some extra code here.
So that's where we're at.
If the user typed the p whether it's upper or lower case we're going
to execute this code right here.
What does this code say, print all the numbers in the list.
What's my list numbers right here.
What's the best way to do that range based for loop, no question.
Really, really easy.
There's one thing we need to take care of though.
If that list is empty, we need to tell the user list is empty.
We can do that now or we can do it in an else statement.
I'm going to do it right now so I'm going to say
remember indent right here.
I'm going to say if numbers is the vector.size is the
method that returns the size.
If that returns a 0, what's going on, the list is empty, right.
So I'm just going to say cout.
And again, indent under the if cout, the 2 square
brackets with nothing in it.
And then I'll just say list is empty.
That's it, endline and that's the if part.
Now what about the else part.
The else part's going to have a few more statements than this.
So I'm just going to say else and then again a block statement here.
I could have said if numbers.size is not equal to 0, and then put
this code up here and then the l sort of in the empty part, both
ways are absolutely perfectly logical, and they're both correct
But in this case right now I'm here.
So when am I in here I'm in here when the the size is not equal to 0, right.
In other words, there is stuff in that vector.
So what I want to do now is print it out.
I want to print it out on a nice way with those brackets.
So I'm just going to say cout and I'm going to print out that left
bracket followed by a space, no end line because I want to print
the numbers right next to it.
And then I want to close it off with an end bracket.
Now what I want to do here in the middle is print out those
numbers, one after the other with a space between them.
How do I do that, what's the best way to do that a range based for loop.
So let's do that for auto, I'll call the variable num,
and the collection is numbers.
And what do I do for each one of those, display it.
Cout num followed by a space.
That should do it.
Okay, now right here, I definitely want to do an endline that
will take me to the next line.
Now let me clean this up so we can see the structure a little better.
And I'll get rid of this space as well.
So look what's going on here.
We entered a selection.
The selection is p so the user wants to print out all those elements.
We know that there is either nothing in the list in which case we do this.
Otherwise, we print out all the numbers in the list.
Right now the list is empty the first time I do this because I haven't
been able to add anything right.
So we'll run it, and, when I select p, it should say list is empty.
There it is.
The list is empty then I get the menu again, that's important.
I'm going to quit.
Now let's test this with a list that's not empty what we'll do is we'll
just come right up here and just put some stuff in there, 1 2 3 4, right.
The list is definitely not empty now.
So we'll run this and we expect to get 1 2 3 4 right
in the brackets, perfect.
So looks like our print is done.
I'll quit, and we'll move on to the next one.
Now we're here.
It wasn't p.
Let's assume that it wasn't p, right.
So what we'll do is we'll create the else if ladder here.
So I'll say else if the selection is an a uppercase or the
selection is a lowercase a, then we want to worry about adding
something to the list, right.
So I've got my block statement here.
So what do I do here.
Well, I need to ask the user to enter a number and then add it to my vector.
So I need a variable, int.
Let's call it num to add.
That's what we're going to read the number into.
We'll initialize it to 0, and we'll say we'll output to the user enter
an integer to add to the list.
Something like that.
And what we'll do is we'll read it into num to add.
So now we've got the number that we want to add.
What do we want to add it to, the vector numbers.
How do you do that, numbers.pushback, right.
We're going to push it to the back of the vector,
and we're going to push num to add, just like that.
And then we're going to tell the user we've added their number.
So I'll just say something let's see out num to add.
Let's say just simple added.
And we'll put an endline here.
That looks pretty good.
We can test that out as well.
What I'll do is I'll come up here and I'll initialize
that vector again to empty.
We'll get rid of that test right there.
So there we go.
The vector is empty again.
We'll run this, and let's add we'll press a.
Enter an integer to add to the list, perfect.
We'll type in an integer 10.
We're reading that into num to add now and pushing it
to the back of the vector.
It says here 10 added.
Now if we print the numbers again with the p option, we should
see a list with a 10 in it.
Awesome.
That's exactly what we have.
Let's add a 20.
And we'll print it again.
Now we've got a 10 and a 20.
Exactly like what we expected.
Okay, we'll press q to quit.
So we're working our way down these options here.
The next one would be m to display the mean.
So let's do that.
Again, we'll say else.
Remember, that's that else if else ladder else if the selection is
equal to a capital m character or the selection is equal to
the lowercase m character.
In this case, I want a block statement and in here is where
we're going to calculate the mean.
Okay, just like we did before in some of the other examples that we've done.
How do you calculate a mean.
Well, we need a running total, right.
We add them all up and we divide by the size of how
many elements are there.
But we need to be careful of one thing.
We have to be sure that the vector is not empty, right.
So we'll do this test again.
So we'll say if numbers.size is equal to 0, we'll simply say cout, and we
could do this in a block statement but I'll just use a single statement.
We'll say unable to calculate mean, no data or something like that.
And we'll finish it off with an endline.
Now we've got our else statement in a block.
We need to do more than one thing here.
So now the only time we're ever going to be in here
is if there is data, right.
There's stuff in the vector.
There are integers in there.
So what do we do, we need to calculate, add them all up.
So let's create a variable called total, and it's an integer is fine.
And now what do we do, so we loop through that vector again,
and we total up all the numbers.
So let's use another range base for loop, that's
perfect for this situation.
Let's say num and the collection again is numbers.
And what do we do at each iteration, we just say total equals total
plus that number, perfect.
Now we're out of that loop.
Remember, don't indent here.
We're going here because we don't have a block statement there.
And now we just simply say cout the mean, let me
clean that up the mean is.
And what we can do here is we can actually do the
calculation right in line.
We could just say total divided by numbers.size which is the amount
of data in that vector, endline.
Okay, now that's almost correct but it's not quite correct.
There's a little error here.
And if you remember what it is, we've got an integer total being
divided by an integer size.
So we're going to do integer division here.
So this is where a good test case comes in handy.
Let's run this, and let's type in, let's add a 1 and we'll add a 2.
Let's print our list, 1 and 2.
Let's take the mean, we expect 1.5, we get a 1.
That's because we're doing integer division instead of double division.
So let me quit here.
We need to provide a static cast here of one of these 2 guys.
So we'll just do this so we'll stay static_cast to a double and of total.
That's it.
We'll run this again.
Let's add a number one.
Let's add another number 2.
Print them out, make sure we have the one and the 2 in there, yes we do.
Let's take the mean.
Now we expect 1.5.
Now we've got 1.5.
Okay, so the 2 pieces left are smallest and largest and this
video is getting a little long.
So I'm going to stop this here.
And I'll join you in the next video where we'll do the last few pieces.