码迷,mamicode.com
首页 > 其他好文 > 详细

*Exercise 5.1 Summing reciprocals of five values

时间:2014-12-18 20:26:11      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   for   on   div   

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 }

 

*Exercise 5.1 Summing reciprocals of five values

标签:style   blog   ar   io   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/xiaomi5320/p/4172460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!