[C language exploration tour] Part 1 Lesson 10: Exercise questions + exercises, study tours

Source: Internet
Author: User

[C language exploration tour] Part 1 Lesson 10: Exercise questions + exercises, study tours


Introduction


1. Course outline

2. Part 1 Lesson 10: exercise + exercise

3. Part 2 Lesson 1: Modular programming


Course outline


Our courses are divided into four parts. Each part has exercise questions after completion and answers will be published. Three games will also be written in C language.


Basic knowledge of C Programming


  • What is programming?

  • To do their best, you must first sharpen your tools.

  • Your first program

  • World of Variables

  • Computing

  • Conditional expressions

  • Loop statement

  • Practice: The first C games

  • Function

  • Exercise questions

  • Exercise: Perfect the first C games


C Advanced Technology


  • Modular programming

  • Attack pointer, C trump card

  • Array

  • String

  • Preprocessing

  • Create your own variable type

  • File read/write

  • Dynamic Allocation

  • Practice: "hanging villain" games

  • Secure text input

  • Exercise questions

  • Exercise: Explain the pointer in your own language


Develop 2D games with C language-based SDL Library


  • Install SDL

  • Create a window and canvas

  • Show images

  • Event Processing

  • Practice: "Super Mary pushes boxes" games

  • Time usage

  • Use SDL_ttf to edit text

  • Use FMOD to control sound

  • Practice: Visual Sound line

  • Exercise questions


Data Structure


  • Linked List

  • Heap, stack, and queue

  • Hash table

  • Exercise questions



Part 1 Lesson 10: exercise + exercise


The first part of the course has all ended. Before starting the second part of the exploration journey, we must consolidate the knowledge we learned in the first part.


So I have prepared some exercise questions for you: 15 multiple choice questions + a small exercise

15 questions (single answer) are attached to the answer at the end of this lesson, but I hope you will finish the answer first.


Exercise questions


  1. What is the program responsible for converting source code into binary code?

    A. Speaker

    B. Compiler

    C. Transcoder


  2. Which of the following is not an IDE (integrated development environment )?

    A. NotePad ++

    B. Visual Studio

    C. Code: Blocks

    D. Xcode


  3. In C language, which value is regarded as false (false) of a Boolean value )?

    A. 1

    B. 0

    C.-1

    D.-2


  4. What does return 0 in the following code mean?

    # Include <stdio. h>

    Int main ()

    {

    Printf ("Hello world! \ N ");

    Return 0;

    }


    A. The program has not executed the operation.

    B. The program is not running properly.

    C. The program runs normally.


  5. Which of the following is a line break in C?

    A. \ t

    B. \ n

    C. Just press the Enter key on the keyboard.


  6. If I have a variable bankAccount (bank account) whose type is long and its value is 6500000 (local tyrants), what will the following code output?

    Printf ("your bank account has % ld RMB \ n", bankAccount );


    A. Your bank account has % ld RMB

    B. Your bank account has 6500000 RMB

    C. Your bank account has ld yuan and bankAccount


  7. Which of the following types of memory won't be cleared after the computer is shut down?

    A. Register

    B. High-speed cache

    C. Memory

    D. Hard Disk


  8. After this operation, what is the value of the result variable?

    Result = (8/3)-2;


    A.-2

    B. 0

    C. 1

    D. 2


  9. What are the problems with the following switch statements?

    Switch (variable)

    {

    Case 5:

    Printf ("Hello ");

    Case 12:

    Printf ("hello ");

    Default:

    Printf ("goodbye ");

    }


    A. The break statement is missing.

    B. A semicolon is missing behind the braces of the switch.

    C. Enclose the commands under each case in braces.

    D. default should be written as case default


  10. Which for loop can display the following information on the screen?

    Line n ° 1

    Line n ° 3

    Line n ° 5

    Line n ° 7


    A. for (count = 1; count <9; count + = 2)

    B. for (count = 1; count <= 7; count ++)

    C. for (count = 0; count <9; count + = 2)

    D. for (count = 1; count <8; count ++)


  11. How many times will the following code display "Hello "?

    Int count = 14;

    While (count <15)

    {

    Printf ("Hello \ n ");

    }


    A. 0

    B. 1

    C. 14

    D. 15

    E. This is an infinite loop.


  12. Under which circumstances is the return statement not required?

    A. The function has no parameters.

    B. The function type is void.

    C. The function must return 0 values.


  13. What are function parameters?

    A. Specify the function name.

    B. Indicate the return value of the function.

    C. The variable that we pass to the function to make it work.


  14. What are the problems with the following functions?

    Int square (int number)

    {

    Int result = 0;

    Result = number * number;

    }


    A. The function does not return any value.

    B. The function cannot run, because we forgot a semicolon somewhere.

    C. No problem at all


  15. How many functions can a program contain?

    A. Only one is the main function.

    B. 100 at most

    C. 1024 at most

    D. No restrictions



Exercise answer

  1. B. Compiler

  2. A. NotePad ++

  3. B. 0

  4. C. The program runs normally.

  5. B. \ n

  6. B. Your bank account has 6500000 RMB

  7. D. Hard Disk

  8. B. 0

  9. A. The break statement is missing.

  10. A. for (count = 1; count <9; count + = 2)

  11. E. This is an infinite loop.

  12. B. The function type is void.

  13. C. The variable that we pass to the function to make it work.

  14. A. The function does not return any value.

  15. D. No restrictions


It is not difficult to answer questions. You can definitely get a high score after a careful review. Are you correct?

If you have any questions, please leave a message in the public account or contact Alibaba Cloud. Thank you!



Exercise: Improve the "more or less" game


In the previous article, we wrote the first C language game. Although it was very simple, there was no good-looking graphic interface (there would be milk and bread in the future), we could be proud of it!

Now let's repeat its source code:


# Include <stdio. h>

# Include <stdlib. h>

# Include <time. h>


Int main (int argc, char ** argv)

{

Int mysteryNumber = 0, guessNumber = 0;

Const int MAX = 100, MIN = 1;

// Generate a random number

Srand (time (NULL ));

MysteryNumber = (rand () % (MAX-MIN + 1) + MIN;

/* The loop part of the program. If the user does not guess the number, it will keep repeating */

Do

{

// Request the user to enter the expected number

Printf ("what is this number? ");

Scanf ("% d", & guessNumber );

// Compare the numbers and mysterious numbers entered by the user

If (mysteryNumber> guessNumber)

Printf ("I guess it's okay! \ N ");

Else if (mysteryNumber <guessNumber)

Printf ("I guess it's big! \ N ");

Else

Printf ("That's amazing. You guessed this mysterious number !! \ N ");

} While (guessNumber! = MysteryNumber );


Return 0;

}


Program explanation (from top to bottom ):

  1. Pre-processing command: the three lines starting with #. include indicates "include" in English. So it indicates the database to be introduced. I have already provided you with this part of code. If the error occurs during your program running, you are enough: P

  2. Variable: In this game, there is no need for too many variables. There is only one variable guessNumber used to record the number entered by the user, and the number mysteryNumber randomly extracted by a computer. At the same time, two constants (the const variable is actually called the read-only variable) MAX and MIN are defined, and the values are 100 and 1 respectively. The benefit of this definition is that if you want to change these two values later, it will be very convenient to directly change the two values in this row. If you do not use MAX and MIN, but write 100 and 1 in every place in the program, the workload will be huge if you want to change the value later.

  3. Random Number: srand and rand generate a random number between 1 and 100. The value is assigned to mysteryNumber.

  4. Loop: I chose do... while loop. In theory, a while loop can also be done, but I think the use of do... while here may be more logical. Why? Do you still remember the characteristics of do... while loop? That is, the commands in the loop body are executed at least once, unlike the while loop, which may not be executed once. Here we have to ask the user to enter a number at least once. It is impossible for the user to guess the number without entering it at once.

  5. Every time we run in the loop body, we request the user to enter a number and assign the value of this number to the guessNumber variable, next we will compare the size of guessNumber and mysteryNumber (the number to be guessed:

    If mysteryNumber is greater than guessNumber, the output is "Small guess" and the loop continues.

    If mysteryNumber is smaller than guessNumber, the output is "guessed" and the loop continues.

    MysteryNumber is equal to guessNumber, which is the case of the else statement, that is, We guessed it. The output is "great. you have guessed this mysterious number !", End Loop


Loop also requires a condition. The condition we give is that as long as the number to guess is different from the mysterious number, the loop continues.


The current game is still very basic, but there are the following improvements:

  1. Add a counter to record the number of steps and output it when you guessed it: "That's great. You guessed this mysterious number in ** step !"

  2. The previous program ended in only one round. What should I do if I want to continue the next round if the player is not satisfied? You can add a question: "Do you want to continue playing ?", Wait for the user to enter a number to answer. Define a Boolean value continue to store user input answers. For example, if the default value of continue is 1, the user continues to play the next round by default, but if the user inputs 0, the program stops, game ended

  3. Add a mode: two-person mode. You can give me a question to guess. However, I hope that you can choose which mode to play from the beginning of the program, whether it is a classic man-machine combat or a human-person combat. If it is in the two-person mode, it is not to use srand and rand to generate a mysterious number, but to allow the player to input this number through scanf.

  4. Set several difficulty levels for players to choose from: elementary (a number in 1-), intermediate (a number in 1-), and advanced (a number in 1 ). If you design this way, you need to rewrite the MAX value. At this time, MAX cannot be a const variable. You must remove the const before MAX and retain the MIN variable.


You can improve and expand this game to make it more entertaining and difficult.

The improved source code can be sent directly to the mini-editor by using the public account, or to the contact information of the mini-editor. Please feel free to contact us. Thank you!



Part 2 Lesson 1 notice:


Today's class is here to cheer up.

The first part is over. It's time to start the second part of the journey!

Next time, let's take the second part of the first lesson. Let's get to know about Modular programming!




Programmer Alliance public account* If you think this article is good, click "share to friends" or "send to friends" in the upper-right corner of the screen"

* New friends should follow the "Programmer alliance" Search Public AccountProgrammerleleague

Small No.: frogoscar

Xiaobian QQ: 379641629

Alimail: enmingx@gmail.com

(Most common with mailbox)


PS: A friend reported that reading articles on the mobile phone is too tired. In fact, they can be viewed on a browser webpage.

Method 1. click the "·" button in the upper-right corner of the screen, select "Copy link", paste the link to your browser, or send it to yourself by email, you can open it in your computer's browser.



Method 2. toutiao.comWww.toutiao.com, Search my self-Media "Programmer alliance", which has an article, you can also directly into this link: http://www.toutiao.com/m3750422747/



How do new friends view all articles:

Click "view public account" and then "view historical messages"





The "Programmer alliance" public account is designed for programmers and App designers. You love programming and sharing to push a wide range of programming-related knowledge, excellent software recommendations, and industry trends. Search for ProgrammerLeague and follow-up ~


Continuous attentionProgrammer AlliancePublic Account, more interesting, expected, and content with highlights are waiting for you!



Click "read the original article" below to view the Chinese version of "C programming language" 2 prepared by Dennis Ritchie. PDF: Baidu cloud disk download (you can open a file on your mobile phone and view it directly)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.