(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;}
Error message 1:
Error reason 1:do...while statement while need to be appended with a semicolon
Correction Method 1: Add a semicolon after the while statement, such as the do{statement} while (expression);
Error message 2:
The error cause 2:n is an integer variable, so the output is 0 or 1 is an integral type
Correction Method 2: Change 1 to 1.0, cast
Error message 3:
Error reason 3:while the statement in which the loop should begin, and the statement given in the title as the end of the loop
Correction Method 3: Change Item < EPS to item >= EPS
Error message 4:
Error cause 4: The EPS defined in the question is in double format
Correction Method 4: Change%f to%LF
Correct answer, match the sample
Correct code
#include<stdio.h>int main(){ int flag,n; double eps,item,s; printf("Input eps: "); scanf("%lf",&eps); flag = 1; s = 0; n = 1; do{ item = 1.0 / n; s = s + flag * item; flag = -flag; n = n + 3; }while( item >= eps); printf( "s = %f\n",s); return 0;} (二)学习总结
1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
A: while (1) represents an infinite loop, when the number in parentheses is 1 o'clock, the expression is true, the loop statement
for (;;) Indicates the number of known cycles with a for statement, Expression 1 is the initial value, expression 2 is to determine the loop condition, Expression 3 is the step size. First executes expression 1, then the expression 2, then executes the loop body, then executes the expression 3, then the expression 2, then executes the loop body, and then goes back to the expression 3, until the expression 2 is not established, jumps out of the loop. So for (;;) Also for infinite loops. If there is no stop sign in the program, the execution continues, and a break is added to the program to stop 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) 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, with a For statement loop, first judgment after execution, when the number of cycles is unknown, but given the conditions at the end of the loop condition, with a while statement loop, but also the first judgment after execution, when the number of cycles is unknown, and the loop condition is unknown in the loop, it needs to be clear When the loop body is executed at least once, the Do...while statement is used to cycle, and then the judgment is executed.
For statement: With PTA cycle structure (1) The 2nd is titled: Statistics on average students ' scores and passing numbers
This problem with for more convenient, the problem clearly gives the number of cycles N, the number of times assigned to the initial value, clear the cycle conditions and steps, you can get the answer.
While statement: with PTA cycle structure (2) The 7th is titled: The other person does not want to talk to you, and throws you a bunch of numbers ... And you have to find the "250" on this big moving number from this string of numbers.
This problem is typical with a while statement, do not know how many times the loop, but know the condition of the end of the loop, when the input number first encountered 250, the loop ends.
Do...while statement: With the PTA cycle Structure (2) The sixth is titled: Odd and
The requirement is that when not output 0 and negative numbers, so the need to cycle first, then judge, until the output. So it is appropriate to use Do...while.
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?
(1) for
#include <stdio.h>int main(){int x = 0,sum = 0,i = 0;double average = 0.0;for(;;){ scanf("%d",&x); if(x != -1) { sum + = x ; i++; } else { break; }} average = sum/i;printf("%f",average);return 0;}
(2) while
#include <stdio.h>int main(){int x = 0,sum = 0,i=1;double average = 0.0;while(grade != -1){ scanf("%d",&x); sum += x;
(3) Do...while statement procedure:
#include <stdio.h>int main(){int x = 0,i = 0,sum = 0;double average = 0.0;do{ scanf("%d",&x); sum + = x; i++;}while(x != -1);average = (sum+1)/(double)(i-1);printf("%f",average);return 0;}
I think the For Statement loop is simple and easy to understand, and is more intuitive and handy to use.
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;}
Results:
(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;}
Results:
Reason: (1) The break statement is the end of the entire loop process, no longer determine whether the condition of the execution loop is established (2) The Continue statement only ends the loop, not the execution of the entire loop.
(iii) Experimental summary
This experiment summarizes 1, 2 and 3 questions in the cycle structure (2).
1. The simple staggered sequence part of the given precision and
(1) Title
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(void){int i=1;double eps;double j=1.0,k,s=0.0;scanf("%lf",&eps);do{ k=i/j; if (fabs(k)<eps) {s=1.0;break;} i=-i; j+=3;
(4) Experimental analysis
Issue 1: None
(5) PTA Submission List
2. Guess the number game
(1) Title
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)!=EOF){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
Reason: No problem is analyzed clearly
Solution: Ask the classmate for advice
(5) PTA Submission List
3. Find Odd and
(1) Title
The subject requires the calculation of odd numbers in a given series of positive integers and
(2) Flowchart
(3) Source code
#include <stdio.h>int main(){int n,sum = 0;do{ scanf("%d", &n); if (n % 2 == 1) sum += n;} while (n > 0);printf("%d\n", sum);return 0;}
(4) Experimental analysis
Issue 1: None
(5) PTA Submission List
(iv) Mutual evaluation of blogs
C Language Programming sixth time job