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: A semicolon is missing from the while on line 17th.
Correction method: Add a semicolon after the while in line 17th.

Input sample run:

Different from the results

Error message 2:

Error reason: The%f in the input statement for line 8th should be%lf.
Correction method: Change the input statement in line 8th to%f to%LF.

Error message 3:

Cause of Error: 1 of the assignment statement in line 13th should be 1.0.
Correction Method: Change the assignment statement in line 13th to 1 in 1.0.

Error message 4:

Error reason: The < in the while statement in line 17th of the Do...while statement should be >=.
Correction method: The < in the Do...while statement in line 17th is changed to >=.

Re-enter the sample:

Same as the result

(ii) Study summary
1. Statement while (1) and for (;;) What is the meaning? , how to ensure that this cycle can be performed properly?
All represent an infinite loop.
While statement: 1 is true, the parentheses are the loop execution condition, so the infinite loop is represented.
For statement: The second condition is not written, which means that the constant is true. Infinite loops.

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) Number of cycles known
Takes a For loop statement. Columns such as: 1,2,3,4,5,6,7,8 in the loop structure (1). The number of cycles is given in the title of these questions. So it is better to take the for loop directly when the number of cycles is known.
(2) The number of cycles is unknown, but the cyclic conditions are clear when entering the cycle
Use the While loop statement. Columns such as: 1,2,3,5 in the loop structure (2). These questions do not specifically give the number of cycles, but can be known through the topic to enter the cycle of conditions. This situation is better with a while statement.
(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
Use the Do...while loop statement. Columns such as: 4,6,7 in the loop structure (2). These problems can not be known through the topic cycle times, and can not be known through the topic into the cycle of conditions. But the condition of the loop can be defined in the loop body. So it is better to use do...while sentences.

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 i,x = 0,sum = 0;double p;for(i = 0 ;x >= 0 ; ){    scanf("%d",&x);    if(x >= 0){    sum = sum+x;    i++;}}p = sum/(double)i;printf("%f",p);return 0;}

While statement:

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

Do...while statement:

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

I think it's better to use the while statement.
The while statement is simpler than the Do...while statement and is more intuitive than the for statement.

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

(1) Results of operation:

(2) Results of operation:

The function of break is to jump out of the loop; Continue is to end the cycle.

(iii) Experimental summary
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(){int i = 1;double sum = 0.0,eps = 0.0;scanf("%lf",&eps);while(fabs(1.0/i) > eps){    sum = sum+1.0/i;    i = pow(-1,i)*(fabs(i)+3);}sum = sum+1.0/i;printf("sum = %.6f",sum);return 0;}

(4) Experimental analysis
Question: The answer is wrong.
Cause: The output did not pay attention to the question of keeping several decimals.
WORKAROUND: Corrected the retained results at the time of the output.

(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!");
Break
}
Else
{
printf ("Good guess!");
Break
}
}
}
return 0;
}

(4) Experimental analysis
Question: The whole idea at the beginning was problematic.
Solution: By asking classmates and with the help of students to complete the problem.

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

(3) Source code

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

(4) Experimental analysis
Issue: No attention to the use of statements.
WORKAROUND: The Do...while statement should be used.

Problem: The condition for terminating the loop is not handled properly.
Workaround: Replace a method to terminate the loop.

(5) PTA Submission List

(iv) Mutual evaluation of blogs
1.http://www.cnblogs.com/abc15369938639/p/7856186.html
2.http://www.cnblogs.com/yaole10086/p/7851074.html
3.http://www.cnblogs.com/lliu/p/7846322.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.