得到平均值和标准差的错误计算

时间:2014-02-07 17:59:48

标签: c

我正在尝试使用以下计算平均值和标准差 代码段。我没有得到任何编译错误。 当我使用.txt文件运行它时,输出 出现如下:

"
"is not a valid integer

错误求和,均值和标准差以及Can't input double values

int main ( int argc, char *argv[] )
{

      if(buffer == NULL)
      {

      }

      double result = fread(buffer,1,lSize,file);
      if(result != lSize){

      }
      else{

        char *str = strtok(buffer," ,");

        int count = 0;
        while(str != NULL){
          double val = (int) strtol(str,&str,10);

          if(*str != ' ' && *str != '\0')
          {


          }
          else{

            if(count == 0)
            {

              root = malloc(sizeof(struct node));
              root->next = NULL;
              root->value = val; 
              current = root;
              count++;

            }
            else{

              double x = createNode(current,val);      
            }
          }

          str = strtok(NULL," ,");
        }

      }
      current = root;
      printf("Sum is %lf and link count is %lf\n",
             getSummation(current), getNodeCount(current));

      double mean = getMean(getSummation(current),getNodeCount(current));
      double stdev = getStandardDeviation(root, mean,getNodeCount(current));



      fclose( file );
      free(buffer);
    }
  }
  return 0;
}


int createNode(struct node *n,double val)
{
  if(n != NULL){
    while(n->next != NULL)
    {
      n = n->next;
    }
    n->next = malloc(sizeof(struct node));
    n = n -> next;
    n -> value = val;
    n -> next = NULL;
  }
  return 1;
}

int getSummation(struct node *element){
  double sum = 0.0f;
  if(element != NULL)
  {
    while(element != NULL)
    {
      sum = sum + element->value;
      element= element->next;
    }
  }
  else
  {
    printf("Null Point Exception thrown");
  }
  return sum;
}

int getNodeCount(struct node *link)
{
  int count = 0;

  while(link != NULL)
  {

  }

  return count;
}

double getMean(double summation,int numberOfNodes)
{

}

double getStandardDeviation(struct node *link, double mean,int linkCount)
{


  while(link != NULL)
  {

  }

  stdev = sqrt((diffrenceSummation)/(linkCount - 1));
   return stdev;
}

1 个答案:

答案 0 :(得分:0)

1)在main()

之前添加原型
//double getSummation(struct node*element);
int getSummation(struct node *element);
int createNode(struct node *n,double val);
int getNodeCount(struct node *link);
#include <math.h>

2)使用一致的格式说明符

  //printf("Summation is %lf and link count is %lf\n", 
  //  getSummation(current), getNodeCount(current));
  printf("Summation is %d and link count is %d\n",
       getSummation(current), getNodeCount(current));

3)各种铸件都值得怀疑

// mean = (double)summation/(double)numberOfNodes;
mean = summation/numberOfNodes;
// double linkValue = (double) link->value;
double linkValue = link->value;
// buffer = (char*) malloc(sizeof(char)*lSize);
buffer = malloc(lSize);
// double val = (int) strtol(str,&str,10);
double val = strtol(str,&str,10);