Exercise 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)//神马意思?
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\n", 4.0*sum + 3.0);
22   printf("The result is an approximation of pi, isn‘t that interesting?\n");
23     return 0;
24     }
时间: 2024-10-16 11:27:28

Exercise 5.2 Summing 100 data values的相关文章

*Exercise 5.1 Summing reciprocals of five values

Exercise 5-1. Write a program that will read five values of type double from the keyboardand store them in an array. Calculate the reciprocal of each value (the reciprocal ofvalue x is 1.0/x) and store it in a separate array. Output the values of the

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this part of the exercise, I will build a logistic regression model to predict whether a student gets admitted into a university. You want to determine each

k-Exercise 5.3 Handling monetary values as integers

Exercise 5-3. Write a program that will read five values from the keyboard and storethem in an array of type float with the name amounts. Create two arrays of fiveelements of type long with the names dollars and cents. store the whole number partof e

Spock - Document - 03 - Data Driven Testing

Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful to exercise the same test code multiple times, with varying inputs and expected results. Spock's data driven testing support makes this a first class

Lessons Learned from Developing a Data Product

Lessons Learned from Developing a Data Product For an assignment I was asked to develop a visual ‘data product’ that informed decisions on video game ratings taking as an indicator their ranking on the MetaCritic site. I decided to use RStudio’s Shin

Quality assessment and quality control of NGS data

http://www.molecularevolution.org/resources/activities/QC_of_NGS_data_activity_new table of contents expected learning outcomes getting started exercise 1: checking Illumina data with the FASTX-Toolkit exercise 2: checking 454 data with the FASTX-Too

Python for Data Analysis | MovieLens

Background MovieLens 1M数据集含有来自6000名用户对4000部电影的100万条评分数据. ratings.dat UserID::MovieID::Rating::Timestamp users.dat UserID::Gender::Age::Occupation::Zip-code movies.dat MovieID::Title::Genres 通过pandas.read_table将各个表分别读到一个pandas DataFrame对象中. * head=Non

HTML5数据存储方案data与jQuery数据存储方案$.data()的区别

我们先看下$.fn.data()的使用,这个和$.data()是不一样的,前者是和某个jquery对象相关,后者则是全局方法.主要有data()和removeData()这2个实例方法.通过下面的例子和执行结果可以看到:$.fn.data()和$.fn.removeData()跟$.data的使用方式没有什么差别. // 支持任何数据类型 2. $( "body" ).data( "name", "xx" ); 3. $( "body&

Jmeter 二次开发 将CSV Data Set Config添加从哪一行开始读数据

经常遇到性能测试的时候,有100万条数据,才用了5万条,中途因为某些原因停止了,继续用的时候, 要么要清除DB中数据,要么要清除数据源中的数据, 觉得特别麻烦, 希望改写下代码,将 Ignore first line (only used if Variable Names is not empty)一列变成开始圆形需要选择的行数, StartLineNumber或者新增一个属性,StartLineNumber,原有的属性不需要更改. 看了一下大概涉及的java文件如下:config目录下的CS