C Language Programming sixth time-cycle structure 2

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

The result of the compilation is:

So let's see what's wrong with it!

Error message 1:

Cause of error: No semicolon after do......while statement;
Correct

At this point the compilation results are:

Error message 2:

Error Reason:: Item is of type double, but 1/n is int integer, format is wrong.
Correct

At this point the compilation results are:

Compile correctly, then try it out at run time.

The output error will need to be checked again.

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
Correct

Error message 4:

Cause of error: ESP is defined as a double, and the problem does not match.
Correct

Compile and run the results;

Match the result, the modification is completed and the modified code is:

#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 (;;) Expression 1 is the initial value, the expression 2 is the judgment, the expression 3 is the step, the expression 1 is executed, then the expression 2 is executed, then the loop body is executed, then the expression 3 is executed, then the expression 2 is executed, then the loop body is executed, and the expression 3 is performed until the expression 2 exits. So for (;;) Also for infinite loops. The meaning of the two loops is equivalent. Both loops continue to loop when there is no stop statement in the statement, and the loop stops jumping if a similar break statement appears in the 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) The number of cycles is known
(2) The number of cycles is unknown, but the loop condition is explicitly
(3) The number of cycles is unknown when entering the loop, and the loop condition is unknown when entering the loop, it needs to be explicitly
for the above three cases in the loop body. What loop statements are used to achieve better? For each case, use the topics in the two-cycle structure work we have completed to illustrate.

(1) example for the FOR statement:
to write a program, calculate 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.
(2) example of a while statement:
This requires the calculation of an odd number of integers in a given series of positive numbers. 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. The
here uses the WHILE Loop statement, which allows you to enter the number of the odd and, because do not know how many times to loop, so the use of the while (1) This infinite loop, similar topics are:
The other side does not want to talk to you, and throw a bunch of numbers to you ... And you have to find the "250" on this big moving number from this string of numbers.
This number, do not know how many times to cycle, this is the typical first cycle to judge.
(3) do...while Statement Example:
to any of the natural number N0??, the first number of its members summed up, and then multiply it by 3 plus 1, into a new natural number n?1; N1?? Repeat this operation, can produce new natural number n?2?? ;...... Repeated this operation many times, the result of the operation will eventually get a fixed number n?k, like a number "trap" fall into. The
asks for the natural number of the input, and gives it the process of falling into the trap.
fall into the trap number, when you enter a natural number, you have to lock his individual digits, and then sum, his request is when the output of this time and the last and the same output, so you need to cycle first, then start to judge, until the output. The Do...while statement is used in cases where this is unknown. The
is when the output of this time and the last output output, so we need to loop once, then judge, until the output, so it is appropriate to use Do...while.
3. There are questions: Enter a batch of student scores, ending with 1 to calculate the student's average score. The
requires the use of a for statement, a while statement, a Do While statement three loop statements are implemented, and you think which form 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);}

It is more appropriate to use the while statement on this topic. Because this program is an unknown number of cycles, but the loop condition is clear when entering the loop, if you write with a for statement or a do...while statement, you need to add a judgment statement more than the while trouble.

C Language Programming sixth time-cycle structure 2

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.