0

This was the instructions: Write a program that starts out asking the user for the mean u and standard deviation s for the normal distribution (see the wiki article http://en.wikipedia.org/wiki/Normal_distribution The program then asks for an N, and then asks for N values x. For each x it writes out f(x) to the screen. Note that the program asks the user for u, s, and N just once. After that it asks for N values for x, one by one. After each value x it writes out the corresponding value of the function. Of course, use double precision for this and use the square root and exponential function from the standard math library.

This is my code so far but I can't get the N to work.

#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s,N,x,math1, math2, math3,n,;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Deviation: \n");
scanf("%d", &s);


    n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x = math1 * exp(math3)*exp(math2);
printf("%d \n", x);
system("Pause");
}

1 Answer 1

1
n=1/2;

This will be equal to 0, since 1 is an integer, 2 is an integer and 1 divided by 2 is 0 in integer math.

Try 1.0/2.0

Make sure every other division has a double on one or both sides, otherwise it will be done as integer math

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.