C Language Programming sixth time job

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

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

Error message 2:

Error cause 2:n is an integer variable, so the output is 0 or 1
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
Corrected to match the output sample
Give the corrected 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;    }     

(ii) Learning Summary
1. Statements 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 the parentheses is 1 o'clock, the expression is true, the loop statement is executed, when the number in parentheses is 0 o'clock, it is false and jumps out of the loop.
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) 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.
When the number of cycles is known, with a For statement loop, the 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 loop body is executed at least once, the Do...while statement is executed and then judged.
For statement: With PTA cycle structure (i) the first is titled example, the title is: Calculated sequence 1 + 1/3 + 1/5 + ... The sum of the first n items.
This problem with for more convenient, the problem clearly gives the number of cycles, give the number of initial value, clear the cycle conditions and step, you can get the answer.
While statement: With the PTA cycle Structure (ii) the seventh is titled: The other party 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 (ii) the sixth is titled, the title is: the number falling into the trap.
The requirement of this problem is that when the output of this time output is the same as the previous output, it needs to be cycled once and then judged until it is 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 statement program:

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

(2) While statement program:

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

(3) Do...while statement procedure:

#include <stdio.h>int main(void){    int grade = 0,i = 0,sum = 0;    double average = 0.0;    do    {        scanf("%d",&grade);        sum = sum + grade;        i++;    }    while(grade != -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 the while statement is very similar to the Do...while statement, where the condition of the loop is judged to be different. But the while statement is relatively concise and does not need to be repeated many times. The while statement in the topic input-1, will be 1 also counted in, need special explanation. In conclusion, I think the For statement 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;}

The results are as follows:

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

The results are as follows:

The only difference between the two programs is that the first one uses break to control the loop statement, and the second uses continue to control the loop statement. Break control is to jump out of the loop body, the execution of the statement outside the loop, and the first program in the input 2 o'clock has jumped out of the loop, so the answer is S=1;continue control is to end this cycle, jump out of this non-executed loop statement, and then execute the next loop statement until the end. This statement asks for an odd number of 1-10, so the answer is s=25.

(iii) Experimental summary
This experiment summarizes 1, 2 and 3 questions in the cycle structure (2).
First question: The 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

(2) Flowchart:


(3) Source code:

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

(4) Experimental Analysis:
Issue 1: The final output of the result is 0.000000
Reason 1:item is defined as double type, flag*1 is 0
Solution 1: Change 1 to 1.0

Issue 2: This program is wrong with the while statement
Cause 2: This program must be executed once to cycle
Workaround 2: Change the while statement to the Do...while statement

(5) PTA Submission List

Second question: Guess the number game
(1) Title: To make a game console randomly generated a positive integer within 100, the user input 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 num,n,i,a;    i = 1;    scanf ("%d%d", &num,&n);        do {scanf ("%d", &a);            if (a < 0) {printf ("Game over");        Break } else if (a = = num && i <= N) {if (i = = 1) {printf ("bingo!"                );            Break                } else if (I <= 3) {printf ("Lucky you!");            Break                } else if (i > 3 && i <= N) {printf ("good guess!");            Break            }} else if (i > N) {printf ("Game over");        Break        } else if (a > num) {printf ("Too big\n");          } else if (a < num) {printf ("Too small\n");    } i++;    } while (num! = a); return 0;}  

(4) Experimental Analysis:
Issue 1: When the number entered is greater than the number of times, end the program, output results
Cause 1: No end when the number entered is equal to the number of times
Solution 1: Change I>n to I>=n

Issue 2: Can be output when negative numbers are entered
Cause 2: No break after the IF statement
Workaround 2: Add Break on line 13th
(5) PTA Submission List

Question three: find Odd and
(1) Title: The problem requires the calculation of a given series of positive integers in the odd number and.

(2) Flowchart:

(3) Source code:

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

(4) Experimental Analysis:
Issue 1: The while statement used at the beginning, but not very well understood
Reason 1: The do...while statement should be used more
Solution 1: Change the loop to Do...while Infinite loop

(5) PTA Submission List

(iv) Mutual evaluation of blogs
Screen: The wrong steps to clear, and the experimental analysis concise clear, very detailed, but also attached to the wrong completion of the source code, which is worth learning
(http://www.cnblogs.com/yjy751522356/p/7838296.html)

Hougonda: Wrong problem I feel that you did not change out, is to keep six decimal places there, but the experimental summary and learning summary you completed very well, the idea is clear, need to learn from you.
(http://www.cnblogs.com/HGD980425/p/7837319.html)

Lucky you: The correct problem is clear, the process is concise and clear, but in the experimental analysis of the source code part, the last is to use the markdown format, the specific how to operate, the teacher has said in the group, the group files have, hope to correct.
(http://www.cnblogs.com/LYFRRS/p/7854183.html)

Wind away from you in: This blog Park homework completed quickly, the idea is also very clear, the format is very standard.
(http://www.cnblogs.com/GX201701-/p/7828562.html)

C Language Programming sixth time job

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.