C Language Programming sixth time job

Source: Internet
Author: User

(a) Correct the wrong question

(1) Sequence summation: Enter a positive real number EPS, calculate the sequence portion and 1-1/4 + 1/7-1/10 + ..., the absolute value of the last item is less than the EPS (keep 6 decimal places).
Input/Output Sample:
Input eps:1e-4
s = 0.835699
(2) source program (with the wrong program):

  • Error message 1:
  • Error cause 1:
    A semicolon is missing after the while statement on line 17th.
  • Correction Method 1:
    Add a semicolon after the while statement on line 17th.
  • Error message 2:
  • Error cause 2:
    In the discovery topic, n is defined as int, and N participates in the division operation, so there is a problem with the output of the number type.
  • Correction Method 2:
    Add a point to the 1 after 12 rows.
  • Error message 3:

  • Error cause 3:
    The answer is not correct, both input and output are%f.
  • Correction Method 3:
    Change "%f" to "%LF" and "%.6f" when entering and outputting.
  • Error message 4:

  • Error cause 4:
    The while statement is a circular closing sentence, and the question gives the statement that the loop jumps out.
  • Correction Method 4:
    Change the less-than sign to a greater than equals sign.
  • All correct and bring in several sets of data for running calculation

    The results of the operation are consistent with the expected results.
  • The correct generation source

    (ii) Study summary 1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
  • Difference:
    (1) because "1" is true, the statement while (1) represents an infinite loop of statements inside a break statement when there is no closing loop.
    (2) because for (;;) There is no judging condition, as an empty statement, so for (;;) is also an infinite loop.
  • Judge:
    The meaning of these two loops is equivalent, in order to ensure that the infinite loop can meet the desired way to run and produce results, need to exit the loop at the right time, to achieve this goal we can use the break statement to exit the loop, to ensure that the condition is "1", that is true.
    2. In general, when designing a looping structure, it is possible to use the for, while, does while three statements, and three statements can be converted to each other, but in some specific cases, we should first select some kind of statement to quickly implement the loop design. If the following conditions are true:
    (1) Number of cycles known
    (2) The number of cycles is unknown, but the cyclic conditions are clear when entering the cycle
    (3) The number of cycles is unknown, and the loop condition is unknown when entering the loop, it needs to be clear in the loop body
    For the above three cases, what is the use of the loop statement to achieve better? For each case, use the topics in the two-cycle structure work we have completed to illustrate.
    (1) Number of cycles known:
    Using a For loop typically causes the statement block to repeat the specified number of times. We use the For loop counter to specify the number of cycles. For example, we learn the loop structure (1) 7-5, because the number of cycles known, so with a for loop, the source program such as:

    (2) The number of cycles is unknown, but the cyclic conditions are clear when entering the loop:
    Although the number of cycles is unknown, but the loop condition is clear when entering the loop, using the while statement, for example, we learn the loop structure (2) 7-3 questions, the source program is as follows:

    (3) The number of cycles is unknown, and the loop condition is unknown when entering the loop, it needs to be clear in the loop body:
    The number of cycles is unknown, and the loop condition is unknown when entering the loop, so it needs to be clear in the loop body, using the Do...while statement, for example, we learn the loop structure (2) 7-1 questions, the source program is as follows:

    3. The following questions: Enter a batch of student scores, ending with 1 to calculate the student's average score.
    It is required to use the For statement, while statement, do While statement three loop statement implementation, and explain which form you think is more appropriate?
  • The source code for the while statement is as follows:
  • The source code for the For statement is as follows:

    The source code for the Do...while statement is as follows:

    Using the Do-while statement is not the same as using the for and while statements because it is more complex to exclude users from entering-1 directly at the first number.
    It is more appropriate to use the while statement on this topic. Because this program is an unknown number of cycles, but the loop condition is clear when entering the loop, if you write with a for statement or a do...while statement, you need to add a judgment statement more than the while trouble.
    4. Run the following program, enter 1 to 10, what are the results? Why?
  • Program 1:
  • Program 2:
  • Output result 1:
  • Output Result 2:
  • Analysis and reasons for two programs:
    The only difference between the two programs is the break loop control statement used by the first statement, and the second statement uses the Continue loop control statement, the difference between the two loop control statements is that break is the end loop and jumps out of the loop statement, and then executes the statement outside the loop, This statement when input 2 is to jump out of the loop, and continue is to end this cycle, out of the loop body has not yet executed the statement, continue the next cycle, until this normal end, we can also modify the source program to achieve the intended purpose, but are not as concise as the continue statement.

    (iii) experimental summary 7-1 The simple staggered sequence part of the given precision and
    (1) Title:
    The subject asks to write the program, computes the sequence part and 1-1/4 + 1/7-1/10 + ... The absolute value of the last item is not greater than the given precision EPS
    (2) Flowchart:


    (3) Source code:

    (4) Experimental Analysis:
  • Problem:

    The result of the output is 1.00000.
  • Reason:
  • Workaround:
    Add point zero after 1.
    (5) PTA Submission list:

    7-2 guessing number games
    (1) Title:
    Guess the number game is to make game console randomly produce a positive integer within 100, the user enters a number to guess, you need to write a program automatically compare it with the randomly generated guessed number, and the hint is big ("Too big"), or small ("Too small"), equal to guess. If guessed, the program is closed. The program also requires the number of statistical guesses, if 1 times to guess the number, the hint "bingo!" If the number is guessed within 3 times, it prompts "Lucky you!" If the number is more than 3 times but is within N (>3) (including nth), it prompts "good guess!" If you don't guess more than n times, prompt "Game over" and end the program. If the user enters a negative number before reaching n times, it also outputs "Game over" and ends the program.
    (2) Flowchart:


    (3) Source code:


    4) Experimental Analysis:
  • Problem:
    Total partial error when submitting.
  • Reason:
    I used the Do...while statement to confuse the out-of-the-back statement.
  • Workaround:
    After submission, carefully review the submission data and the topic requirements.
    (5) PTA Submission list:

    7-3 Find Odd and
    (1) Title:
    The subject requires the calculation of the odd number of a given series of positive integers.
    (2) Flowchart:

    (3) Source code:

    (4) Experimental Analysis:
    The problem is not very difficult, is to judge the data in the infinite cycle and after a certain condition to jump out of the loop and output data, the output of the result must be at the end of the loop to summarize the data.
    (5) PTA Submission list:

    (iv) Mutual evaluation of blogs

    Http://www.cnblogs.com/liukaixuan/p/7855946.html
    Http://www.cnblogs.com/shaosiming/p/7854102.html
    Http://www.cnblogs.com/jjjj1234/p/7857951.html

C language Programming sixth job

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.