C Language Programming sixth time-cycle structure (2)

Source: Internet
Author: User
Tags html comment

(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).

Pre-Modified source code

Error message (1):

Cause of error: Use the Do While loop to add a semicolon after the while, this statement special need to remember

Correction method: Add a semicolon after the while

Error message (2):

Cause of Error: item = 1/n; n is defined as integral type in the second statement, so 1/n does not appear decimal

Correction method: Put item = 1/n; Change to item = (double) 1/n;

Error message (3):

Cause of Error:}while (item < EPS); this statement is incorrect; because the loop condition in the loop statement is when the item < ESP condition satisfies the output, and the source code is misunderstood as item > ESP when the output

Correction method: Put}while (Item < EPS), change to}while (item > EPS);

Error message (4):

Cause of error: In the problem of the last item less than EPS is required to add

Correction Method: Add the statement s = s + flag * Item before the output statement at the end of the loop statement;

Error message (5):

Error reason: Double variable in the input quotation mark, if it is decimal, output with%f, and input with%LF

Correction method: The scanf ("%f", &eps), the sentence changed to scanf ("%lf", &eps);

The source code after the output has been modified:

Match with expectations!

(ii) Study summary

(a) statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?

(1) is an infinite loop, which is followed by a break after the IF statement in the Loop statement, so that it can jump normally and execute the

(b) 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) The number of cycles is known to be better with a For loop statement

(2) The number of cycles is unknown, but the loop condition explicitly uses the WHILE loop statement to achieve better performance when entering the loop

(3) The number of cycles is unknown, and the loop condition is unknown when entering the loop, it is better to use the Do While loop statement in the loop body explicitly

For Loop Statement Example: The third problem in the circular structure of PTA (1)

While Loop Statement Example: The fourth problem in the circular structure of PTA (2)

Do and Loop Statements example: The fifth question in PTA cycle structure (2)

(c) The following questions have been put: Enter a batch of student scores, ending with 1 to calculate the average student's performance.

Implemented with a For loop statement

Implemented with a while loop statement

Implemented with Do and loop statements

I think it's better to use the while statement, because the number of loops is unknown, but the conditions before entering the loop are clear, compared with a for statement or a Do While statement is more troublesome

(d) Run the following procedure, enter 1 to 10, what are the results? Why?

First program source code and results

The second program source code and results

First result reason

Break, which means that the above statement is run out, so when run out of 1 o'clock, run 2 results directly out of the run and operation, and the value of S is 1, so the output is 1

The second cause of the result

Continue, which means no subsequent statements, and then back to run the above steps, this program represents 10 number of odd sum, so the result is 25

(iii) Experimental Summary (i) The simple staggered sequence part of the subject 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.

(1) Flowchart:

(2) Source code:

(3) Experimental analysis:

Question 1: The problem is the odd item is plus, even the number of the sum is reduced, how to make each item is changed

Reason: There is no flexible use of defining variables so that the defined variables are changed once every loop

Solution: Ask classmates, find the problem that has been done before, to solve the idea, solve the problem

(4) PTA Submission list:

(ii) The title guessing 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.

(1) Flowchart:

(2) Source code:

(3) Experimental analysis:

Question 1: At first sight this question is completely blindfolded, do not know how to do

Reason: Not to turn big problems into small problems, starting from the details of minor issues

Solution: Ask classmates, surf the internet to check the information, get the problem solving ideas

Issue 2: This topic is more than the case, there is no association between the various cases

Reason: lack of logical thinking

Solution: Ask classmates, in the Exchange group to ask seniors, and finally get the correct results

(4) PTA Submission list:

(iii) The question of odd and

The subject requires the calculation of the odd number of a given series of positive integers.

(1) Flowchart:

(2) Source code:

(3) Experimental analysis:

Question 1: How to judge an odd number and add it, you need to use the Loop statement and the judgment statement nested use

Reason: The problem to ask for odd and, therefore need to judge odd

Solution: Find the previous courseware, review the teacher's story, write the correct procedure

(4) PTA Submission list:

(iv) Comments on the blog commentary Beep redgummies blog

Change the wrong part of the idea clear, learning summary part of the summary is very comprehensive, it is worth to the Beep redgummies learning, in the second error of the wrong problem changed two mistakes

Beep redgummies Blog Address: http://www.cnblogs.com/bilililili/p/7842022.html comment on a cat Lei's blog

This learning method is well worth learning by marking the wrong place in the correct part of the problem.

A cat lei? Blog address:

Http://www.cnblogs.com/8426224ll/p/7847221.html

Comments on mobile, kinky collar four hooker night Things blog

The student's blog writing is very good, the wrong part of the idea is clear, and each score is very high, is my study role model!

Mobile, kinky collar four prostitute night Things blog address:

Http://www.cnblogs.com/yjy751522356/p/7838296.html

C Language Programming sixth time-circular structure (2)

Related Article

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.