How to use "cal_ATS_function" twice in the main BUT with diff variables? tks

/* ------------------------------------cal_UCL------------------------------------------------*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <conio.h>


int main(void)

{

double cal_ATS_function(int ns, int n,double sigma,double h,double UCL, double LCL,double u);
int n, ns;
double h, UCL, LCL, UCL1, LCL1, UCL2, LCL2, sigma, u, u0;
double tau, R, ATSo;
double step;


ns = 10000;
n = 5;
sigma = 2.;
h = 2.;
u0 = 10.;
tau = 370;
R = 5;



step = 0.5*sigma;
u = u0;
UCL1 = u0;

for( UCL2=u0 +step; ;UCL2+=step)
{ LCL2 =2*u0- UCL2;
ATSo =cal_ATS_function(ns, n, sigma, h, UCL2, LCL2, u);

if(abs(ATSo-tau)< 5)
{ UCL= UCL2;
return UCL;
}

else
{
if (ATSo<tau)
UCL1=UCL2;

else

break;
}
}


do
{ UCL=0.5*(UCL1+UCL2);
LCL= 2*u0 - UCL;
ATSo=cal_ATS_function(ns, n, sigma, h, UCL, LCL, u);

if(abs(ATSo-tau)< 5)
{
return UCL;
}

else
{
if (ATSo<tau)
UCL1 = UCL;

else

UCL2 = UCL;


}

printf("UCL = %f\n ",UCL);
return 0;

}





/* ---------------------------------cal_ATS_function--------------------------------*/


double cal_ATS_function(int ns, int n,double sigma,double h,double UCL,double LCL,double u)


{
int i;
double sum,ARL,ATS,RL;
double cal_xbar_RL(int,double,double,double,double);

sum=0;

for (i=1;i <= ns; i++)
{
RL= cal_xbar_RL(n,u,sigma,UCL,LCL);
sum = sum + RL;
}

ARL = sum/ns;
ATS = ARL*h;

return ATS;
}



/* -------------------cal_xbar_RL function-------------------------------*/

double cal_xbar_RL(int n, double u, double sigma, double UCL, double LCL)

{

double RL, sum, X, xbar;
int i;
double get_x(double, double);

RL=0;

do

{
sum =0;

for(i=1;i<= n; i++)
{
X= get_x(u,sigma);
sum = sum + X;
}
xbar = sum/n;
RL = RL + 1;

}while (xbar> LCL && xbar< UCL);

return RL;

}



/*----------------------- get_x function--------------------------------------*/
/* get a sample value x */
double get_x(double u, double sigma)
/* u and sigma are the mean and standard deviation of x */
{
double uniRandom;
double normalRandom;
int random;
double normal_z(double);

do
{ random = rand();
} while (random == 0 || random == RAND_MAX);

uniRandom = random / (double)RAND_MAX;

/*produce normal distribution*/
normalRandom = normal_z(uniRandom) * sigma + u;
return normalRandom;
}




/************************************************************
normal_z() is used to find the random variable z from the
cumulative probability F. z follows a standard normal
distribution
************************************************************/
double normal_z(double F)
{ double beta, ubeta, y, yi, z;
int i;
static double b[] =
{1.570796288, 3.706987906e-2,
-8.364353589e-4, -2.250947176e-4,
6.841218299e-6, 5.824238515e-6,
-1.04527497e-6, 8.360937017e-8,
-3.231081277e-9, 3.657763036e-11,
6.936233982e-13};

if (F < 0.5 || F > 0.5)
{ beta = (F < 0.5) ? F : (1. - F);
y = -log(4. * beta * (1. - beta));

ubeta = 0.;
for (i = 0; i <= 10; i++)
{ if (i == 0)
yi = 1.;
else
yi *= y;

ubeta += (b[i] * yi);
}
ubeta *= y;
ubeta = sqrt(ubeta);
z = (F < 0.5) ? (-ubeta) : ubeta;
}
else
z = 0.;

return(z);
}






I don't understand.
hi, you don't have to know the in-depth meaning of each functions but can anyone help me check if my command is correct in calling

ATSo =cal_ATS_function(ns, n, sigma, h, UCL2, LCL2, u);




despite the original function definition is given as "UCL","LCL". AND NOT "UCL2" and "LCL1"?


Thanks
Of cause you can.

The function definition just valid in the definition.
when u call a function, you just make sure the parameter type is same as definition.
Like:

1
2
3
4
5
6
7
8
9
int mai(void)
{
    int a;
    double b;

    cal_ATS_function(a, a, b, b, b, b);

    return 0;
}



http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
CAN SOMEONE HELP Me? THANKs-Kelly


Don't understand why my function definition is wrong?



* ---------------------------------cal_ATS_function--------------------------------*/

line 91:
double cal_ATS_function(int ns, int n,double sigma,double h,double UCL,double LCL,double u)

line 94:
{



cal_UCL_draft1.cpp(91) : error C2062: type 'double' unexpected
cal_UCL_draft1.cpp(94) : error C2143: syntax error : missing ';' before '{'
My question is.. why are you declaring your prototype for your cal_ATS_function in main?? That will never compile.

You have this...

1
2
3
int main(void)
{
double cal_ATS_function(int ns, int n,double sigma,double h,double UCL, double LCL,double u);


and it should be something like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// Delcare prototype
double cal_ATS_function(int ns, int n,double sigma,double h,double UCL, double LCL,double u);

int main(void)
{

int n, ns;
double h, UCL, LCL, UCL1, LCL1, UCL2, LCL2, sigma, u, u0;
double tau, R, ATSo;
double step;

ns = 10000;
n = 5;
sigma = 2.;
h = 2.;
u0 = 10.;
tau = 370;
R = 5;

double myDouble;

myDouble = cal_ATS_function(ns, n,sigma,h,UCL, LCL,u);
Last edited on
Topic archived. No new replies allowed.