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)

#include<stdio.h>int main(){    int flag,n;    double eps,item,s;    printf("Input eps: ");    scanf("%f",&eps);    flag = 1;    s = 0;    n = 1;    do{        item = 1/ n;        s = s + flag * item;          flag = -flag;        n = n + 3;    }while( item < eps)    printf( "s = %f\n",s);    return 0;}

Compile the source program, use the method of modifying the first error and recompile each time, log the error message of each error, parse the cause of the error, and give the correct statement.
Error message 1:
Error reason: There is no semicolon at the end of the while statement in the Do...while statement in line 17th of the source program.
Correction method: Add a semicolon to the last of the while statement in the Do...while statement in line 17th of the source program.
Correct statement:

while( item < eps);

Trial run, source program:

Operation Result:
is different from the expected result s = 0.835699.

Error message 2:
Error reason: The%f in the input statement on line 8th of the source program should be%LF.
Correction method: Change the input statement in line 8th of the source program to%f to%LF.
Correct statement:

scanf("%lf",&eps);

Error message 3:
Cause of Error: 1 of the assignment statement in line 13th of the source program should be 1.0. Because item is floating-point data.
Correction method: Change the value of the assignment statement in line 13th of the source program to 1 1.0.
Correct statement:

item = 1.0/ n;

Error message 4:
Error reason: The < in the Do...while statement in line 17th of the source program should be >=.
Correction method: Change the < in the Do...while statement in line 17th of the source program to >=.
Correct statement:

while( item >= eps);

Trial run, source program:

Operation Result:
Same as expected result s = 0.835699.

(ii) Study Summary 1. Statements while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?

For:
while (1) represents an infinite loop,
for (;;) Also represents an infinite loop.

while (1):
  while(条件){    代码}

When the condition is true, the code inside the curly braces is executed, because 1 is true, so the loop executes all the time, and you can jump out of the loop with a break statement inside the loop.

for (;;) :

In the for () statement, the second clause is a loop condition, and if nothing is written, it is expressed as a constant, so the loop executes all the time, and the break statement can be used inside the loop to jump out of the loop.

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

It is better to use a For loop statement, such as: 1,2,3,4,5,6,7,8 in the loop structure (1). The title of these questions requires a number to be entered, which is the number of cycles. So it is better to use the FOR Loop statement when the number of cycles is known.

(2) The number of cycles is unknown, but the cyclic conditions are clear when entering the cycle

The use of the while loop statement is better, such as: 1,2,3,5 in the loop structure (2). These questions do not know the number of cycles through the topic, but can be known through the topic into the cycle of conditions. So when the loop number is unknown, but the loop condition is clear when entering the loop, it is better to use while loop to implement.

(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 use of Do...while Loop statement is better, such as: Loop structure (2) in the 4,6,7. These problems can not be known through the topic cycle times, and can not be known through the topic into the cycle of conditions. But the condition of the loop can be defined in the loop body. So when the number of cycles is unknown, and the cyclic conditions are unknown when entering the loop, it is better to use the Do...while loop statement when the loop body is clear.

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:
# include <stdio.h>int main(){int i,x = 0,sum = 0;double p;for(i = 0 ;x >= 0 ; ){    scanf("%d",&x);    if(x >= 0)    {        sum = sum+x;        i++;    }}p = sum/(double)i;printf("%f",p);return 0;}
While statement:
# include <stdio.h>int main(){int i,x = 0,sum = 0;double p;while(x >= 0){    scanf("%d",&x);    if(x >= 0)    {        sum = sum+x;        i++;    }}p = sum/(double)i;printf("%f",p);return 0;}
Do While statement:
# include <stdio.h>int main(){int i,x = 0,sum = 0;double p;do{    scanf("%d",&x);    if(x >= 0)    {        sum = sum+x;        i++;    }} while(x >= 0);p = sum/(double)i;printf("%f",p);return 0;}

I think the form of do is more appropriate.

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;}
(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;}

(1) The result is

(2) The result is

Because: the function of break is to jump out of the loop; the function of continue is to end this cycle.

(iii) Experimental Summary 1 The simple staggered sequence part and (1) of the given precision are obtained.

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
# include <stdio.h># include <math.h>int main(){int i = 1;double sum = 0.0,eps = 0.0;scanf("%lf",&eps);while(fabs(1.0/i) > eps){    sum = sum+1.0/i;    i = pow(-1,i)*(fabs(i)+3);}sum = sum+1.0/i;printf("sum = %.6f",sum);return 0;}
(4) Experimental analysis

No problem

(5) PTA Submission List

2 Guessing number games (1) topic

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
#include<stdio.h>int main(){  int m,n;  scanf("%d%d",&m,&n);  int num;  int s=0;  while(scanf("%d",&num) > 0)  {    s=s+1;    if(s>n||num<0)    {      printf("Game Over\n");            break;    }    else    {      if(num>m)printf("Too big\n");      else if(num<m)printf("Too small\n");      else if(num==m&&s==1)      {         printf("Bingo!\n");                break;      }      else if(num==m&&s<=3)      {        printf("Lucky You!\n");                break;      }      else       {        printf("Good Guess!\n");                break;      }    }  }  return 0;}
(4) Experimental analysis

Problem:
Reason: There is no consideration for a direct exit from this situation.
Solution: Add consideration to the situation of direct exit.

(5) PTA Submission List

3 Find odd and (1) topics

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

(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

Question 1:
Reason: Using the statement error, do not apply the while statement, apply the Do...while statement.
Workaround: Replace the while statement with the Do...while statement.
Question 2:
Reason: This condition is not considered as an empty set.
Solution: Add consideration to the case of the empty set.

(5) PTA Submission List

(iv) Mutual evaluation of blogs 1. Guo Yulin: http://www.cnblogs.com/HBQ521/p/7826030.html

Your blog overall looks not neat, the same part of the font is different, resulting in a very ugly, other good.

2. Xu Tian laugh! : http://www.cnblogs.com/snxtx/p/7824427.html

The format of your blog looks beautiful and tidy. The summary part is also very full, a little flaw, is your first part of the 4 "error message" format is not the same.

3. Zhaoxiaohui: http://www.cnblogs.com/2205747462x/p/7851029.html

Your blog looks neat. If you change the source program in the third part into a direct insert form will be more beautiful.

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.