Kodable: programming for kids

This "guess the number" game is a great introductory program when learning a new programming language. Here's how to lớn write it in C.

You watching: Kodable: programming for kids


*

I taught myself about programming baông xã in elementary school. My first programs were on the Apple II, but eventually, I learned C by reading books và practicing. And the best way to practice programming is to lớn write sample programs that help exercise your new knowledge.

One program I like to write in a new language is a simple "guess the number" game. The computer picks a random number from 1 khổng lồ 100, và you have to figure it out by making guesses. In another article, I showed how to write this "Guess the number" game in Bash, and my fellow phung123.com authors have written articles about how to write it in Java, Julia, and other computer languages.


What"s great about a "Guess the number" game is that it exercises several programming concepts: how khổng lồ use variables, how to compare values, how lớn print output, and how lớn read input.

Over the summer, I recorded a video clip series khổng lồ teach people how khổng lồ write programs in the C programming language. Since then, I"ve sầu heard from many people who are learning C programming by following it. So, I thought I"d follow up by writing a "Guess the number" game in C.

Pichồng a random number

Start the "Guess the number" game by writing a function to lớn pichồng a random number. When writing functions, good programmers try to make them flexible, so they can reuse them to solve sầu slightly different problems. So, instead of hard-coding the function to pick a random number between 1 và 100, write the function khổng lồ piông chồng a random number between 1 và some integer value maxval:


#include #include intrandnum(int maxval) /* piông chồng a random number from 1 khổng lồ maxval */ int randval; getrandom(&randval, sizeof(int), GRND_NONBLOCK); /* could be negative, so ensure it"s positive */ if (randval 0) return (-1 * randval % maxval + 1); else return (randval % maxval + 1);
The function uses the Linux system call getrandom to lớn generate a series of random bits. You can learn more about this system Call on the man page, but note that getrandom will fill the variable with random zeroes & ones. That means the final value could be positive sầu or negative, so you need to lớn do a kiểm tra afterward to ensure the result of your randnum function is a positive value.

See more: Từng Có Một Người Yêu Tôi Như Sinh Mệnh Ebook, Từng Có Một Người Yêu Tôi Như Sinh Mệnh

Write the program

You can use this function to lớn write your "Guess the number" program:


#include #include intrandnum(int maxval) ...intmain(void) int number; int guess; number = randnum(100); puts("Guess a number between 1 and 100"); vì chưng scanf("%d", &guess); if (guess number) puts("Too low"); else if (guess > number) puts("Too high"); while (guess != number); puts("That"s right!"); return 0;
The program starts by picking a random number between 1 & 100 using the randnum function. After printing a prompt to lớn the user, the program enters a do-while loop so the user can guess the number.

In each iteration of the loop, the program tests the user"s guess. If the user"s guess is less than the random number, the program prints "Too low," và if the guess is greater than the random number, the program prints "Too high." The loop continues until the user"s guess is the same as the random number.

When the loop exits, the program prints "That"s right!" & then immediately ends.


$ gcc -o guess -Wall guess.c$ ./guessGuess a number between 1 and 10050Too high30Too low40Too low45Too high42Too low43Too low44That"s right!

Try it out

This "guess the number" game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare details in each language.

See more: Tải Download Photoshop Cs6 Full Crack Vĩnh Viễn 1000% Google Drive

Do you have sầu a favorite programming language? How would you write the "guess the number" game in it? Follow this article series to see examples of other programming languages that might interest you.


*

The C++ language is complex, but it can teach you a lot about data types, memory management, và code linking.
*

Writing simple games is a fun way lớn learn a new programming language. Put that principle lớn work lớn get started with Java.

Chuyên mục: Chia sẻ