C Language Programming sixth time-cycle structure (2)

Source: Internet
Author: User

(a) Correct the wrong question
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);

(ii) Study summary

1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
A: All are infinite loops, and if statements are followed by a break in the loop statement, they can jump out normally and execute

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.

A: (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

① is written with a for loop structure. For example:
Cycle structure (1) 7-2 statistical average student scores and passing numbers, 7-3 odd and even split 7-8 find the minimum value, give the source program of 7-3:

#include <stdio.h>int main(void){int N,i,n,a = 0,b = 0;scanf("%d",&n);for(i = 0;i < n;i++){   scanf("%d",&N);    if(N % 2 == 0) {    a++; }   else {    b++; }}  printf("%d %d",b,a);  return 0; }

② can be written using the loop structure of while and do while. For example: Loop structure (2) in 7-5 crawl worms, loop structure (1) 7-4 in the end is not too fat to give 7-5 of the source program:

#include <stdio.h>int main(void){    int N,U,D,head=1,time=0;    scanf("%d %d %d",&N,&U,&D);    while(1)     {        head+=U;        time++;        if(head>N)break;            head-=D;            time++;    }    printf("%d",time);    return 0;}

The ③ can be written using the loop structure of do while. For example: the loop structure (2) in 7-1 for the given precision of the simple staggered sequence part and 7-3 for odd and, given 7-3 of the source program:

#include <stdio.h>int main(){    int n = 0, sum = 0;    do    {        scanf("%d", &n);        if (n % 2 == 1)         sum += n;    } while (n > 0);    printf("%d\n", sum);    return 0;    }

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?

Answer: While better. The end condition is written directly in parentheses, and can be ended in one.

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

(2)

A: The two source programs look basically the same, but the difference is that the first one is break, and the second one is continue. The result is an output of 1, an output of 25, because the first one executes a s=s+n, and S is 0,n 1, when the first input 1 is directly out of the loop s=s+n, so the final output 1, and the second flowchart with the continue two control cycle conditions, He does not jump out of the judgment after the next, and will be divisible by 2 of the number of filtering out, so the final output is 25.

(iii) Experimental summary

(1) Title

7-1 the simple staggered sequence part of the given precision and

(2) Flowchart

(3) Source code

#include <stdio.h>  #include <math.h>  int main(){      int i=1,k=1;      double sum=0,t,n;      scanf("%lf",&n);      do{          t=k*1.0/(3*i-2);              sum+=t;          k=-k;          i++;      }while(fabs(t)>n);      printf("sum = %f\n", sum);      return 0;  }

(4) Experimental analysis
Question 1: Wrong answer, input 0.02 often wrong

Cause: Cyclic judging condition problem

Workaround: Fabs (t) >n, not >=

(5) PTA Submission List

(1) Title:

7-2 guessing number games

(2) Flowchart

(3) Source code

#include <stdio.h>int main(void) {    int number, n;    int count = 0;    int value;    scanf("%d%d", &number, &n);    while(count <= n) {        scanf("%d", &value);        if(value < 0) {            printf("Game Over\n");            break;        }        ++count;        if(value > number)            printf("Too big\n");        else if(value < number)            printf("Too small\n");        else {            if(count == 1)                printf("Bingo!\n");            else if(count <= 3)                printf("Lucky You!\n");            else if(count <= n)                printf("Good Guess!\n");            break;        }    }    if(count > n)        printf("Game Over\n");      return 0;}

(4) Experimental analysis

Error message: The initial input generates a random number, using the Math function rand (), but the result output is incorrect.

Reason: Understand the test instructions error, do not use the function can be, directly enter the two numbers and then the loop can be.

Note: The loop end can be done with break. In the subject need to define a quantity flag=0, if the subsequent with the definition of equality before the judgment is true, if not equal to judge false, and in the condition of break to jump out of the next operation.

(5) PTA Submission List

(1) Title

7-3 Find Odd and

(2) Flowchart

(3) Source code

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

(4) Experimental analysis
Issue 1: Wrong answer

Reason: If the judgment condition habitually writes shu*1.0/2! =0

Workaround: Change to Shu%2!=0

(5) PTA Submission List

(iv) Mutual evaluation of blogs
(1) http://www.cnblogs.com/bilililili/p/7842022.html
(2) http://www.cnblogs.com/wsd-lian/p/7860213.html
(3) http://www.cnblogs.com/zxh980818/p/7842242.html

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