【问题标题】:Not Printing Average不打印平均值
【发布时间】:2017-11-29 06:56:47
【问题描述】:

我有一个项目在两个文件中,但我无法让主程序打印出平均变量,无论我改变什么,我都只会得到 0.0。它也没有打印出一个完整的其他功能任何提示?

Main File:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float average(void);
float std_dev(float);
float output(float);

float  array[10]  =  {4.8,  12.98,  82.1,  5.98,  19.75,  24.9,  75.7,  3.45,  10.0,  28.11};
extern float avg;
int main()
{
 float s = 0.0;
 printf("The average value of the array is %.2f \n", avg);
 s = std_dev(avg);
 printf("The standard deviation of the array is %.2f \n", s);
 return 0;
} 

static void output(float var)
{
    printf("The value of the variable is %.2f \n", var);
}

第二个文件:

#include <math.h>
extern float array[];
float avg = 26.78;

static float average()
{
    int n;
    float sum = 0.0, mean=0.0;
    for(n=0; n<10; n++)
        sum = sum + array[n];
    mean= sum/10;
    output(mean);
    return mean;
}
float std_dev()
{
int n;
float cumm_diff = 0.0;
for(n=0; n<10; n++)
     cumm_diff += (avg -array[n]) * (avg -array[n]);
return sqrt(cumm_diff/10);
}

【问题讨论】:

  • 奇怪的是,你用float代替double,却用sqrt()代替sqrtf()
  • 您应该尽可能避免使用无参数函数。如果您将数组作为长度加上指向开始的指针传递(例如float std_dev(size_t num_data, float data[])
  • 程序编译后(有编译错误如output()的声明和定义不同),avg变量打印就好了。
  • 发布的代码包含大量 double 文字。在所有情况下,它们都应该是 float 文字。要使它们成为 float 文字,请将 f 附加到每个文字的末尾。
  • 关于:static void output(float var) 使用static 修饰符背后的想法是该函数在当前文件之外不可见。建议将语句更改为:void output( float var ) 然后在第二个文件中插入语句:extern void output( float );

标签: c file debugging io syntax-error


【解决方案1】:

以下建议的代码:

  1. 干净编译
  2. 执行所需的功能
  3. 演示如何在外部文件中执行函数
  4. 正确地对函数进行原型设计
  5. 删除无关代码
  6. 适当地格式化代码,以便于阅读和理解
  7. 消除“神奇”数字
  8. 让编译器确定数组中有多少项
  9. 使用float 文字而不是double 文字
  10. 记录了包含每个系统头文件的原因
  11. 使用了有意义的参数和变量名
  12. 在编译时导出count 的值
  13. 在需要时将int count 参数转换为float

现在建议的代码:

头文件:main.h

#ifndef MAIN_H
#define MAIN_H
void output(float var);
extern float avg;
#endif // MAIN_H

头文件:util.h

#ifndef UTIL_H
#define UTIL_H
float calc_std_dev( float *, int  );
void  calc_mean( float *, int );
#endif // UTIL_H

main.c 文件

#include <stdio.h>  // printf()
#include "main.h"
#include "util.h"


float  array[]  =
{
    4.8f,  12.98f,  82.1f,  5.98f,  19.75f,
    24.9f, 75.7f,   3.45f,  10.0f,  28.11f
};



int main( void )
{
    printf("The average value of the array is %.2f \n", avg);

    float s = calc_std_dev( array, sizeof(array)/sizeof(float) );
    printf("The standard deviation of the array is %.2f \n", s);

    calc_mean( array, sizeof(array)/sizeof(float) );
    return 0;
}


void output(float var)
{
    printf("The mean value of the array is: %.2f \n", var);
}

util.c 文件

#include <math.h>  //sqrtf()
#include "main.h"
#include "util.h"


float avg = 26.78f;


void calc_mean( float array[], int count )
{
    int n;
    float sum  = 0.0f;
    float mean = 0.0f;

    for(n=0; n<count; n++)
        sum = sum + array[n];

    mean= sum/ (float)count;
    output(mean);
}


float calc_std_dev( float array[], int count  )
{
    int n;
    float cumm_diff = 0.0f;

    for(n=0; n<count; n++)
         cumm_diff += (avg -array[n]) * (avg -array[n]);

    return sqrtf(cumm_diff / (float)count);
}

【讨论】:

    【解决方案2】:

    你永远不会调用你的函数average(),这就是为什么不计算平均值的原因,但是std_dev()会打印和使用初始化值avg = 26.78(不是0 - 你可能在某个时候改变了它)。

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多