Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pass by reference problem (http://www.go4expert.com/forums/pass-reference-t21199/)

askmewhy25 4Mar2010 19:32

Pass by reference problem
 
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[10],mean,calc,sd;
  char error[128];
 
  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("Invalid input. Please try again!!!\n");
      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();
  }
}


karthigayan 5Mar2010 11:58

Re: Pass by reference problem
 
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[10],mean,calc,sd;
  char error[128];
  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("Invalid input. Please try again!!!\n");
      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();
}



All times are GMT +5.5. The time now is 05:27.