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:

#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: There is no semicolon after the while in the Do...while statement
Correction method: Add a semicolon after the while
Error message 2:

Error cause: An error occurred in the result of the operation, the item in the source program is defined as a double type, but in the Do...while Loop statement, it becomes an integral type
Correction method: Change item=1/n to item=1.0/n or before 1 plus (double) as item= (double) 1/n
Error message 3:

Cause of error: After correcting the previous error, the result of the operation remains unchanged. Consider if the loop condition is incorrect, and then the recurrence condition is reversed.
How to FIX: Change Item < EPS to item > EPS
The result of the correction is:

But just when the third error was modified, the item was mistakenly

Consistent with the output sample. Don't quite understand why.

(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) in 1 represents a constant expression, that is, not 0, can be infinite loop, that is, a dead loop, if you want to jump out of the loop, you can add a break statement.
Similarly, for (;;) Neither the initial value nor the cyclic condition, as in the while (1) statement, is a dead loop, if you want to jump out of the loop, but also add the break statement.

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.
A: (1) a loop for statement with Count control when the number of cycles is known
(2) When the number of cycles is unknown, but the loop condition is clear when entering the loop, it is possible to use conditional control of the split-loop while statement
(3) Finally, the Do...while statement that uses the loop body to execute at least once
For example, the first question in the loop structure 1:

In the subject, the number of cycles known is n*2, so a For loop statement is preferred.
But sometimes, for example, the first question in loop structure 2:

The same is the sum, but there is no clear how many items, only the cycle conditions, so it is preferred to consider the while loop.
Sometimes the while loop and the Do...while loop are interchangeable.
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:
The For statement is:
# include

}
The while statement is:
# include

}
The Do...while statement is:
# include

}

I think it's appropriate to use the Do...while statement
4. Run the following program, enter 1 to 10, what are the results? Why?
(1)
#include

(2) The result is:

The result is that the first program uses a break to jump out of the loop, and the second program is using the contine to jump out of the loop. Break is the end of the entire loop structure, so when you enter 1 o'clock, it jumps out of the loop, and contine only jumps out of the loop.
(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:

(4) Experimental Analysis:
Problem:

Reason: No corresponding header file was added
Workaround: Add the header file at the beginning of the program:

(5) The Situation PTA submission:

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:

(4) Experimental Analysis:
Problem: compilation does not show errors, runs without errors, but always displays answer errors when you submit PTA
Reason:

Spelling has errors!!!
Workaround: Change luck to Lucky
(5) The Situation PTA submission:

3. 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:

(4) Experimental Analysis:
Problem:

Reason: No circular condition is clarified

Workaround: Remove the equals sign.
(5) The Situation PTA submission:

(iv) Mutual evaluation of blogs
Http://www.cnblogs.com/yaole10086/p/7851074.html
Http://www.cnblogs.com/windsky-1999/p/7838107.html
Http://www.cnblogs.com/123ruike/p/7851176.html

C language Programming sixth 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.