yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

C lesson 4


3m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

Hey guys, this is M kids and one today with our fourth and final C lesson. So in this C lesson, we're going to be making a gamish program, and basically what it is, is you have to guess a number between 1 and 1,000. When you guess it wrong, it tells you whether the number you guessed is too high or too low.

So, what are the new things that this program does? The first one is a random number generator. That's pretty cool in my opinion. The random number generator is how you generate pretty much anything randomly; you get a number and then you use if statements. So this is more using the random number generator.

Okay, then the second thing that's new with this, and that's even more important, is that I use four loops in this application. So, a loop is a piece of code that runs a certain amount of times, and you can use some logic with that, like make it run if a statement is true. Like if something happens, then don't do this again, stuff like that.

So, um, I'm going to run the program. The high low is what I like to call it, and then we'll type in the code. Now, in the code, I will warn you there are four warnings normally, but ignore them because they really don't cause any trouble whatsoever. So, I'm opening up the application.

So, it says, "I'm thinking of a number between 1 and 1,000. What is it?" You might ask. "Guess it and win!" So, I'll take my first guess. My guess will be 500. Too low! So then I'll guess 750. Too low! 850. Too low! 950. Too high!

Okay, so obviously it's taking me a little while, so I'll type cheat. So, cheat is my cheat code, so the answer is 912. So it says, "912 is the number guessed in 10 attempts." So the source code fills up about three-fourths of a page; it's not that hard.

So first of all, open up Xcode, create a new project, command line utility standard tool, call it high. So, now on main.c right here at the beginning, after include <studio.h>, we're going to do an include <string.h> and another include <math.h>. I'm not actually sure if we have to include the <math.h>, but it's cool to include anyway.

So, right here where it says "Hello World," just say "Guess the number." Okay, so now we're going to, um, generate a random number. So first, um, let me explain random number generators. You seed them, which gives them a value to base their random number off of. So we're going to be seeding it with the time, the exact amount of seconds since 1970, January 1st, midnight.

So, um, the way to get the time and to seed a random number generator, you do srandom(left parenthesis) then the number you're going to seed it with. I'll do time(left parenthesis) null in all caps right parenthesis another right parenthesis semicolon.

Next, we're going to actually get the random number, so I'm going to make a new integer in rand, and then I'll do equals random(left parenthesis) right parenthesis space percent space 1,000. Then I'll do rand equals rand + 1. Okay, so now we'll have a random number that's between 1 and 1,000.

So, let me explain what this percent thing is. Percent means make this a number, a value between 1 and 1,000. So it takes it and it shrinks it down to a smaller number, but that will actually not make it between 1 and 1,000; it'll make it be between 0 and 900. So that's why we're adding one to it.

So, now right under this, we're going to make two integers, int guess and int guesses. So now I'm going to make guesses equal zero. Okay, so now this is where we're going to do that loop that keeps on looping over and asking us to enter a new number.

So, I'll say for because that's the type of loop, a for loop space left parenthesis guess equals minus one guess bang equals rand. That means doesn't equal; the exclamation point equals means it doesn't equal guess plus plus right parenthesis.

So, the first thing before the semicolon here runs the first time it gets to this line. This loop, the second thing checks if it's true. If it is true, then it does it, or if it is true, then it stops. So it does it while guess doesn't equal random, so it'll keep on doing it as long as this statement is true.

After the second semicolon, the third statement in here is something that happens every time. So we're adding one to the integer guesses. So th...

More Articles

View All
How To Be Alone | 4 Healthy Ways
He who sits alone, sleeps alone, and walks alone, who is strenuous and subdues himself alone, will find delight in the solitude of the forest. - The Buddha. Some people avoid solitude like the plague. Others love being alone and thrive best in solitude w…
Connecting f, f', and f'' graphically | AP Calculus AB | Khan Academy
We have the graphs of three functions here, and what we know is that one of them is the function f, another is the first derivative of f, and then the third is the second derivative of f. Our goal is to figure out which function is which— which one is f, …
There Are Thousands of Alien Empires in The Milky Way
What if there are thousands of alien empires in the Milky Way, each spanning a few to maybe tens of star systems, chatting, trading, sometimes shooting or ignoring each other politely? And if so, why is nobody visiting us? While the Milky Way is ancient a…
5 Things to Know About Virunga | Explorer
[Music] Veranga is Africa’s oldest and largest national park. It’s an enormous area, 3,000 square miles of protected space. But perhaps most importantly, it’s home to most of the world’s critically endangered mountain gorilla. Barunga is a UNESCO World He…
Place value when multiplying and dividing by 10 | Math | 4th grade | Khan Academy
What is 700s * 10? Well, let’s focus first on this times 10 part of our expression. Because multiplying by 10 has some patterns in math that we can use to help us solve. One pattern we can think of when we multiply by 10 is if we take a whole number and…
Possessive pronouns | The parts of speech | Grammar | Khan Academy
All right, grammarians, let’s get down to it and start talking about possessive pronouns. A possessive pronoun is a pronoun that we use to show possession of something, which is just sort of a fancy way of saying you have it. So possession equals having s…