C Language Programming sixth time-cycle structure (2)

Source: Internet
Author: User

(a) Correct the wrong question:
Error message One:

Error reason: There is no semicolon behind the while.
Correction Method:

Add a semicolon after the while.

Correct the error message one after the compilation without errors, but run an error.

Error message two:

Error cause 1:

While the parentheses are the conditions in which the loop can be performed, the item < EPS is the limiting condition.
Correction Method:

If you want to loop, change the item < EPS to item >= EPS.

Error cause 2:

EPS defines a double type so the input should be%LF
Correction Method:

Change the%f into a%lf.

Error cause 3:

n defines an integral type, and item is a double type
Correction Method:

Change 1 to 1.0.
No errors were found after the compilation was run,

(ii) Learning Summary:
1. Statements while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
while (1) means: 1 is true (conditional), 0 is false (conditions are not set), while (1) is always true, if the internal does not jump out of the statement, it is a dead loop.
for (;;) Meaning: For after the parentheses, the content before the first semicolon is executed before the first loop, the content before the second semicolon is to be judged before each execution (if the value of the expression is true, then the loop body, if false, then jump out of the loop body), The contents after the second semicolon are executed after each execution of the loop body. There are no restrictions (the 2nd semicolon before) (the condition will inevitably become true, the loop will stop), so it will not stop. Another way to stop is to use break in the loop body to jump out of the loop body.

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.
① Cycles are known
to find the minimum value with a For statement, such as the 8th question in the PTA loop structure (1). The problem is that the input first gives a positive integer n in a row, followed by n integers, and outputs the smallest of n integers in a row in the format "min = Min". The number of cycles is known, input n integers to find the minimum value, so the number of cycles is n times. The
② loop is unknown, but the loop condition is clear
with whlie statements in the loop, such as in the PTA Loop structure (2) of the 7th question to find 250, the title is the input in a row gives not know how many absolute value does not exceed 1000 of the whole number, wherein there is at least one "250" guaranteed. The first occurrence of "250" in a row is the number that the opponent throws (counting starts from 1). The title guarantees that the number of outputs is within the integral type range. This is not known to loop several times, so with the while (1) infinite loop, when the input 250 loop stop, so add break statement to control. The
③ loop is unknown, and the loop condition is unknown when it enters the loop, and it needs to be explicitly
in the loop body with a Do while statement, such as with a Do while statement. For example, in the PTA Cycle structure (2) crawl the 5th worm, the topic is a worm 1 inches long, in a deep n-inch of the bottom of the well. 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). This program will have at least one loop
3. There are questions: Enter a batch of student scores, end with 1, and calculate the student's average score. The
requires the use of a for statement, a while statement, a Do While statement three loop statements are implemented, and you think which form is more appropriate?
For statement:

# include<stdio.h>int main (){    int m,i,n,sum;    double average;    sum = 0;    scanf("%d",&m);    for(i = 1;i<=m;i++)    {        scanf("%d",&n);        if(n == -1)        {            break;        }        else         {            sum = sum+n;        }       }             average = (double)sum/m;    printf("%.1f",average) ;    

While statement:

# include<stdio.h>int main (){    int m,sum,i;    double average;    sum = 0;    i = 0;    while(1)    {        scanf("%d",&m);        if(m == -1)        {            break;            }                else                 {                         sum = sum+m;                         I++;                 }                    }            average=sum/i;    printf("%.2f",average);     return 0;}

For while statement:

# include<stdio.h>int main(){    int m,sum,i;    double average;    sum = 0;    i = 0;    do{    scanf("%d",m);    sum = sum+m;    i++;}while(m!=-1);average = sum/i;printf("%f",average);return 0; }

Use the Do While statement because the program executes at least one time.
4. Run the following program, enter 1 to 10, what are the results? Why?
(1)

#include<stdio.h>int main(){    int n,s,i;    s = 0;    for(i = 1; i <= 10; i++)    {        scanf("%d",&n);             if(n % 2 == 0)        break;              s = s + n;      }printf("s = %d\n",s);return 0;}

Enter 1 to 10, result:

(2)

#include<stdio.h>int main(){    int n,s,i;    s = 0;    for(i = 1; i <= 10; i++){    scanf("%d",&n);         if(n % 2 == 0)        continue;           s = s + n;      }printf("s = %d\n",s);return 0;}

Enter 1 to 10, result:

The output results of the two programs are s=1,s=25, respectively.
The difference between the two programs is that the first one uses the break jump statement, and the second uses the continue statement. The continue statement ends this loop only, rather than terminating the execution of the entire loop. The break statement ends the entire loop and no longer determines the condition of the execution loop. In the first program, when input 1 o'clock (n%2==0) is established, it executes the break statement, jumps out of the loop body, and outputs S=1. In the second program, enter 1 to 10, when the IF statement is set to end the loop (not the entire loop), otherwise the loop is executed.

(iii) Experimental summary:
1. For a simple staggered sequence part of the given precision, and:
(1) Title: Calculated sequence section and 1-1/4 + 1/7-1/10 + ... The absolute value of the last item is not greater than the given precision EPs. The output part and the value s in the format of "sum = S" in a row are accurate to six digits after the decimal point.
(2) Flowchart:

(3) Source code:

# include <stdio.h># include<math.h>int main(void){int m,n;double eps,sum,item;item = 1;sum = 0;m = 1;n = 1;scanf("%lf",&eps);if(eps>=item){  sum = 1;}while(fabs(item)>eps){    item = (double)m/n;    sum = sum+item;    n = n+3;    m = -m;}printf("sum = %.6f",sum);return 0;}

(4) Experimental Analysis:
Issue 1:

Reason: No consideration when entering the first number is greater than or equal to item, at this point sum=1
Workaround:

Add an If statement before the loop.
(5) The PTA submission list:

2. Guess the number game:
(1) title:
Guess the game is to make the machine randomly produce a positive integer within 100, the user enters a number to guess, You need to write a program that automatically compares it to the randomly generated guesses and suggests that it is larger ("Too big"), or smaller ("Too small"), and that the equivalent is guessed. 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:

  #include <stdio.h>int main () {int i,n,a,n;    scanf ("%d", &i), scanf ("%d", &n), N = = 0;while (n<=n) {scanf ("%d", &a);    n=n+1;        if (a<0) {printf ("Game over\n");    Break            } if (I>a) {printf ("Too small\n");            } else if (i<a) {printf ("Too big\n");            } else {if (n==1) {printf ("bingo!\n");        Break            } else if (n>1&&n<=3) {printf ("Lucky you!\n");        Break            } else if (n>3&&n<=n) {printf ("good guess!\n");        Break            } else {printf ("Game over\n");        Break }}}} return 0;}  

(4) Experimental Analysis:
Error message:

Cause of Error:

Does not give an initial value to N, in the loop does not make n=n+1
Correction Method:

(5) PTA Submission list:

3. Find Odd and:
(1) Title:
The input gives a series of positive integers in a row, separated by a space. When a zero or negative integer is read, the input ends and the number is not processed. Outputs a sequence of odd integers in a row.
(2) Flowchart:

(3) Source code:

# include <stdio.h>int main(void){   int a,sum;   sum = 0;   scanf("%d",&a);   do   {if(a<0) {  break;  }else if(a%2!=0) {  sum = sum+a;  } scanf("%d",&a);  }while(a>0);  printf("%d",sum);  return 0;}

(4) Experimental Analysis:
The cyclic condition of the subject is that the a>0,while is followed by a cyclic condition, and the Whlie is appended to the parentheses in the a>0 loop.
(5) PTA Submission list:

(iv) Mutual evaluation of blogs:
(1) http://www.cnblogs.com/luyu0322-/p/7846404.html
(2) http://www.cnblogs.com/yiqiekaoziji/p/7858416.html
(3) http://www.cnblogs.com/karry-5201314/p/7846648.html

C Language Programming sixth time-cycle 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.