Exercise 5.2 Summing data values

Source: Internet
Author: User

Exercise 5-2. Define an array, data, with elements of type double. Write a loop that
Would store the following sequence of values in corresponding elements of the array:


1/(2*3*4) 1/(4*5*6) 1/(6*7*8) ... up to 1/(200*201*202)


Write another loop that would calculate the following:


DATA[0]-data[1] + data[2]-data[3] + ...-data[99]


Multiply The result of this by 4.0, add 3.0, and output the final result. Do you recognize the
Value you get?

1 //Exercise 5.2 Summing data values2#include <stdio.h>3 4 intMainvoid)5 {6   Doubledata[ -];//Stores Data Values7   Doublesum =0.0;//Stores sum of terms8   DoubleSign =1.0;//sign-flips between +1.0 and-1.09size_t i =0;Ten  One   intj =0; A    for(i =0; I <sizeof(data)/sizeof(Double) ; ++i)//God horse means?  -   { -j =2* (i +1); theData[i] =1.0/(J * (J +1) * (j +2)); -Sum + = sign*Data[i]; -Sign =-Sign ; -  } +  -    //Output The result +printf"The result is%.4lf\n",4.0*sum +3.0); Aprintf"The result is a approximation of pi, isn ' t that interesting?\n"); at     return 0; -}

Exercise 5.2 Summing data values

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.