lucproblem014-short.c (1449B)
1 /* Write a function that receives integers and returns the sum, average 2 and standard deviation of these numbers. Call this function from main() 3 and print the result in main() */ 4 /* Let Us C, Chap - 9, Page 159, Problem 9.1 */ 5 6 #include <stdio.h> 7 #include <math.h> 8 9 void stats(double *, double *, double *); 10 11 int main() 12 { 13 double sum, average, standardDeviation; 14 stats(&sum, &average, &standardDeviation); 15 16 printf("\n--- Stats ---" 17 "\nSum: %g" 18 "\nAverage: %g" 19 "\nStandard Deviation: %g", 20 sum, average, standardDeviation); 21 return 0; 22 } 23 24 void stats(double *sum, double *average, double *standardDeviation) 25 { 26 int n; 27 printf("How many numbers you want to give input: "); 28 scanf("%d", &n); 29 30 double inputNumber[n]; 31 int i; 32 33 printf("\n--- Enter Numbers ---\n"); 34 for (i = 0; i < n; i++) 35 { 36 printf("Enter number %d: ", i + 1); 37 scanf("%lf", &inputNumber[i]); 38 } 39 40 double tempSum = 0; 41 for (i = 0; i < n; i++) 42 tempSum += inputNumber[i]; 43 44 double tempAverage = tempSum / n; 45 46 double tempStandardDeviation = 0.0; 47 48 if (n > 1) 49 { 50 double tempSumation = 0; 51 for (i = 0; i < n; i++) 52 tempSumation += pow((inputNumber[i] - tempAverage), 2.0); 53 54 tempStandardDeviation = sqrt(tempSumation / (n - 1)); 55 } 56 57 *sum = tempSum; 58 *average = tempAverage; 59 *standardDeviation = tempStandardDeviation; 60 }