C Language Seeking 1-1/3+1/5-1/7+...--small program, the sermon

Source: Internet
Author: User

Problem: write program in C language to seek 1-1/3+1/5-1/7+ ...

Example:

1#include <stdio.h>2 voidMain () {3     intn=1;4     floatsum=0, a=1;5      while(a<= -){6sum=sum+n/A;7n=-N;8a=a+2;9     }Tenprintf"%f\n", sum); One}

Analysis:

The summation of the series, each alternating with plus or minus, and the denominator incrementing by 2.

First, this is an addition (summation) problem, involving Summand, Addend, and the concept. Each addition is produced as a new summand, and then a new addend is added, and the new addend (i.e. between each one) has regularity. This keeps repeating the addition operation.

The calculation is inseparable from the memory, for people, each operation of the intermediate results must be remembered or written on the draft paper to proceed to the next calculation, and the new results after the old results do not need to remember; the same is true for computers, where data is stored in a storage unit, represented in a program by variables, and used to stage various data. Includes raw data, intermediate results, and final results. Also, the value of the variable can be changed, and when the old value is no longer needed and the new value is generated, the variable is updated to the new value. Only from this point of view, Quadrochromatic paper has no advantage to say ~

For the additive problem, the Addend and (new Summand) can be saved with the same variable (sum), which is constantly updated. Start at 0, then for various intermediate results, until the final result is final and output. In addition, Addend is also a variable that is updated every time to a new value, and the update is regularly traceable: as a fraction, each molecule is updated to the opposite number, and the denominator increments by 2. The numerator denominator is changing, so it can be used as a variable individually.

However, instead of using a real variable to represent addend, we use a variable-containing expression, n/A, to save a variable.

Then, in addition to declaring and initializing variables, c code updates variables (including reference and assignment of variables) according to logic (rules). And, because the variables are updated in a regular manner, are based on the old value of the operation, you can use a general formula to represent, that is, the same form, so you can use the loop structure to achieve (here, we only loop to the denominator is 99).

Summary:

We analyze this program from the perspective of variables. The thing to do is to declare and initialize the variable (sometimes the input of the variable supersedes the initialization), and the variable's multiple references and updates (the first assignment is called initialization, again the assignment is the update), it may end up outputting it, outputting it to the screen or disk, and so on.

Formally, a variable appears to the left of the assignment (=) (when it is declared and initialized, when it is assigned), or to the right (reference).

1#include <stdio.h>2 voidMain () {3     /*declaration and initialization of variables*/4     intn=1;5     floatsum=0, a=1;6     /*cyclic reference and assignment of variables*/7      while(a<= -){8sum=sum+n/A;9n=-N;Tena=a+2; One     } A     /*the output of the variable*/ -printf"%f\n", sum); -}

Attention:

There is a hole in it, that is, if N and a are all integer variables, n/A will be integer, not float, and the result will be wrong. Therefore, we declare a as a floating-point type.

Expand:

Interestingly, Pi can be obtained by using this series (infinite series). Multiply it by 4 to get the approximate value of π, the more the number the more accurate.

C Language Seeking 1-1/3+1/5-1/7+...--small program, the sermon

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.