# Pass by reference problem

Discussion in 'C' started by askmewhy25, Mar 4, 2010.

Joined:
Jan 24, 2010
Messages:
49
0
Trophy Points:
0
I am making a program that solves for Standard Deviation unfortunately my program
doesn't print correct answer but just prints the 1.#J or the infinity error if I am right

I can't figure out what is the problem with my codes can someone help me to debug..
because I don't have enough knowledge about pass by reference

here is the code

Code:
```//Prototypes
double Standard();
double EvalStandard(double mean,double calc,int b,double data[]){
int a;
double sum,sd;
for(a=0;a<b;a++){
sum=sum+data[a];
}
mean=sum/a;
for(a=0;a<b;a++){
calc+=pow((data[a]-mean),2);
}
sd=sqrt(calc/(b-1));
return sd;
}

//Function
//Standard Deviation Function
double Standard(){
int SA,a,b;
double sum=0,data,mean,calc,sd;
char error;

do{
system("cls");
printf("Standard Deviation\n\n");
printf("Enter the Number of Data (max. 10): ");
scanf("%s",&error);
b=atoi(error);
if(b>10||b==0){
system("cls");
printf("Please press the spacebar to continue!!\n");
getche();
}
}while(b>10||b==0);
for(a=0;a<b;a++){
printf("Enter the Actual Values of Data %d: ", a+1);
scanf("%s",&error);
data[a]=atof(error);
}
if(b==1){
printf("The Standard Deviation the Data is 0\n");
printf("Please press the spacebar to continue!!");
getche();
}
else{
sd=EvalStandard(mean,calc,b,data);
printf("The Standard Deviation the Data is %.2lf\n",sd);
printf("Please press the spacebar to continue!!");
getche();
}
}
```

Last edited: Mar 4, 2010
2. ### karthigayanNew Member

Joined:
Feb 19, 2010
Messages:
33
0
Trophy Points:
0
I have checked your code in linux environment and I inserted some of the lines to manage the flow.

When I execute I got the error "undefined reference to `sqrt'".Because in the program you used a math.h function.

Then I compile the program using -lm option.It is for the math.h functions.

cc -lm test_program.c

Then I ran the program It got work.

Code:
```#include<stdio.h>
#include<math.h>
//Prototypes
double Standard();
double EvalStandard(double mean,double calc,int b,double data[]){
int a;
double sum,sd;
for(a=0;a<b;a++){
sum=sum+data[a];
}
mean=sum/a;
for(a=0;a<b;a++){
calc+=pow((data[a]-mean),2);
}
sd=sqrt(calc/(b-1));
return sd;
}

//Function
//Standard Deviation Function
double Standard(){
int SA,a,b;
double sum=0,data,mean,calc,sd;
char error;
char c;
while(1)
{
do{
system("clear");
printf("Standard Deviation\n\n");
printf("Enter the Number of Data (max. 10): ");
scanf("%s",&error);
b=atoi(error);
if(b>10||b==0){
system("clear");
printf("Please press the spacebar to continue!!\n");
c=getchar();
}
}while(b>10||b==0);
for(a=0;a<b;a++){
printf("Enter the Actual Values of Data %d: ", a+1);
scanf("%s",&error);
data[a]=atof(error);
}
if(b==1){
printf("The Standard Deviation the Data is 0\n");
printf("Please press the spacebar to continue!!");
getchar();
if((c=getchar())!=' ')
break;

}
else{
sd=EvalStandard(mean,calc,b,data);
printf("The Standard Deviation the Data is %.2lf\n",sd);
printf("Please press the spacebar to continue!!");
getchar();
if((c=getchar())!=' ')
break;
}
}
}

main()
{
Standard();
}
```