Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Calculate the Standard Deviation

C Language Examples - Calculate the Standard Deviation

C Language Examples C Language Examples

Calculate the standard deviation.

Example

#include

<

stdio.h

#include

<

math.h

float

calculateSD

(

float

data

[

]

)

;

int

main

(

)

{

int

i

;

float

data

[

10

]

;

printf

(

"

Enter 10 elements:

"

)

;

for

(

i

=

0

;

i

<

10

; ++

i

)

scanf

(

"

%f

"

, &

data

[

i

]

)

;

printf

(

"

\

nStandard deviation = %.6f

"

,

calculateSD

(

data

)

)

;

return

0

;

}

float

calculateSD

(

float

data

[

]

)

{

float

sum

=

0

.0

,

mean

,

standardDeviation

=

0

.0

;

int

i

;

for

(

i

=

0

;

i

<

10

; ++

i

)

{

sum

+=

data

[

i

]

;

}

mean

=

sum

/

10

;

for

(

i

=

0

;

i

<

10

; ++

i

)

standardDeviation

+=

pow

(

data

[

i

]

mean

,

2

)

;

return

sqrt

(

standardDeviation

/

10

)

;

}

The output is:



输入10个元素: 1

2

3

4

5

6

7

8

9

10



标准偏差 = 2.872281

C Language Examples C Language Examples