输出打印扫描函数两次,而不是一次。需要修复

The output prints the scan functions twice, not once. Needs fixing

我这里的代码很好用:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int* get_data(int num_grades);
float calc_average(int num_grades, int grades[]);
void display_average(float ave);
float standard_deviation(int num_grades);
void display_standarddeviation(float standarddev);
float calc_median(int num_grades, int grades[]);
void display_median(float median);

int main(int argc, char* argv[])
{
    const int MAX_GRADE = 100;
    printf("enter number of grades: ");
    int num_grades;
    scanf("%d", &num_grades);

    int* result = get_data(num_grades);

    if(num_grades == 0)
    {
        printf("no grades to average\n");
    }
    else
    {
        float ave = calc_average(num_grades, result);
        display_average(ave);
        float standarddev = standard_deviation(num_grades);
        display_standarddeviation(standarddev);
        float median = calc_median(num_grades, result);
        display_median(median);
    }

    free(result);
    return 0;
}

float calc_average(int num_grades, int grades[])
{
    float ave;
    int i;
    int sum = 0;
    for(i = 0; i < num_grades; i++)
    {
        sum += grades[i];
    }
    ave = (float)sum/num_grades;

    return ave;
}

float calc_median(int num_grades, int grades[])
{
    float median;
    int i;
    for (i = 0; i < num_grades; i++)
    {
        if(i % 2)
        {
            median = (grades[i/2] + grades[(i/2)+1]) / 2.0;
        }
        else 
        {
            median = grades[(i/2)+1];
        }
    }

    return median;
}

float standard_deviation(int num_grades)
{
    float standarddev;
    int i;
    float formula = 0.0f;
    int* result = get_data(num_grades);
    formula = powf((*result - calc_average(num_grades, result)), 2);

    float ave = formula/num_grades;
    standarddev = sqrtf(ave);

    return standarddev;     
}


void display_average(float ave)
{
    printf("average: %.2f\n", ave);
}
void display_standarddeviation(float standarddev)
{
    printf("standard deviation: %.2f\n", standarddev);
}
void display_median(float median)
{
    printf("median: %.2f\n", median);
}

int* get_data(int num_grades)
{
    int* a;
    a = malloc(num_grades * sizeof(int));
    int i;
    for(i = 0; i < num_grades; i++)
    {
        printf("enter a grade: ");
        int grade;
        scanf("%d", &grade);
        if(grade <= 100)
        {
            a[i] = grade;
        }
        else
        {
            printf("grade needs to be > 0 and <= 100\n");
            i--;
        }
    }

    return a;
}

但是,输出如下所示:

enter number of grades: 5
enter a grade: 75
enter a grade: 77
enter a grade: 80
enter a grade: 85
enter a grade: 90
average: 81.40
enter a grade: 75
enter a grade: 77
enter a grade: 80
enter a grade: 85
enter a grade: 90
standard deviation: 2.86
median: 85.00

我希望输出如下所示:

enter number of grades: 5
enter a grade: 75
enter a grade: 77
enter a grade: 80
enter a grade: 85
enter a grade: 90
average: 81.40
standard deviation: 2.86
median: 85.00

如何调整此代码,使其不会 运行 扫描函数上的双循环?

standard_deviation 功能已损坏。它再次获得成绩:

float standard_deviation(int num_grades)
{
    float standarddev;
    int i;
    float formula = 0.0f;
    int* result = get_data(num_grades); /* XXX */
    formula = powf((*result - calc_average(num_grades, result)), 2);

你可以这样改写:

float standard_deviation(int num_grades, int* result)
{
    float standarddev;
    int i;
    float formula;
    formula = powf((*result - calc_average(num_grades, result)), 2);

当然还要相应地更新 main