#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;
void genRanNumber(float ranNum[])
{
for (int i = 0; i < 10; i++)
ranNum[i] = (float)(rand() % 100) / 10.0;
}
float calMean(float ranNum[])
{
float sum = 0.0;
for (int i = 0; i < 10; i++)
sum += ranNum[i];
return sum / 10.0;
}
float calStd(float ranNum[], float avg)
{
float std = 0.0;
for (int i = 0; i < 10; i++)
std += (ranNum[i] - avg) * (ranNum[i] - avg);
return sqrt(std / 10);
}
int main(void)
{
srand(time(NULL));
float ranNum[10] = { 0.0 };
float avg;
float std;
genRanNumber(ranNum);
cout << " Random numbers ar as follows: " << endl;
for (int i = 0; i < 10; i++) {
cout << " " << ranNum[i] << endl;
}
cout << endl;
avg = calMean(ranNum);
cout << " Average: " << avg << endl;
std = calStd(ranNum, avg);
cout << " Standard deviation: " << std << endl;
return 0;
}난수 생성을 위해 <time.h>, <stdlib.h>를 사용하였습니다.
srand(time(NULL));은 실행할 때 마다 난수표를 새로 갱신합니다.
표준편차를 위한 제곱근 함수는 math.h에서 사용하였습니다.