Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will read five values of type double from the keyboard and

ID: 3869940 • Letter: W

Question

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 a 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.

code is

/*Exercise 1 Summing reciprocals of five values */
#include <stdio.h>

int main(void)
{
const int nValues = 5; /* Number of data values */
double data[nValues]; /* Stores data values */
double reciprocals[nValues];
double sum = 0.0; /* Stores sum of reciprocals */
int i;

printf("Enter five values separated by spaces: ");
for(i=0; i<nValues; i++){
scanf("%lf", &data[i]);
}

printf(" You entered the values: ");
for(i=0; i<nValues; i++){
printf("%lf ", data[i]);
}
for(i=0; i<nValues; i++){
reciprocals[i] = 1.0/data[i];
}
for(i=0; i<nValues; i++){
sum = sum + reciprocals[i];
}
printf(" Sum is %lf ",sum);


return 0;
}

Explanation / Answer

Hi Let me know if you need more information:-

=======================================

#include <stdio.h>

int main(void) {
   const int nValues = 5; /* Number of data values     */
   double data[nValues]; /* Stores data values        */
   double reciprocals[nValues];
   double sum = 0.0; /* Stores sum of reciprocals */
   int i;

   printf("Enter five values separated by spaces: ");
   fflush(stdout);
   for (i = 0; i < nValues; i++) {
       scanf("%lf", &data[i]);//reads the input from console
   }
   /* SECOND WAY READING ONE BY ONE VALUE INTO ARRAY
   for (i = 0; i < nValues; i++) {
   printf("Enter value: ");
   fflush(stdout);//to send buffer to the output
   scanf("%lf", &data[i]);//read values from keyboard
   }
   */
   printf(" You entered the values: ");
   for (i = 0; i < nValues; i++) {
       printf("%lf ", data[i]);
       fflush(stdout);
   }
   for (i = 0; i < nValues; i++) {
       reciprocals[i] = 1.0 / data[i];
   }
   for (i = 0; i < nValues; i++) {
       sum = sum + reciprocals[i];
   }
   printf(" Sum is %lf ", sum);
   fflush(stdout);

   return 0;
}


===============

OUTPUT:-

================

Enter five values separated by spaces:
12 23 45 67 89

You entered the values:
12.000000 23.000000 45.000000 67.000000 89.000000
Sum is 0.175195

======

Enter five values separated by spaces:
12.3 14.6 23.7 23.93 98.5

You entered the values:
12.300000 14.600000 23.700000 23.930000 98.500000
Sum is 0.243929

============

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote