The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Compute standard deviation
Posted by: Gdog | May 8, 2009 @ 10:09pm
C++ Code
[
Download
]
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { int num = 1; char c = 'a'; float sum = 0, ssum = 0, f = 0; char g[8]; int i = 0,j = 0; printf("How many numbers to average?"); i = 0; while( i < 7 && (c = getchar()) != '\n') { g[i++] = c; } g[i]='\0'; num = atoi(g); if(num <= 0) { printf("Error must enter value > 1\nExiting..."); exit(1); } for(j = 0;j<num;++j) { printf("Enter number %d of %d:",j+1,num); i = 0; while( i < 7 && (c = getchar()) != '\n') { g[i++] = c; } g[i]='\0'; f = atof(g); sum += f; ssum += f*f; } printf("The standard deviation is %g",sqrt(ssum/num - pow(sum/num,2))); getchar(); return 0; }
Syntax Highlighting
[
Open in new window
]
Author Comments
If there are N data points x_i and the mean is X then the std deviation is computed as sqrt( (1/N)*sum {x_i^2}- X^2). This formula lets us avoid having to store the input numbers in an array.
Rating
4.48 / 8
129 Votes
http://codebin.yi.org/274
page generated in 0.00 seconds