bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / lucproblem014.c

lucproblem014.c (2886B)


      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 158, Problem 9.1 */
      5 
      6 #include <stdio.h>
      7 #include <math.h>
      8 
      9 // Function prototype: Using pointers for 'call by reference' to return 3 values.
     10 void stats(double *, double *, double *);
     11 
     12 int main()
     13 {
     14     double sum, average, standardDeviation;
     15     // Passing addresses of variables to receive results from the function.
     16     stats(&sum, &average, &standardDeviation);
     17 
     18     printf("\n--- Stats ---"
     19            "\nSum: %g"
     20            "\nAverage: %g"
     21            "\nStandard Deviation: %g",
     22            sum, average, standardDeviation);
     23     return 0;
     24 }
     25 
     26 // Function to calculate statistics on user-provided numbers.
     27 void stats(double *sum, double *average, double *standardDeviation)
     28 {
     29     int n;
     30     // Input Validation Loop for N
     31     do
     32     {
     33         printf("How many numbers you want to give input: ");
     34 
     35         if (scanf("%d", &n) == 1)
     36         {
     37             break;
     38         }
     39         else
     40         {
     41             printf("\nPlease enter a valid number.\n");
     42             // Clearing input buffer to handle invalid input
     43             while (getchar() != '\n' && !feof(stdin))
     44                 ;
     45         }
     46     } while (1);
     47 
     48     // Variable-Length Array (VLA) to store the input numbers.
     49     double inputNumber[n];
     50     int i = 0;
     51 
     52     printf("\n--- Enter Numbers ---\n");
     53 
     54     // Input Loop for numbers
     55     while (i < n)
     56     {
     57         printf("Enter number %d: ", i + 1);
     58 
     59         if (scanf("%lf", &inputNumber[i]) == 1)
     60         {
     61             // Clearing input buffer after successful read
     62             while (getchar() != '\n')
     63                 ;
     64             i++;
     65         }
     66         else
     67         {
     68             printf("Invalid input. Only integers are allowed. Please try again.\n");
     69             // Clearing input buffer to handle invalid input
     70             while (getchar() != '\n' && !feof(stdin))
     71                 ;
     72         }
     73     }
     74 
     75     // 1. Sum Calculation
     76     double tempSum = 0;
     77     for (i = 0; i < n; i++)
     78         tempSum += inputNumber[i];
     79 
     80     // 2. Average (Mean) calculation
     81     double tempAverage = tempSum / n;
     82 
     83     // 3. Standard Deviation (Sample SD formula used)
     84     double tempStandardDeviation = 0.0;
     85 
     86     // Preventing division by zero if n is 1. SD is 0 for a single number.
     87     if (n > 1)
     88     {
     89         double tempSumation = 0;
     90         // calculating the sum of squared differences from the mean
     91         for (i = 0; i < n; i++)
     92             tempSumation += pow((inputNumber[i] - tempAverage), 2.0);
     93 
     94         // Calculating sample standard deviation
     95         tempStandardDeviation = sqrt(tempSumation / (n - 1));
     96     }
     97 
     98     // Assigning final values back to the variables in main().
     99     *sum = tempSum;
    100     *average = tempAverage;
    101     *standardDeviation = tempStandardDeviation;
    102 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror