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; scanf("%f",&eps);    printf("Input 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 1:
Cause 1: You want to add a semicolon behind the while, so the program has an error
Correction 1: Add a semicolon after the while, but if the while statement does not add a semicolon, plus an error occurs
There is no problem compiling the program, but there is a problem with running the result
Error 2:
:
Cause 2:item is a double type, but it is an integral type in the expression, there is a problem with the format
Correction 2: Will item = 1/n; Change to item = 1.0/n;
Continue editing, the output is still wrong
Error 3:

Cause 3:
Parse question, should be error in while
Correction 3: Change it to item > EPS
Error 4:
The output does not match the answer
Reason 4: The topic definition EPS is a double type, while in the Code scanf ("%f", &eps);
Correction 4: Change%f to%LF
Running results are correct
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;}

(ii) Study summary

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, while the prototype of a while statement is a while (expression) statement, when the expression is non-0 o'clock, the execution of the nested statement in the while statement, while (1) indicates that forever is true, the loop continues indefinitely until it encounters
Break is stopped. The content before the first semicolon in the for (::) is executed before the first loop, and the content before the second semicolon is judged before each execution, where there are no restrictions, so the loop will not stop until a break is encountered. As a general rule, you can always do this without hitting a break.

2. In general, when designing a looping structure, it is possible to use the for, while, do, and three statements, and three statements can be converted to each other, but in some specific cases, we should prefer some kind of statement
To quickly implement a circular 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.
A: The loop condition is known to use a For statement, you can determine the number of times, the number of cycles is unknown, but the loop condition is clear when entering the loop, using the while statement,
The number of times is unknown, and the loop condition is unknown at the time of entering the loop, it needs to be explicitly used in the loop body, the statement executes once and then judges into the loop
Example: (1) Number of cycles known: Use for statement, such as our PTA work cycle structure (1) in 7-7, because the number of cycles known, so with a for loop
(2) The number of cycles is unknown, but the loop condition is clear when entering the loop: using the while statement, such as 7-1 questions in our PTA work cycle structure (2)
(3) The number of cycles is unknown, and the cycle conditions in the loop is unknown, need to be clear in the loop body: using Do...while statements, such as our PTA work cycle structure (2) 7-7 questions

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 sum,grade ,i=0;double ave=0;for(;;){scanf("%d",&grade);if(grade!=-1){    sum=sum+grade;    i++;            }       else if(grade==-1){    break;}       }ave=sum/i;printf("%f",ave);return 0;    

While statement

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

Do While statement

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

I think this problem with the while statement is a bit better, because this problem is the number of cycles unknown, so with a for loop is not very good, if you have to do when you have to judge, and then to cycle, but also more trouble,
So use while a bit better.

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

Results:


In the program (1) if the number of inputs is 2 in addition to the remainder of 0, then jump out of the loop, when the input of 2 is the remainder of 0, then jump directly out of the loop, so and 1, but in (2) if the number of inputs by 2 in addition to the remainder is 0,
Then continue with the For loop, then do not add this number to the loop, the calculation is 1-10 odd and so and 25, which is the difference between break and continue.

(iii) Experimental summary

One: (1) Topic
Simple staggered sequence part and (10 points) for a given precision
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.
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.
(2) Flowchart

(3) Source code

#include<stdio.h>#include<math.h>int main(){   int a,b;double sum=0,eps,x;   scanf("%lf", &eps); x=1.0;a=1;b=1; if(eps>=1) sum=1.0;    else   {while(fabs(x)>eps) {x=b*1.0/a;sum=sum+x;b=-b;a=a+3;     }         }    printf("sum = %0.6lf\n",sum);  

(4) Experimental analysis
Write with For loop is also more skilled. But you don't know what to do with while. There are also types of conversion applications not very skilled, do not know when the conversion must be forced, sometimes can not

(5) PTA Submission

Two: (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.
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.
(2) Flowchart

(3) Source code

#include <stdio.h>#include <stdlib.h>int main(){int x,y,m,n=0;scanf("%d %d",&y,&m);while(1){    if(m<=n)    {        printf("Game Over\n");        exit(0);    }    scanf("%d",&x);    if(x<0)    {           printf("Game Over\n");        exit(0);    }    if(m>n)    {        if(x<y)        {        printf("Too small\n");        }        if(x>y)        {        printf("Too big\n");        }        if(x==y)        {            if(n<1)            {                printf("Bingo!\n");                break;            }            else if(n<3)            {                printf("Lucky You!\n");                break;            }            else if(n>=3&&n<m)            {                printf("Good Guess!\n");                break;            }        }    }    n++;}return 0;}

(4) Experimental analysis
This problem write more trouble, first need to read the topic, according to the level of writing down a little bit, write the time is more troublesome, write when those else if and if the relationship is always wrong

(5) PTA Submission


Three: (1) Topic
The subject requires the calculation of the odd number of a given series of positive integers.
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.
(2) Flowchart

(3) Source code

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

(4) Experimental analysis
The problem is relatively simple, there is no big question

(5) PTA Submission

Four: Blog Mutual evaluation

A cat lei:? http://www.cnblogs.com/8426224ll/p/7847221.html
Luckyyou:http://www.cnblogs.com/lyfrrs/p/7854183.html
Blue Sky: http://www.cnblogs.com/hfh0420/p/7857846.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.