C Language Programming sixth time job

Source: Internet
Author: User

C Language Programming sixth assignment (i) 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:

#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:

Cause: Do While loop is incomplete

Correction: Add a semicolon on the last side of the while (Item < EPS)
Correct do and loop
do{

}

while ();

Error message 2:

Cause: The data type is wrong, the previously defined item is a double type, and item=1/n in the loop, so the output will lose precision because 1 and n are of type int.

Correction: the item = 1/n; 1 is changed to 1.0, or a forced transition is preceded by 1 plus (double), the first method is generally used

Error message 3:



Cause: The output value is not changed to take into account that the loop is not normal execution, to see the title of the last item is less than the absolute value of EPS, and then see the cycle condition is while (item < EPS);
But this is a Do While loop first executes the statement after do and then judge the loop condition of while, for the true continuation of the loop, false jump out of the loop. According to the requirements of the topic should be item<eps jump out of the loop

Correction: while (item < EPS) changed to while (Item>=eps)

Error message 4:

Reason: The program entered a dead loop can not jump out, in the format of input when the EPS is a float, and the definition of a double type this will lose precision.

Correction: Change the scanf ("%f", &eps),%f to%lf

Float is a single precision, the effective number is 6~7 double, the effective number is 15~16 but when they are output, there are 6 decimal places after the decimal point when the output is not necessary to change the%f to%.6LF
Here's an example:

#include<stdio.h>int main(){    float n;    n= 1/3.0;    printf("%f",n);}

Output results

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

The while statement first determines whether or not the loop is true only if the loop condition is met, so writing 1 in the while loop condition is equivalent to opening all the data portals, all in the loop, into an infinite loop, like eating a flashy mai at all and not stopping until you meet break;for (;;) Like while (1), the For loop is the order of execution for (expression 1; expression 2; expression 3), the expression 1 is executed, the condition 2 is true, the loop body statement is executed and the expression 3 is executed, and then the expression 2 is judged until the expression 2 executes the following statement for the false jump loop. and for (;;) Is the expression 2 has been the green light has been circulating, but also encountered a break stop. To ensure that the normal execution of the two loops in the loop body has a break, otherwise the loop can not stop, into the dead 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 done to illustrate

1, according to the number of cycles known, should first consider using a for loop similar to loop structure 1 of the 7-5 statistical characters that question according to the question required to enter 10 characters, the number of English letters, spaces or carriage returns, numeric characters and other characters, such problems will usually give a definite number of cycles.
2, the number of cycles is unknown, but the cycle conditions in the loop when it is clear to consider the while loop, such as the loop structure 2 of the 7-2 guessing the number of games, the loop condition is that the number of inputs than the specified small to continue the cycle
3, the number of cycles is unknown, and the cycle conditions in the loop when the unknown, need to be clear in the loop body, this should use the Do While loop, does while loop and while loop is the biggest difference is do while will be executed once and then the condition judgment, for the true continuation loop for the false exit loop. Like the 7-3 in the loop structure 2 is odd and because the title requirement is to enter a positive integer

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

Since the first input does not know the positive and negative, the judgment condition is put on the back, which shows the advantages of doing 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

#include<stdio.h>#include<stdlib.h>int main(){int sum,n,i;double average;sum=0;for(i=0;1;){    scanf("%d",&n);        if(n==-1)    {        printf("average=0.00");        exit(0);    }        else    {        sum+=n;         i++;            }    }average=(double)sum/i;printf("%.2lf",average);}

2. While statement

#include<stdio.h>#include<stdlib.h>int main(){int sum,n,i;double average;sum=0;i=0;while(1){    scanf("%d",&n);         if(n==-1)    {        printf("average=0.00");        exit(0);    }        else    {        sum+=n;         i++;            }    }average=(double)sum/i;printf("%.2lf",average);}

3. Do While statement

#include<stdio.h>#include<stdlib.h>int main(){int sum,n,i;double average;sum=0;i=0;do{    scanf("%d",&n);         if(n==-1)            {                printf("average=0.00");        exit(0);            }               else    {        sum+=n;         i++;            }    }while(1);average=(double)sum/i;printf("%.2lf",average);}

The use of the For statement and the Infinite loop of the while, the Do, and so on are similar to the circular process, which is just a slight change and the three loops can be used to deal with the same problem, and can be substituted for each other. For this question because I do not know the number of students, so the infinite loop does while is better because the most important feature of the Do While loop is that it is sure to loop once.
While and Do-while loops, the loop body should include statements that tend to end the loop. The For statement features the strongest. When using the while and do-while loops, the operation of the loop variable initialization should be done before the while and Do-while statements, while the for statement can implement the initialization of the loop variable in expression 1.

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

Operation Result:

Cause: The first run n=1 does not meet the conditions in the IF statement, the execution s=0+1,s=1,i=i+1,i=2<=10 is true read n=2 conforms to the IF condition n%2==0 so the program ends, output s=1;

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

Operation Result:

Reason: This code and above is a break and continue difference, but the operation is very different, the second code is to pick out 1 to 10 odd and sum, the running process is n=1,n%2! =0 does not conform to the execution condition of the IF statement, executes the CONTINUE statement s=s+1;i=i+1=2,n=2,2%2=0 the conditions conforming to the IF statement, ends the loop, and proceeds to i=2+1=3,n=3,n=4, 5, 6 ...

(iii) Experimental Summary (1) 7-1 The simple staggered sequence part of the given precision and

Requirements: The subject required to write a program, the calculation 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 flag;double eps,num,sum,S,n;scanf("%lf",&eps);flag=1;num=1;S=0;do{    S=S+flag*1/num;    n=fabs(flag*1/num);    flag=-1*flag;    num=num+3;    sum=S;}while(n>eps);       printf("sum = %.6lf",sum);}
(4) Experimental analysis

Problem:

Reason: Originally thought the accuracy loss, the data type is not correct, after checking to determine no problem, later found that the cyclic condition error

Correct: Change less than and greater than in while (N<eps)

(5) PTA Submission List

(1) 7-2 guess number game

Requirements: Guess the number game is to make a machine to randomly produce a positive integer less than 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 n,guess,right,ci;scanf("%d %d",&right,&n);ci=0;while(ci<=n){    ci+=1;    scanf("%d",&guess);    if(ci>n)    {                printf("Game Over");                break;      }    if(guess<0)    {        printf("Game Over");        break;    }    else    {        if(guess>right)        {            printf("Too big\n");        }        else if(guess<right)        {            printf("Too small\n");        }        else        {                        if(ci==1)            {                printf("Bingo!");                break;             }              else if(ci<=3&&ci>1)             {                printf("Lucky You!");                break;             }            else if(ci>3&&ci<=n)            {                    printf("Good Guess!");                    break;             }                     }              }}}
(4) Experimental analysis

Problem:

Reason:

Correct:

(5) PTA Submission List

(1) Find odd and

Requirements: The subject requires the calculation of a given series of positive integers in the odd and

(2) Flowchart

(3) Source code
#include<stdio.h>int main(){int n,sum;sum=0;do{    scanf("%d",&n);    if(n<=0)    {    break;      }    else{        if(n%2!=0)    {    sum=sum+n;      }               }    }while(n>0);printf("%d",sum); }
(4) Experimental analysis

Problem: None But this problem can also be written in a while loop but relatively less than the do and simple
While loop

#include<stdio.h>int main(){int n,sum;sum=0;scanf("%d",&n);while(n>0){    scanf("%d",&n);    if(n<=0)    {    break;      }    else{        if(n%2!=0)    {    sum=sum+n;      }               }    
(5) PTA Submission List

(iv) Mutual evaluation of blogs

Guo Zanxu: http://www.cnblogs.com/1234569ss/p/7824423.html
Zhaoxiaohui: http://www.cnblogs.com/2205747462x/
?? : http://www.cnblogs.com/myg123/p/7841714.html

C Language Programming sixth time job

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.