28/03/2016

C++ Program to Calculate Standard Deviation Using Function

#include <iostream>
#include <cmath>
using namespace std;
float standard_deviation(float data[], int n);
int main()
{
    int n, i;
    float data[100];
    cout << "Enter number of data: ";
    cin >> n;
    while (n>100 || n<=0)
    {
        cout << "Error! number should in range of (1 to 100)." << endl;
        cout << "Enter the number of data again: ";
        cin >> n;
    }
    cout << "Enter elements: " << endl;
    for(i=0; i<n; ++i)
        cin >> data[i];
    cout << endl;
    cout << "Standard Deviation = " << 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);          
}

Output:-

www.programmingwithbasics.com

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: