简体   繁体   中英

Not Printing Average

I have a project going in two files, but I cannot get the main program to print out the average variable, I just get 0.0 no matter what I change. It also does not print out a whole other function Any Tips?

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);
}

Second File:

#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);
}

the following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. demonstrates how to execute functions in external files
  4. properly prototypes the functions
  5. removes extraneous code
  6. properly formats the code for ease of readability and understanding
  7. eliminates the 'magic' numbers
  8. lets the compiler determine how many items are in the array
  9. uses float literals rather than double literals
  10. documented why each of the system header files is included
  11. used meaningful parameter and variable names
  12. derived the value of count at compile time
  13. cast the int count parameter, when needed, to a float

and now the proposed code:

header file: main.h

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

header file: util.h

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

main.c file

#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 file

#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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM