Okay, so welcome to the section 9 challenge.
This challenge puts together just about everything we've
learned to solve a problem.
I mean the section 9 workspace under the challenge project.
There's also a challenge solution project, which includes my
solution which I'll cover in the video right after this one.
Let's talk about the challenge and then I'll give you a
demo of the running solution.
This challenge is about using a collection or a list of integers
and allowing the user to select a few options from the menu.
So here's the menu, p-a-m-s-l-q are the selections.
If the user presses p, you print the elements of the list.
If they press a, you add a number to the list, which you ask them for.
If they press m, you display the mean of the numbers in the list.
S and l, the smallest number and the largest number.
And of course, if they press q, you quit.
Now anything else, you should tell them invalid choice and so forth.
Okay, just like we've been doing.
So let's go over a little bit more of the detail here.
We're only accepting valid choices from the user, both
uppercase and lowercase selections should be allowed.
And if they enter an illegal choice, you should say unknown
selection, please try again, and then redisplay that menu.
Okay, If the user enters a upper or lowercase p, we display
all the elements of the list.
Remember, this is a list of integers.
If the list is empty, you should just display two square brackets
and then say the list is empty.
If the list is not empty, then all the list elements, all the integers in
that list should be displayed inside square brackets separated by spaces.
So in this case, the list has the integers 1 2 3 4 and 5., and
this is how we'll display them.
If the user enters an uppercase or lowercase a, then you'll prompt
them for a number to add to the list in which case you add it.
And then you display whatever number they entered added.
Okay.
It's okay to have duplicate entries in the list.
And the last couple are pretty straightforward.
If they press m upper or lower case, you basically iterate
through that list and calculate the average of the list or the
mean of the list and display that.
If the list is empty, obviously you can't calculate the means,
you simply display unable to calculate the mean no data.
For s, again upper or lower case, you basically iterate
through the list and find the smallest integer in that list.
And that's what you display back to the user.
So in this case, if the list contains 2 4 5 1, you'll display
the smallest number as one.
If the list is empty, you should display unable to determine
the smallest number because obviously the list is empty.
L works the same way except you're looking for the
largest element in the list.
When they press q, you say goodbye, and the program terminates.
Okay, so that's the challenge.
Before you begin, write out the steps you need to take and decide you know
what order you should do them in.
Think about the loops.
You know.
You need loops you know your if statements or switch statements.
Think about what the best way to do this is.
Start small, build little pieces, test those little pieces and then
add as you go and test as you go.
One big hint, use a vector.
I mean don't use an array here.
Use a vector, I should be pretty obvious because we want this to grow dynamically.
A couple of more things.
You could add functionality once you finish this basic challenge
to search for a number in the list.
And if it's found, how many times did it occur, maybe it's not in the list.
The vector class has a .clear method that you can
call to clear out the list.
So maybe you want to you know have an option for the user to
delete everything in the list.
Maybe you want to not allow duplicate entries.
So if I press to enter the number 5, you'll let me enter it.
If I want to enter it again, you'll say sorry it's already in the list.
So come up with your own ideas.
It's really all about learning and practice.
Okay, so let me give you a sample run of this.
So here's the menu.
As you can see p is print, a is add, m is mean, s is smallest,
l is largest and q is quit.
Let's do the obvious one.
First, let's press q, we're out.
It says goodbye.
Our program is over.
Okay, so let me run it again, and let's print the numbers in the list.
Right now the list is empty.
So if i press p.
And again, it could be uppercase or lowercase, you get the two square
brackets and the list is empty.
Now I want to add a number.
I'll press a.
Let's say I want to add the number 10 to the list, 10 is added.
And let's say I want to add another number, so I'll press a again and I
want to add negative 10 this time.
So we've added negative 10.
If I want to print the numbers again, I'll press p.
And there's my list 10 and negative 10.
I'll add a couple of more, so let's add 100 and let's add 500.
And we'll display the list again with a p.
And now we've got 10, negative 10 100 and 500, just like what we thought.
Display the mean of the numbers, we'll press m.
In this case, the mean is 150.
Let's add another number.
Let's add three and do the mean again.
You know notice it's 120.6, so the mean should be a
double, not an integer.
Finally, we'll do let me print the list again.
We've got 10 negative 10, 100 503, the smallest should be negative
10 and the largest should be 500.
So let's test that out.
I'll press s for the smallest.
The smallest number is -10.
And I'll press l for the largest, the largest number is
500, exactly what we expect.
And of course if the user types in 1, unknown selection please try again or
if they press o if they press anything that's not one of these letters, we
tell them unknown selection redisplay the menu and grab the input from them.
Again, press q to quit.
That's it.
That's the challenge.
As I said, this can be a tricky little challenge to do because
you've got a lot of things going on.
You've got loops and loops within loops and if statements
and maybe switch statements and maybe do loops maybe while loops.
It's really up to you to decide how to solve it.
Give it a shot.
Don't be discouraged if it takes you more than a few attempts to get it.
That's pretty normal.
I'll see you in the next video.
We'll go over my solution to it or at least one solution to it.
All right, good luck.