Exercise 5.2 Summing 100 data values

來源:互聯網
上載者:User

標籤:c   value   val   su   values   exe   data   ci   at   

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)//神馬意思?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 result21   printf("The result is %.4lf\n", 4.0*sum + 3.0);22   printf("The result is an approximation of pi, isn‘t that interesting?\n");23     return 0;24     }

 

Exercise 5.2 Summing 100 data values

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.