Hello, and welcome.
You guys up for another challenge?
I know I am.
Now that we're done talking about loops, let's apply them.
Let's try to figure out how to solve a problem.
So in this challenge, you're going to create a guess the number c program.
It's going to be pretty cool.
It's going to be a little game.
What's going to happen is your program is going to generate
a random number from 0 to 20.
After you've generated that random number, you're going to
ask the user to guess what it is.
So the user should only be able to enter numbers from 0 to 20.
If they try to guess like the number 50, you're going to display
an error and say, hey, you can only enter numbers from 0 to 20.
So that's one stipulation.
But once the user actually gets a guess and it's a valid guess, you're
then going to tell the user whether or not they were too high or too low.
Or if they were correct, you'll display saying that they're
correct and they will win the game.
Now they only have a certain number of times that they
can guess the correct number.
They can only guess the number 5 times.
After five times they lose the game.
So this game is all about guessing the correct number.
Any number from 0 to 20, you want to validate the input from the user to
make sure they entered for 0 from 20.
And if they entered something and they have less than 5 tries, you're going
to tell them, hey, this was too high or, hey, this was too low than the
number that they're supposed to guess.
And those are the basic requirements of the program, a
basic guess your number type game.
Now what's the output look like.
To give you an idea of how I designed it, you may want to follow.
This. When I first started the program, I displayed a little message.
I said this is a guessing game, just so they know what's going on.
I then told them I have chosen a number between 0
and 20, which you must guess.
You have five tries left to guess.
I then said enter a guess.
They were supposed to enter their guess.
They entered 12 and I printed out, sorry 12 is wrong my
number is less than that.
And then I displayed you have four tries left.
Then they entered a guess of 8, and I said, well, 8 is wrong
too, my number is less than that.
And then they entered 3, I said they had 3 tries left, they entered 4.
I said 4 is wrong.
And then they had 2 tries left, and they finally
guessed my number which was 2.
Once they guessed the correct number, I told them congratulations, you
guessed it, so they won the game because they still had 2 tries left.
It's a basic type of simple game where you're going to need to
use if statements, you're going to need to use while loops.
So you're going to need to use a loop in this because you have to
keep asking for input until the user guessed correctly or until
the number of tries was over 5.
So one of the best ways to start a program like this is
to write down how you're going to create the program on paper.
And I would suggest you do that because if you write down how you're
going to attack the problem on paper, it's going to be easier to solve.
And you can write it out in pseudocode, so it's just English
language, but you want to identify where you're going to have the loops
and where you're going to have the if statements to check for conditions.
Because f you look at this output, there's many conditions
that you have to check for.
The while loop is going to check for conditions or the for loop it's
going to check to make sure that it exits loop if you guessed correctly
or if you're higher than 5 for tries.
You're going to have conditions to also validate what they entered.
So you're going to have an if statement to say, hey,
you just entered a number.
And if it's not between 0 and 20 and that may be you also may want
to have a nested loop for that case.
Because you want to keep asking them to enter another number
until they enter a valid number, not the correct number that you
guessed, but a valid number.
So I could see for this program an outer loop.
The outer loop would be the loop that asked the user to enter numbers until
they have successfully guessed the correct number or until they have
succeeded their number of tries.
I could see an inner loop where it asks if the number is not valid
between 0 and 20, they keep asking the user to enter a valid number.
And then I can see if statements to basically check whether or not the
number is too low, too high or equal.
Because you have multiple exit conditions for your outer loop
whether or not you guessed correctly or whether or not the
numbers equal that may be a good case for using a break statement.
Because remember, you use the break statement when
you have multiple exits.
And so you could have something inside your outer loop that says
if they guess correctly, jump out.
And then the condition in the outer loop itself in the while loop or
the for loop itself, the condition x it would be whether or not the
number of tries is greater than 5.
So that's one design approach.
I mentioned many things.
The only thing left, you have everything at your disposal.
If you learned everything I menti1d except this random number generator.
This is new.
You know how to do if statements, you know how to do loops, you
know how to declare variables, you know all these nice things.
You know how to print output to the display, but you don't know how
to do a random number generator.
So I'm going to give you the code for that.
You need to generate a random number.
Because each time you run the program, you want to have a
new number that they can guess.
So to generate a random number from 0 to 20, these are the
steps that you have to follow.
First you have to include 2 additional libraries, the stdlib.h, time.h.
So you want to add these include directives at the top of your file.
Sdtlib.h will allow you to use the srand function, which allows
you to generate random numbers.
Time.h allows you to provide the seed for the random number using
the time, getting the current time.
So when we include additional libraries outside of our program,
it just means we want to perform some additional functionality.
We haven't talked about all the standard libraries that c comes
with, but they come with 2 nice ones to get the current time
and also to generate a random number that we're going to use.
So we include those first 2 directives at the top.
Then we're going to create a variable inside of our main
function called time_t, and you can just name it t, you add that.
Once you add your time variable, you can initialize your random number
generator using the srand function.
So what you do is you say srand.
This provides a seed to the random generator, it's
just used to initialize it.
And here you just copy this line of code srand ((unsigned) and then time
and then you pass an ampersand t.
Don't even have to understand what this code does,
just copy and paste it.
But it is generating a random, it's initializing
the random number generator.
After you've initialized a random number generator, you
have to generate the number.
And all you do to generate the number is you say rand, you call
a function named rand and you say %21 that's a module because
you want it to be between 0 and 21, you use 21 hard-coded.
You can store that random number in an integer, and that's
the number you're going to compare against to exit the loop whether
or not they guessed correctly.
It's also going to be the number that you compare against uh
to determine whether or not you print too low or too high.
So this should be a lot of fun.
Again, a lot of concepts you're going to be exposed to, if statements,
looping constructs, the break statement, inner and outer loops,
nested loops, a lot of nice stuff.
So good luck, have fun, and I'll provide a solution
in the next lecture. Thank you.