0

How do you write a CPP program to find standard deviation of N numbers?

Updated: 8/11/2023

Wiki User

9y ago

1. Calculate the mean average of the N numbers. Suppose it is stored in the variable D.

2. Now calculate the differences between each number with D and square the value. As there are N numbers so store this difference in an array. Example arr[0] = (num0 - D)^2; arr[1] = (num1 - D)^2; arr[N-1] = (numN - D)^2;

3. Now sum array value and divide by N, suppose the value is stored in F. Now square root F. It is the standard deviation of your N number.

I hope you can write the code by yourself or follow the part:

suppose you will store the N numbers in an array num. Now:

int num[N+2], D = 0; //or declare the num array as float or double if there are any precision value

for(int i = 0; i < N; i++)

{

D += num[i];

}

D /= N;

int arr[N+2];

for(int i = 0; i < N; i++)

{

arr[i] = (num[i] - D)^2; //square the difference.

}

int F = 0; //if precision value is accepted then declare F as float or double not int.

for(int i = 0; i < N; i++)

{

F += arr[i];

}

F /= N;

F = sqrt(F); //use #include <cmath> in your header file list so that you can use sqrt() function or simply use #include <bits/stdc++.h>

cout<<F<<endl;

- Thanks

Wiki User

9y ago

Wiki User

12y ago

Write a program to calculate the standard deviation of an array of values.

S.D. = Use nesting function concept