正态分布/标准偏差

时间:2013-01-30 14:55:22

标签: c

我的说明:编写一个程序,开始向用户询问正态分布的均值u和标准差s(参见wiki article

程序然后要求N,然后要求N值x。对于每个x,它会将f(x)写入屏幕。请注意,程序仅向用户询问u,s和N一次。之后,它逐个请求x的N值。在每个值x之后,它写出函数的相应值。

我感到困惑的是 N 应该代表什么。我认为这是x的数量,但有人可以为我澄清这个吗?

#include <stdio.h>
#define _USE_MATH_DEFINES 
#include <math.h>
#include <stdlib.h>

int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;

printf("Enter Mean: ");
scanf("%lf", &u);
printf("Enter Standard Deviation: ");
scanf("%lf", &s);
printf("Enter number of x's: ");
scanf("%lf", &N);

    for (v=1; v<=N; v++)
    {
    printf("Enter Value: ");
    scanf("%lf", &x);
    n=(-1/2);
    printf("f(x)= ");
    math1 =1/(u*sqrt(2*M_PI));
    math2= (x-u)/s * (x-u)/s;
    math3= M_E * exp(n);
    x1 = math1 * exp(math3)*exp(math2);
    printf("%lf  \n", x1);
    }
system("Pause");
}

2 个答案:

答案 0 :(得分:2)

N代表输入数量

从这部分中可以清楚地看出: for (v=1; v<=N; v++)

答案 1 :(得分:1)

如果你的指示是给定的,那么N确实代表所需的 x 值的数量。

您的程序就是这样,要求 x N 值。

首先,它在程序开头声明了一个变量 N

double u,s, N, x1,math1, math2, math3,n, v, x;

然后它提示输入为整数:

printf("Enter number of x's: ");
scanf("%lf", &N);

...最后使用该整数读取 x N 值。

for (v=1; v<=N; v++)
{