Hello There, Guest! Login Register
Advertise Here | Remove Ads

C Program to Calculate Standard Deviation

  • 0 Vote(s) - 0 Average


06-17-2016, 06:25 PM #1
Shompa Offline FFU SENIOR FFU SENIOR
VIP MEMBER
Posts:2,319 Threads:222 Joined:Sep 2014 Reputation: 28 Sex Female
FFUC: $0
In this program, elements of arrays are used for storing the data and this array is passed to function which calculates standard deviation and finally the result(standard deviation) is displayed in main() function.

Code:
#include <stdio.h>
#include <math.h>
float standard_deviation(float data[], int n);
int main()
{
    int n, i;
    float data[100];
    printf("Enter number of datas( should be less than 100): ");
    scanf("%d",&n);
    printf("Enter elements: ");
    for(i=0; i<n; ++i)
        scanf("%f",&data[i]);
    printf("\n");
    printf("Standard Deviation = %.2f", standard_deviation(data,n));
    return 0;
}
float standard_deviation(float data[], int n)
{
    float mean=0.0, sum_deviation=0.0;
    int i;
    for(i=0; i<n;++i)
    {
        mean+=data[i];
    }
    mean=mean/n;
    for(i=0; i<n;++i)
    sum_deviation+=(data[i]-mean)*(data[i]-mean);
    return sqrt(sum_deviation/n);          
}







Forum Jump:


Users browsing this thread:1 Guest(s)

Advertise Here | Remove Ads