*Exercise 5.1 Summing reciprocals of five values

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

 1 //Exercise 5.1 Summing reciprocals of five values
 2 #include <stdio.h>
 3
 4 int main(void)
 5 {
 6   const int nValues = 5;               // Number of data values
 7   double data[nValues];
 8   int i = 0;              // Stores data values
 9   double reciprocals[nValues];
10   double sum = 0.0;                    // Stores sum of reciprocals
11
12   printf("Enter five values separated by spaces: \n");
13   for( i = 0 ; i < nValues ; ++i)
14     scanf("%lf", &data[i]);
15
16   printf("You entered the values:\n");
17   for( i = 0 ; i < nValues ; ++i)
18     printf("%15.2lf", data[i]);
19   printf("\n");
20
21   printf("\nThe values of the reciprocals are:\n");
22   for( i = 0 ;  i < nValues ; ++i)
23   {
24     reciprocals[i] = 1.0/data[i];
25     printf("%15.2lf", reciprocals[i]);
26   }
27   printf("\n\n");
28
29   for( i = 0 ; i<nValues ; i++)
30   {
31     sum += reciprocals[i];              // Accumulate sum of reciprocals
32     if(i > 0)
33       printf(" + ");
34     printf("1/%.2lf", data[i]);
35   }
36   printf(" = %lf\n", sum);
37   return 0;
38 }
时间: 2024-10-10 18:30:10

*Exercise 5.1 Summing reciprocals of five values的相关文章

Exercise 5.2 Summing 100 data values

Exercise 5-2. Define an array, data, with 100 elements of type double. Write a loop thatwill 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 tha

深度学习 Deep LearningUFLDL 最新Tutorial 学习笔记 2:Logistic Regression

1 Logistic Regression 简述 Linear Regression 研究连续量的变化情况,而Logistic Regression则研究离散量的情况.简单地说就是对于推断一个训练样本是属于1还是0.那么非常easy地我们会想到概率,对,就是我们计算样本属于1的概率及属于0的概率,这样就能够依据概率来预计样本的情况,通过概率也将离散问题变成了连续问题. Specifically, we will try to learn a function of the form: P(y=1

CoAP协议笔记[RFC7252]

1. What is CoAP “The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications

ufldl学习笔记与编程作业:Logistic Regression(逻辑回归)

ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践. 在deep learning高质量群里面听一些前辈说,不必深究其他机器学习的算法,可以直接来学dl. 于是最近就开始搞这个了,教程加上matlab编程,就是完美啊. 新教程的地址是:http://ufldl.stanford.edu/tutorial/ 本节学习链接:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 有了线性回归的基础再来学

(4)风色从零单排《C++ Primer》 变量,引用,指针

从零单排<C++ Primer> --(4)变量,引用,指针   变量的初始化 所谓变量的初始化,指在创建变量的同时给予值. 初始化方法: int units_sold = 0; int units_sold = {0}; int units_sold{0}; int units_sold{0}; long double ld = 3.1415926536: int a{ld}, b = {ld}; //error:narrowing conversion required int c(ld)

mysql-省市区县-中华人民共和国统计局-最新行政区域划分-有需要其他的格式的,留下联系方式

insert into t_province (ProvinceCode,ProvinceName) values ('410000','河南省'); insert into t_province (ProvinceCode,ProvinceName) values ('420000','湖北省'); insert into t_province (ProvinceCode,ProvinceName) values ('422800','恩施土家族苗族自治州'); insert into t_p

kExercise 5.4 Table of reciprocals, squares, cubes, and fourth powers.

Exercise 5-4. Define a two-dimensional array, data[11][5] , of type double. Initializethe elements in the first column with values from 2.0 to 3.0 inclusive in steps of 0.1.If the first element in a row has value x, populate the remaining elements in

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable In thispart of this exercise, you will implement linear regression with one variableto predict profits for a food truck. Suppose you are the CEO of a rest