以下是说明:
请注意,程序只要求用户输入u、s和N一次。之后,它逐个询问x的N个值。在每个值x之后,它写出函数的相应值。
这是我的代码:
#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Standard Deviation: \n");
scanf("%d", &s);
printf("Enter number of x's \n");
scanf("%d", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value \n");
scanf("%d", &x);
n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x1 = math1 * exp(math3)*exp(math2);
printf("%d \n", x1);
}
system("Pause");
}我的程序在“输入X的数量。有人能帮我找出原因吗?”之后停止。
发布于 2013-01-27 09:57:07
向scanf()传递不正确的格式字符串会调用未定义的行为。由于值为double,因此所有格式说明符都应为%lf
scanf("%lf", &u);等。
因此,不会进入for循环,并在pause处停止。
https://stackoverflow.com/questions/14543675
复制相似问题