zoukankan      html  css  js  c++  java
  • koExercise 5.2 Summing 100 data values

    Exercise 5-2. Define an array, data, with 100 elements of type double. Write a loop that
    will 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 will 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 100 data values
     2 #include <stdio.h>
     3 
     4 int main(void)
     5 {
     6   double data[100];                    // Stores data values
     7   double sum = 0.0;                    // Stores sum of terms
     8   double sign = 1.0;                   // Sign - flips between +1.0 and -1.0
     9   size_t i = 0;
    10 
    11   int j = 0;
    12   for( i = 0 ; i < sizeof(data)/sizeof(double) ; ++i)// sizeof (数组名字)计算了这个数字的大小
    13   {
    14     j = 2*(i + 1);
    15     data[i] = 1.0/(j * (j + 1) * (j + 2));
    16     sum += sign*data[i];
    17     sign = -sign;
    18  }
    19 
    20    // Output the result
    21   printf("The result is %.4lf
    ", 4.0*sum + 3.0);
    22   printf("The result is an approximation of pi, isn't that interesting?
    ");
    23     return 0;
    24     }
  • 相关阅读:
    Django(七)缓存、信号、Form
    Django(六)Session、CSRF、中间件
    Django(五)母版继承、Cookie、视图装饰器等
    Django(四) ORM 外键操作及初识Ajax
    Django(三) ORM 数据库操作
    Django(二)路由系统、视图、模板
    wc命令
    df命令
    rm命令
    mv命令
  • 原文地址:https://www.cnblogs.com/xiaomi5320/p/4173620.html
Copyright © 2011-2022 走看看