OK, so now we're going to take a break from just doing standard lecture and we are going to do some
practice problems, and this is really important.
Honestly, this is probably where the bulk of actual learning really takes place in programming.
So it's great that I'm able to tell you some things about C++ and programming in general, but I feel
like it doesn't really get absorbed until you're actually solving problems on your own.
So what I'm going to do, of course, this course will have some problems that I can just assign as
coding exercises through you to me or I can also, you know, I also have some projects set aside where
I have like a document that has a big project description and you will implement a project if you want
to, of course, which you should.
But that'll come later in the course.
Right now, we're just going to kind of try and use what we've learned so far so we can get better at
it.
So let's go ahead and do that, I have two problems today.
What I'm going to do is have a slide that will have the problem, statement a description of the problem
and show you some sample output, some input and output.
And then you're going to pause the video if you like and try and solve the problem on your own.
And then afterwards, I will walk through it.
So let's go ahead and get started.
So first problem here.
This one involves user input and conditionals and output.
So these are three things that we've learned.
We just went over a user input and conditionals would be if, else, if and else and output is something
we've been doing for a long time now, a CEO, right?
So and the description is to create a program that reads in two characters and two integers.
The program should then check to see if either of the characters match either of the integers.
So this is possible because in C++, you can compare chars and and hence that ASCII table.
You notice that there's a decimal value for each of those characters, so you're able to compare an
integer which represents kind of a decimal value and the decimal value of the characters, so you can
use that ASCII table for reference.
If this is confusing, it might be good to use it either way.
Right?
So you can actually check your output to make sure it's correct.
So you look at the decimal value of a certain character, and then you can enter some input and check
the ASCII table to see if the match is actually correct when your program is deciding if something matched.
So if anything matches, then what you should do is print out the characters, then match so and also
try and follow the input output format that I have mentioned here.
Here's some example stuff so you don't have to use this same input that I'm using.
You can test it with whatever input you like, but this is kind of what should print out right here
is what it means.
So you should say into two characters, followed by two ends and then a colon, and then you can enter
the input, whatever you like.
And then then what should happen is it should print something out like this.
For this example, be has a ASCII decimal value as ninety eight, so be it was matched with ninety eight.
So that's why it printed out B has a decimal value matching ninety eight.
So, you know, this one has no matches, and then this one m matches 77, so that's why I printed this
out here.
OK, so hopefully that makes sense to you.
If you are ready, you can go ahead and pause the video.
Try and pull up an editor and see if you can figure this out for yourself.
Otherwise, I'm going to go ahead and walk through it.
But I really, really think you should try and solve this for yourself, because that is where the actual
learning will happen.
You know, it's all about getting problems like this, and you know, this is a pretty straightforward
problem.
We'll be looking at much more complex problems in the future.
But that everything is going to be challenging, but the more challenging it is, honestly, the better
it is because it really pushes you to be a better programmer.
So try and do the best you can on this, even if you can't figure it out or if it has errors, do all
of the parts that you can.
And just kind of do the best you can is what I mean.
And then I will go ahead and go over it and we can fill in any of the gaps that you might have had.
OK, so go ahead and post the video if you haven't otherwise, I'm going to go ahead and pull up an
editor and try and solve this.
So we can go over it.
So I have the code here.
Open and I just have sort of a skeleton of a program.
So let's also toggle back and forth kind of between the presentation and oh, so this right here it
said that it needs to read in two characters and to integers.
So we're kind of going to break this down.
So we already got a skeleton of a main function there.
But let's go ahead and do this first.
This is something easy, right?
We know that we're going to need to read into two characters in two integers, so we're going to have
to make those variables right.
So let's go ahead and make that.
So I'm going to say Char C one and Charge C two and end one and two.
So now we have some variables, so let's see what it says next.
So the program, it's going to read into this, then it should check to see if either of the characters
match either of the integers.
OK, so let's break this down and just kind of do this next part.
This is something else that we know how to do before we do the matching part that's probably like the
hardest.
We know we got to read them in, right?
So let's go ahead and create some scene.
And also, we should probably make some see out stuff to print a message to the user telling them what
to do.
So I'm going to go ahead and do sit out and just say, let's see what we have here.
Enter two characters, followed by two it's OK to enter two characters, so followed by two ends.
And I think there's a little space here.
OK, so we print that out and then our next thing is to read into this.
So it's the character's first.
So I'm going to scene to see one and then we'll have see two.
And then after that comes in two ends, right?
So we will do our one.
And I too.
Cool.
So let's go ahead and go back to the problem description.
So.
We're now reading in this whole thing, so we're reading in the two characters and two integers, we
did that part.
So the program should check to see if either the characters match either the integers, so how can we
do this part?
We know we can compare them.
So if we're going to be checking to see whether.
Something is matching that kind of implies that we're going to need to use some conditionals, we need
to check whether something's true or not.
Right?
So we're going to go ahead and use some conditional statements for this.
So how should we set this up, set this up?
Well, let's start out with just an if the first thing, right?
And then we'll just kind of have to.
Cover all the different options, right, because what can happen is this character could be matching
this and or this character could be matching this and.
And then this character could be matching this and or this character could be matching this, and so
we kind of have like four options for matching and then we have another option in which none of them
match.
So those are actually five different conditions, right?
So I'm just going to do all of these different.
Conditions, I think you could do just multiple ifs, but I think I'm going to use an elusive because
it only says that.
So it's says if either of the characters match either of the integers.
So I think that maybe we should just use this because there could also be a a chance in which both match,
so maybe there's even some more, so this could match this one or this one and this could match this
one or this one, and then we could have both matching like maybe let's see.
Yeah.
So I think that, you know, we can have conditions that cover those four.
There's those four matching conditions.
But if we leave them is if we leave them as if just instead of exclusive, we're so elusive.
Then that will open up the possibility that we could print out that maybe multiple match, so like a
C one matches this, for example, and then see two matches this, then they would both print out.
If we did El Sift's, then it would only let one thing print out.
So I think I'm just going to do a bunch of ifs.
So for us, for that and for that condition.
So I will say if I see one.
So let's just start out with this basic one is one equals I one.
And you may be thinking, well, couldn't we just do something like C1 equals A1 or C two equals, you
know, equals A1 or, you know, dot dot, dot and just do all of them in one condition.
You could do that.
But think about what you need to print out, so you notice that you actually have to.
If we look ahead, kind of, you have to print out the things that match.
So if you were to put this all in one condition.
And it's like this or this or this or this.
What are you going to print out?
How are you going to know which you know which one matched?
Because if one of these was to be true, like, for example, let's say sea one equals I one is false,
but C two equals nine.
One is true.
How do you know?
You know, how do you know which one to use the variables for to print out this result that says, you
know, blank has a decimal value matching blank?
That's why we need to separate these into different issues.
So I'm going to do that, I'm going to say C one equals I one.
And then what we will do is just print out
that this c one right is has a decimal value matching i1.
So these are variables that we can print out, right?
Because in this example, be matched ninety eight.
We don't have to know like, um.
You know about the specific values that someone's entering, we can just use the variables because if
someone is equal to everyone, then we know that these are the two things we're going to want to print
out to the console as our result.
So we'll say see one and then has only seen one go back to.
This has a dismal value matching as a decimal in value matching and then a space.
And then I'll say I won and then and enduring.
Cool.
So then we're going to be doing like a very similar thing for the other three conditions, so I'm just
going to copy paste
that didn't paste very nice.
You see if I can copy paste that again.
OK, so it just has a.
Just need to back that up.
So I'm going to go ahead and paste some more just because I know I'm going to be doing this for three
other conditions.
But I'm going to be changing these up, so this next one, I'm going to put C one equals I two.
And then I can just change things like this.
I don't have that over to make it look better.
And then let's see a little through this next one.
This will be a see two equals i two.
So we can just change these down here, and this will be or actually, no, I make this, I want to
see two equals I one.
And then this is going to be key to equaling I to.
OK, so if it has a decimal value matching any one of these, it can print out, you know, both of
them.
So the next thing that we have to do, though, is figure out if it doesn't match any of these.
Then what are we going to do?
So if we just put in else right here.
It'll basically.
Just like, you know, if I just put an else right here, it'll only be attached to this Earth right
here, so it only run the health if this is not true, which is totally possible.
But what if one of these matches, you know, above it, we win.
We're not going to want to like, go down here and let's say this matched.
So says, Oh, you know, see two as decimal value matching I one.
But then we get down here and it's like, Oh, well, see, two is not equal to two.
And so if this is false and it just jumped in the Earth and will be like nothing, just matching, we
don't want that because something did match right in that example I was talking about.
So we kind of have multiple options.
If you wanted to kind of put a ton of code, you could just put it in, you know?
A bunch of like aliases after.
After these things, you know, or some other big condition, you know, if you put it loss after each
one of these, then it would just run after each one if it was false anyway.
So we don't want to do that.
So what I think we should do is instead have an else if.
And then we can say, you know, if none of these are matching.
Then we're going to print out that none of them match.
And so this is kind of long.
But, you know, we're just starting out, so it's fine.
So we're going to make a I'm going to make actually a big condition here where I do this, but I just
change it to not equal and never put in or.
And so I'm going to copy all of these and just change it to not equal.
So if I just keep passing these and I can go back and change it to not equal.
So I actually already have that one, so I'm going to copy this one, so it'll just be for four of these
with eight not equal, so.
And it's not yours, actually, I bet this is going to be and because if all of these are false.
And what I mean is that they're not equal.
I don't mean that this evaluates to false, I mean that the equal evaluates to false.
So if none of these are equal.
So that would mean that this not equal thing is true, right?
So if this is not equal and this is not equal and this is not equal and this is not equal, that means
that all the possible matches that you could have are.
False.
And so then you can say that nothing matches which what we have down here is there are no matching characters
and integers.
So that's what we'll go ahead and print out in this case.
So we'll just print that out if this is the case.
Right.
Cool.
So I think that that should probably cover it.
We can go ahead and test it now in this example.
I just called this.
IP one for input one.
But I might just call this like problem one or something.
For the output file.
So let's just double check that we have everything, so we have a chance here.
It looks like they get read in after we print.
So get in right here after we print out this little problem to the user and then what we do is we check
each one for all of these four conditions right now.
See one to I want you want to see two.
I want you to try to.
Cool.
So let's go ahead and run this and.
We'll see see if it works.
So I'm going to go ahead and save this and then I'm going to compile it and we'll stay in touch, practice
one copy and I'll say, Oh, and I'm actually going to call this problem one that you see.
So it looks like that compiles, OK, so I'm going to go ahead and run it, so I will see what I can
see, I will say problem one easy.
All right.
OK, yeah, we put it in line there, so immediately, I'm going to want to get rid of that.
I don't want this in line because that doesn't match the output here.
We entered on the same line.
So I'm going to go ahead and remove this and save it and we compile it.
And run that again.
So let's go ahead and check some of the output that we see here, so we have a B 98 to 80 98.
So this says B has a decimal value matching ninety eight.
So pretty cool.
That seems to match the output.
Let's go ahead and try this next one seed.
Thirty twenty two,
fifty three point two So that's no match in characters and integers, so that looks pretty good.
Then we have this capital M Capital L seventy seven point ninety nine Capital and capital.
Thirty seven ninety nine.
Oops.
I didn't write it again.
Huh?
OK, so that seems to mesh him, has a decimal value match in 77.
Now let's try both of them because that's not represented here, but we should have another test case
to see that both of them match.
So that would be, let's see, enmeshed.
77.
So we should use like maybe M and B.
Well, we can even flip those two.
I'll do B and then capital M and then I'll do a.
Seventy seven and ninety eight.
So be in seventy seven, ninety eight.
So get both of those printing out now we have B has a decimal value matching ninety eight and M has
a decimal value matching seventy seven.
So it would be good to check more test cases on this.
So, you know, not just up here to be as thorough as possible, you should check a lot.
It seems like it's working, so it should be working if you're able to pass these tests, but you should
bring up an ASCII table online.
So just search AC iSpace table and go ahead and try some extra input where you look at the decimal values
for a character and then you can go ahead and test them out here.
When you enter the user input.
OK, so hopefully you're able to figure it out if you didn't do it the same way as me, then that's
not a problem at all.
You might have found a more efficient, better way to do this, and that's great.
Honestly, probably a decent amount, and you probably found a better way to do this without having
so much code, which you know that that's great if you did so.
So let's go ahead and move on to the next problem.
So this one is reading into an array.
So this involves user input strings, arrays, conditionals and output.
So this is something you may not have done before, but we need to make a program that reads in three
characters to an array.
Pretty interesting.
So this program should have a hard coded string password variable that you create.
So the length of that password should be three.
So you're basically going to make up your own password, but it has to be three characters.
So once you've read all three characters from the user input into each position of the array.
You'll compare each one of the characters in the array to the string.
So this is kind of asking the question, does each character in the array at a given position match
the character in the string at the same position?
So this is going to involve indexing both arrays and strings.
So if there is a match, then.
So this they then I think if there's a match, then print that out to the console that there was a match
as well as the password.
So you see some examples here.
You know, here's this like wrong, guess wrong, guess?
But then when it was guessed correctly, it says you guessed the password.
Password equals yes.
In this example, so says the message that first displays is enter the three letter password.
Then you type a guess as the user.
And then you can say, you guessed the password, if it's correct, otherwise you say wrong guess,
and you'll say you guessed the password and then a period and then you'll say password equals and then
whatever the user entered.
OK, so go ahead and pause the video if you like to try this on your own, which you really should.
I'm sure you can figure this out.
I know that we haven't gone over reading into an array, but I think you can figure it out.
I'm just kind of challenging you here.
You do know how to index an array.
So if you're trying to read into a specific position in an array, just think about how you use that
index and how you you see in.
All right.
So I'm going to go ahead and try and solve this here.
So I'm going to actually make a new file here.
So I do find new files, language, C++ and.
Let's see, I'll just start out, I know I'm going to print stuff out, so I'll include extreme may
include namespaces me.
I want that in their main function going, Yeah, let me go ahead and just save this right away.
I think I'm just going to call this problem to keep.
Cool.
So.
Let's go ahead and.
I have the terminal here as well.
Want to clear that?
So let's go ahead and break down this problem, so make a program that reads three characters into an
array.
So we would maybe be thinking that we're going to need some variables to read into, but it doesn't
say to read it into variables.
It says to read it into an array.
So I know for sure I'm I need an array, so I'm going to go ahead and do that.
So I'm just going to call this.
It's going to be an array of characters, right?
Because we're reading in characters three of them.
So.
What I can do is I'm going to I'm just going to call this a RR just for array.
You can call it wherever you want, and it's going to be size three.
So I'm just going to declare my array like that.
So then it says to read into it, so you're probably like wondering, how do I do that?
I got to read it into array.
I read it into a variable form, but I don't know how to read into array.
Well, very similar thing.
So promise if you were able to figure this out on your own, if not totally fine.
But rather than like, let's just say you have something to write, you would just be like seeing into
a.
But if you got to read into the array, you have three different positions that you want to put the
characters at, right?
So what's the first position?
The first position is indexed like this write array is zero.
So all you have to do is see into that.
So see into a R zero and then see into a R one and so on with one and then air two.
So just like that, just like reading into a variable, you read into the array because the array has
three slots, right?
So since we're entering three characters you can read, read into these three slots here.
So, you know, remember that it starts out at zero, the first position is zero in computer science,
we're starting with that index zero.
Most of the time, there are some programming languages that start at one.
I'm not going to talk about those, but most programming languages are starting with the index zero.
So we read into the first position of the era with that first character that the user would enter,
then the second position of the array is a second character and then this is the third position of the
era right since we started here.
These are all offset by one.
They're one less.
Before we read in, though, we're going to have to put out a little message, right?
So the message says, enter the three-letter password.
So let's go ahead and check that out then.
So I'm going to go ahead and push out and enter, say nothing without a password.
The three letter password, yeah, the three letter password.
And we want it on the same line.
So I'm not going to put it in line there.
Cool, so we go ahead and read that in.
And then what needs to happen, let's go back to our problem description.
So once you have read all three characters and the user input into each position of the array, compare
each one of the characters in the array to the extreme, so we haven't made that string yet, right?
This program should have a hard coded string password variable that you create.
So let's go ahead and create that variable.
So we have the array here, and I'm going to go ahead and create a string, and I'm going to call this
string password.
And just say equals.
And in this example, I'll just put, you know, I had that in that example.
Yes, I thought it was something else, though.
We could just say one.
Something like that.
I don't know.
Pretty weak password, right?
So once you read all three characters and the user input in each position and the array, we've done
that right.
So now we need to compare each one of the characters in the array to the string.
So how do we do that if we're comparing them that is similar to like this previous problem, right?
So.
And actually, I'm going to.
Let's just present this to you, because I haven't had it right all the way up.
So this previous one, we were comparing Char and in.
Right, so we're doing another comparison here, which implies that we should probably use some more
conditional, right?
So let's go ahead and do that.
So I'm going to start out with another if and what I need to do is I need to compare.
Each array position, so like this, for example, in this in this, these are the three array positions,
and then we have three.
Positions of the string, right?
So we have one, two, three or more appropriately, zero one to remember how we actually were able
to index strings as well.
So we're going to compare our array indices to our string indices.
So this will be like a zero needs to be compared to this one right there.
All right.
One needs to be compared to this one right here.
Array two needs to be compared to this one right here.
Of course, since we know our password is.
The word one we could just compare directly to a character like a hard coded character like this, like
if Air Zero is equal to uh oh.
But the problem specifically says that.
Compared to one of the characters in the array to the string, so actually doesn't say to index the
string, but I think that.
It says right here, you know, does each character in the ring at a given position match the character
in the string at the same position?
So I feel like that kind of implies that we should index the string as well.
So in that way, if we wanted to do this, we wouldn't have to change our logic later, right?
Let's say we want to change the password to something else, then we don't have to go and change our
if statements are conditional statements.
You know, we don't have to come change our statement, if you know, however many are, if we have
one.
I'm not sure yet how many of you have.
We would only have to change the string if we use the string indexing.
So let's think about this, we need to for sure do this air zero equals equals and then we'll do password
zero like this that way.
This is just saying the first thing in the array is equal to the first thing in this password string.
So that's this will check the first thing.
So what do we do for the rest?
Do we make additional if statements?
Well, we we can.
But we also could just put it all in one if statement because we only care if everything matches right.
So we could say is if this first thing matches and.
And then I can just copy paste this.
And I can say, OK, not only the first thing, but the second thing matches, too.
And and then I'll copy paste again and I'll say and the third.
Position matches as well.
So this is if all three positions are matching, then we can print out.
That you guessed the password.
Password equals and whatever the password is, right?
So I will say you guessed the password.
Password equals and then right here I will just say password to variable.
All right.
Put it in line there.
So that'll print out the actual password and, you know, this text that was matching what was on the
output here.
So what if it doesn't match, though, then what do we print out?
So if it doesn't match, it just says wrong, guess?
So let's go ahead and handle that, so the case in which it doesn't match is basically when all of this
is false, right?
It just really if any one of these is false, actually.
So this is saying, you know, each position has to be true.
The first one and second position has to match and third position estimates.
If this is false, if this whole statement evaluates to false, that means that the passwords don't
completely match.
So that means that I could put an else here and then I can put that I could put wrong guess.
So foot wrong.
Yes.
And up in the line, I cannot Typekit in line correctly.
Let me.
OK, so that seems to pretty much cover it.
Let's go ahead and save this and test it out.
So I went ahead and say that I'm going to compile it now, so I'll save problem to
copy that and I'll call this.
I want to see.
OK.
Comp. All right, so let's go ahead and test this out
to see.
So let's go ahead and enter.
Let's enter the wrong password first.
So I'll just say to the wrong guests and then I will go ahead and say.
Yes, yes, I would say one.
Yes, the password password equals one.
Cool.
So seems to seems to work.
This program is.
You know, kind of not the best is as far as taking input because.
You know, it doesn't really check to see how long things are like, for example, if I was to run this
and I said like the password is password or something like that, says Ron Guess.
But you know, I'm entering extra stuff, which it probably should have a message that says, like,
you haven't entered a password of of length three.
So but you know, we're just kind of doing a basic program now.
So, you know, it still works.
It says wrong, guess.
But something we need to look into the into in the future is to see how, you know, checking for how
long things are that are entered, checking to make sure people entered the right format of something.
You know, there could be like some sort of seeing fail.
If I were to run this and I just put like, let's see, it's taking in charge, so I don't really know
how I would be able to make it to to fail.
But let's say that we are taking in integers, you know, and then I entered like a b c.
That would be a problem, right?
If I was reading into three integers, we already went over that.
So it would be good if we had some kind of mechanism to check what was going on in the input, rather
than just only match it to our password.
But, you know, it still works, still works pretty well.
So if you figure this out, definitely props to you and good job, if not, no big deal, you know?
I didn't go over how to read into positions in an array.
I was just trying to challenge you to see if you could maybe potentially figure it out.
If you had to look up stuff that's totally fine looking up stuff is definitely what you should do.
That's what most programmers do.
If you think that people just automatically write code without having to look stuff up and google it,
you would be greatly mistaken because programmers job is pretty much 90 percent Googling anyways.
So yeah, don't worry about looking stuff up.
So, yeah, we ran into each one of these positions that was kind of the new thing.
And then we used a conditional statement here, just one f where we combined each one of these statements,
these checks with the and operator logical and right here.
Cool, so I only have these two problems.
For this lecture after this, we're going to get into some other ways to take in input.
We will be looking at reading in a line of input.
I think I already kind of foreshadowed that in the last lecture I talked about that, but we'll look
into reading into strings as well, not only reading into the primitive types.
For example, if we were to read this into a string, we could check it as well, and we could also
check the length with like password length.
Right?
So that's something cool that we will do in the upcoming lectures.
OK, so with that, I will see you in the next lectures.