function for calculate the average and standard deviation of table

Function for Calculating Average and Standard Deviation in C

To calculate the average and standard deviation of a table in C, you can use the following functions:

#include <stdio.h>
#include <math.h>

double calculateAverage(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return (double)sum / n;
}

double calculateStandardDeviation(int arr[], int n) {
    double mean = calculateAverage(arr, n);
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += pow(arr[i] - mean, 2);
    }
    return sqrt(sum / n);
}

Explanation:

  1. calculateAverage function:
  2. This function takes an array of integers and its size as input parameters.
  3. It iterates through the array and calculates the sum of all elements.
  4. It then returns the average by dividing the sum by the number of elements in the array.

  5. calculateStandardDeviation function:

  6. This function takes an array of integers and its size as input parameters.
  7. It first calculates the mean (average) using the calculateAverage function.
  8. Then, it iterates through the array and calculates the sum of the squared differences between each element and the mean.
  9. Finally, it returns the square root of the sum divided by the number of elements, which gives the standard deviation.

These functions can be used to efficiently calculate the average and standard deviation of a table of numbers in C.

[[SOURCE 4]]