(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:
Error reason: There is no semicolon behind while.
Correction method: Add a semicolon after the parentheses after the while.
Error message:
Error Reason: Item top one is double type, but 1/n is type int, format is not correct
Correction method: Change the item's calculation to 1 in 1.0.
Error message:
Error reason: The condition of the Do....while statement loop is written as the condition of the loop end.
Correction method: Change while (item < EPS) to while (item >=eps).
Error message:
Error Reason: EPS is defined as double format in the title
Correction method: Change the%f to%LF, and change the%f in the output to%.6f to ensure the output is six decimal places.
(ii) Study summary
1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
For:
While the back parenthesis is a loop condition, that is, when the loop condition is true loop execution, then while (1) is, "1" is represented as real, so this is an infinite loop. for (;;) The three expressions in the post brackets are: one for the value of the judgment, only one time, two judging conditions, three variables of the change situation. When three parentheses are empty, the second is an infinite loop.
In order to ensure the normal operation of the program should be added to the judgment statement, and after adding a break. That is, when the judgment statement jumps out of this loop for real.
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) When the number of cycles is known, it is better to use a for statement, for example: 7-1 to find one of the odd points of the sequence of the first N and.
2) When the number of cycles is unknown, but given the conditional control, with a while statement is better, first judge after execution, for example: 7-2 guess number game.
3) When the number of cycles is unknown, and the cycle conditions in the loop into the unknown, need to be clear in the loop body, with do-while better, first cycle after the judgment, for example: 7-6 fall into the trap number.
3. The following questions: Enter a batch of student scores, ending with 1 to calculate the student's average score.
Requirements are implemented with a for statement, a while statement, a do and an infinite loop of four loop statements, and what do you think is the more appropriate form?
①for statement:
#include<stdio.h>int main(){ int n,i=0,sum=0; double average; for(;;) { scanf("%d",&n); if(n!=-1) { sum=sum+n; i++; } else { break; } } average=(double)sum/i; printf("%f",average); return 0;}
②while statement:
#include<stdio.h>int main(void){int grade=0,sum=0,i=0;double average=0.0;i=0;while(grade!=-1) { scanf("%d",&grade); sum = sum + grade; i++; }average = (sum+1) / (double)(i-1);printf("%f",average);return 0;}
③do While statement:
#include<stdio.h>int main(void){int grade=0,sum=0,i=0;double average=0.0;i=0;do{ scanf("%d",&grade); sum = sum + grade; i++; }while(grade!=-1) ;average = (sum+1) / (double)(i-1);printf("%f",average);return 0;}
④ Infinite Loop statement:
#include<stdio.h>int main(void){int grade=0,sum=0,i=0;double average=0.0;while(1){scanf("%d",&grade);if(grade!=-1){sum = sum +grade;i++;}else if(grade==-1){break;}}average = sum / i;printf("%f",average);return 0;}
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:
The only difference between the two programs is the break loop control statement used by the first statement, and the second statement uses the Continue loop control statement, the difference between the two loop control statements is that break is the end loop and jumps out of the loop statement, and then executes the statement outside the loop, This statement when the input 2 is to jump out of the loop, and continue is the end of the cycle, out of the loop to continue the next cycle, until this normal end, the statement is 1-10 odd and the same, so the two programs answer different.
(iii) Experimental summary
1. The simple staggered sequence part and (10 points) of the given precision are obtained.
(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.
Note: The notation of the cyclic condition
Input format:
The input gives a positive real number EPS in a row.
Output format:
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. The title guarantees that the calculation result does not exceed the double precision range.
Input Sample 1:
4E-2
Output Example 1:
sum = 0.854457
Input Sample 2:
0.02
Output Example 2:
sum = 0.826310
(2) Flowchart
(3) Source program
#include<stdio.h>#include<math.h>int main(){ int b=1,y=-1; double sum=0,eps; scanf("%lf",&eps); while(fabs(y*1.0/b)>=eps) { y=-y; sum=sum+1.0/b*y; b+=3; } printf("sum = %.6f",sum); return 0;}
(4) Experimental analysis
Question 1: This problem cannot be used while
Cause: This program must be cycled at least once
Workaround: Use the Do While statement
(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.
Input format:
In the first line, two positive integers of no more than 100 are given, the random number generated by the game console, and the maximum number of guesses N. The last line gives a user input until a negative number appears.
Output format:
Output each time you guess the corresponding result in a row until the output guesses the right result or "Game over" ends.
Input Sample:
58 4
70
50
56
58
60
-2
Sample output:
Too Big
Too Small
Too Small
Good guess!
(2) Flowchart
(3) Source code
#include<stdio.h>int main(){int a = 0,N = 0,i = 0,b = 0;scanf("%d%d",&a,&N);for(i = 1;i <= N;i++){scanf("%d",&b);if(b < 0 ){printf("Game Over");break;}else if(b > a){printf("Too big\n");}else if(b < a){printf("Too small\n");}else if(a == b){if(i == 1){printf("Bingo!");break;} else if(i <= 3 && i > 1){printf("Lucky You!");break;}else if(i > 3){printf("Good Guess!");break;}break;}if(i >= N){printf("Game Over");break;}}return 0;}
(4) Experimental analysis
When you count the number more than the number of times to end the program
Cause: When it's equal, it's not over.
Workaround: Change I<n to I<=n
(5) PTA Submission List
3. Find Odd and
(1) Title
The subject requires the calculation of the odd number of a given series of positive integers.
Note: The first value is a negative case. Sum initial value
Input format:
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.
Output format:
Outputs an odd number of integers in a single line of a positive integer sequence.
Input Sample:
8 7 4 3 70 5 6 101-1
Sample output:
116
(2) Flowchart
(3) Source code
#include<stdio.h>int main(){ int sum=0,n; scanf("%d",&n); while(n>0) { if(n%2==0) { sum=sum; } else { sum+=n; } scanf("%d",&n); } printf("%d",sum); return 0;}
(4) Experimental Analysis:
There is nothing wrong with the subject, be careful not to read the last 0 or negative number.
(5) PTA Submission List
(iv) Mutual evaluation of blogs
1, Zhuyuanzhang; http://www.cnblogs.com/moying456/p/7859207.html
2, http://www.cnblogs.com/wangqi1998/p/7846847.html
3, http://www.cnblogs.com/shaosiming/p/7854102.html
C Language Programming sixth time job