C Language Programming sixth time-cycle structure (2).

Source: Internet
Author: User

(a) Correct the wrong question
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
source program (with the wrong program):
Error message 1:
Error reason: input error, EPS is defined as double type, so the input should be%LF.
Correction method: Change%f to%LF:
Error message 2:
Cause of Error: Error in calculation form, item is double, and 1/n is integral type calculation method.
Correction method: Change 1 to 1.0:
Error message 3:
The cause of the error, an expression error.
Correction method: Change < to >=:
Error message 4:
Error reason: The Do.....while statement uses an error and the end of the sentence is missing a semicolon.
Correction method: Add a semicolon after the while:
Error message 5:
Error reason: The output format is wrong and does not meet the requirement of the problem.
Correction method: Change%f to%.6f: [] (Http://images2017.cnblogs.com/blog/1242757/201711/1242757-20171116212624921-2062351957.png
Error all corrected after the source program and running results:

(ii) Study summary

1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
A: Both of these are the meanings of infinite loops. The while (1) statement can add a break at the end to ensure that the loop jumps out. and for (;;) You can add one in the loop to make the loop work.
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) The number of cycles is known
(2) The number of cycles is unknown, but the loop condition is explicitly
(3) The number of cycles is unknown when entering the loop, and the loop condition is unknown when entering the loop, it needs to be explicitly
for the above three cases in the loop body. What loop statements are used to achieve better? For each case, use the topics in the two-cycle structure work we have completed to illustrate.
A: When the number of cycles is known, use the For statement. Most of the loop job 1 uses the For statement, for example, 7-1 to find one of the odd points in the sequence of the first n items and, requires the program, computes the sequence 1 + 1/3 + 1/5 + ... The sum of the first n items. Make the question clear the number of cycles. With a for statement. The
is preferred with a while statement when the number of loops is unknown, but the loop condition is clear when entering the loop. Loop Job 2 7-5 A worm is 1 inches long, at the bottom of a well with a deep n-inch. The worm is known to climb up to U-inch every 1 minutes, but must rest for 1 minutes to go up. In the course of the rest, the worm slipped down the D-inch. In this way, the climb and slide are repeated. Excuse me, how long does it take for the worm to crawl out of the well? This requires less than 1 minutes of 1 minutes, and it is assumed that the worm will complete its task as long as the worm's head reaches the top of the well during a crawl. Initially, the worm is lying at the bottom of the hole (i.e. height is 0). For this problem, the number of iterations is not clear and the while statement is used.
when the number of cycles is unknown, and the loop condition is unknown when entering the loop, it needs to be explicit in the loop body, using the Do While statement. Loop Job 2 in 7-1 to find a simple staggered sequence of the given precision part and, requires writing programs, calculating the sequence portion and 1-1/4 + 1/7-1/10 + ... The absolute value of the last item is not greater than the given precision EPs. The number of iterations is ambiguous and the loop condition is ambiguous, so the condition can be satisfied before execution. Use the Do While statement.

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?
For statement:

While statement:

Do While statement:

I think using while is better, although the number of cycles do not know, but not too much, and the operation is more convenient.

4. Run the following program, enter 1 to 10, what are the results? Why?
(1): Run the program and results:

(2): Run the program and results:

Because break is the execution flag that jumps out of the loop, (1) The program ends when it executes to break. and continue in the loop is returned to execute the meaning again, so the program executes to continue will again make I plus 1, so that the program continues.

(iii) Experimental summary

This experiment summarizes 1, 2 and 3 questions in the cycle structure (2).
1:
(1) Title
The simple staggered sequence part of the given precision and
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
Issue 1: The result is always output 1.
Reason: The number of bits in the item is controlled.
Workaround: Double type control.
Issue 2: The result of the S overflow phenomenon.
Cause: Too many bits, the shaping variable is not enough to provide more bytes.
Workaround: Use double to define the result s.
(5) PTA Submission List

2:
(1) Title
Guess number Game
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
Issue 1: When entering negative numbers is a special case, and all output game over conditions are messy.
Reason: No detailed interpretation of the topic, the analysis of the situation is not clear thinking.
Solution: Be patient and examining and do it carefully.
(5) PTA Submission List

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

(3) Source code

(4) Experimental Analysis:
No problem.
(5) PTA Submission List

(iv) Mutual evaluation of blogs
Evaluate the wind away from your blog in the garden homework.
Wind from your blog link: http://www.cnblogs.com/GX201701-/
His blog this time the operation of the garden is more rapid, the work is clear, clear thinking, for the benefit of others to understand.
Evaluate Guo Yulin's Blog Park assignments.
Guo Yulin's Blog Link: http://home.cnblogs.com/u/HBQ521/
His blog garden homework completed better, the wrong problem is very clear pointed out the mistake. is also very clear. Homework questions also have their own ideas.
Evaluation of the solitude of a grain of sand blog garden homework.
Lonely A grain of Sand blog Link: http://home.cnblogs.com/u/HGD980425/
His this time to do a more rapid completion of the industry, but also the completion of the effect is very good, very clear, clear thinking, the overall feeling is very neat, should learn from him.

C Language Programming sixth time-cycle structure (2).

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.