0

-use double precision -use sqrt() and exponential function exp() -use * to compute the square -do not use pow()

I am getting values they are just not anything as to what I expected. I tried making them all signed but it didn't change anything and I've tried printing out with 12 decimal places and nothing seems to be working.I have linked the math library and defined it as well.

double normal(double x, double sigma, double mu)
{   
     double func = 1.0/(sigma * sqrt(2.0*M_PI));
     double raise = 1.0/2.0*((x-mu)/sigma);
     double func1 = func * exp(raise);
     double comp_func = (func1 * func1);

     return comp_func;
}
int main(void)
{
// create two constant variables for μ and σ
const double sigma, mu;
//create a variable for x - only dynamic variable in equation
unsigned int  x;

//create a variable for N values of x to use for loop
int no_x;

//scaniing value into mu
printf("Enter mean u: ");   
scanf("%lf", &mu);

//scanning  value into sigma
printf("Enter standard deviation: ");   
scanf("%lf", &sigma);

//if sigma = 0  then exit
if(sigma == 0)
{
    printf("error you entered: 0");
    exit(0);
}

//storing number of x values in no_x
printf("Number of x values: "); 
scanf("%d", &no_x);

//the for loop where i am calling function normal N times
for(int i = 1; i <= no_x; i++)
{

    //printing i for the counter in prompted x values
    printf("x value %d : ", i);

    // scanning in x
    scanf("%lf", &x);


    x = normal(x,sigma,mu);

    printf("f(x) = : %lf.12", x);

    printf("\n");
}

return 0;   
}

C:>.\a.exe Enter mean u: 3.489 Enter std dev s: 1.203 Number of x values: 3 x value 1: 3.4 f(X) = 0.330716549275 x value 2: -3.4 f(X) = 0.000000025104 x value 3: 4 f(X) = 0.303015189801

But this is what I am receiving

C:\Csource>a.exe Enter mean u: 3.489 Enter standard deviation: 1.203 Number of x values: 3 x value 1 : 3.4 f(x) = : 15086080.000000 x value 2 : -3.4 f(x) = : 15086080.000000 x value 3 : 4 f(x) = : 1610612736.000000

JJCFTW
  • 1
  • 2
  • It seems unlikely the code you posted is the code you compiled and executed. The posted code fails to include ``, ``, and ``. It passes the addresses of `sigma` and `mu` to `scanf`, but they are `const`, and `scanf` requires pointers to things that are not `const`. It also passes `x` to `scanf` for `%lf`, but `x` is declared to be an `unsigned int`, and `%lf` requires a `double`. Unless your compiler is extraordinarily tolerant and you ignored warning messages, this should not have compiled. – Eric Postpischil Feb 09 '19 at 00:14
  • I did include '' & both the std's along with define. My code compiled using gcc. So maybe it was extraordinarily tolerant, but I did not receive one warning message. – JJCFTW Feb 09 '19 at 20:40

2 Answers2

0

Insert these lines:

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

Change:

const double sigma, mu;

to:

double sigma, mu;

Change:

unsigned int x;

to:

double x;

Replace the definition of the normal function with:

double normal(double x, double sigma, double mu)
{   
     double func = 1.0/(sigma * sqrt(2.0*M_PI));
     double t = (x-mu)/sigma;
     return func * exp(-t*t/2);
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Enter mean u: 3.489 Enter standard deviation: 1.203 Number of x values: 3 x value 1 : 3.4 f(x) = : 0.118418273907 x value 2 : -3.4 f(x) = : 33.750765560558 x value 3 : 4 f(x) = : 0.071913926152 These are the results I am receiving from this function `double normal(double x, double sigma, double mu) { double func = 1.0/(sigma * sqrt(2.0*M_PI)); double t = ((x-mu)/sigma); double t2 = -0.5 * t ; double fun = func * exp(t2); return fun * fun ; }` – JJCFTW Feb 09 '19 at 23:50
0
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#include<math.h>
#include<stdio.h>
#include <stdlib.h>

double normal(double x, double sigma, double mu)
{    
 double func = 1.0/(sigma * sqrt(2.0*M_PI));
 double t = (x-mu)/sigma;
 return func * exp((-0.5*t)* t);
}

I Finally got this code above working after tweaking with it literally all day lol, C math can be rather tricky, thank you for the help above as well.

JJCFTW
  • 1
  • 2