c - 错误计算均值和标准差

标签 c

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

"
"is not a valid integer

求和、均值和标准差错误,而且无法输入 double 值

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) 在main()

之前添加原型(prototype)
//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);

关于c - 错误计算均值和标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634649/

相关文章:

c - ## 运算符旁边带有空参数的类函数宏的标准行为?

代码不起作用,出现错误 : segmentation fault(core dumped)

c - C 中的函数在应该返回 int 时不返回任何内容

c++ - 将数组元素添加到链表中

c - asm volatile (""::: "memory") 的生命周期是多少?

c++ - 为什么 wcslen 在 argv[1] 上计算 1 个额外字符?

algorithm - 这个C程序背后的逻辑是什么?

c - realloc()、生命周期和 UB

c - 分配内存并将输入从文件读取到结构数组

c - C 操作方法和用例中结构的前向声明