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