1

我有以下程序调用两个函数,一个收集数组的总和,一个收集平均值:

#include <stdio.h>

float array_sum (float myNumbers[],int size)
{
    float sum = 0;
    int i; /* Variable used in loop counter */

    for (i = 0; i < size; ++i)
    {
        sum +=myNumbers[i];
    }

}

float array_average (float myNumbers[],int size)
{
    float sum = 0;
    float average = 0;
    int i; /* Variable used in loop counter */

    for (i = 0; i < size; ++i)
    {
        sum +=myNumbers[i];
    }

    average = sum/size;
    printf("avg = %5.2f", average);
}

int main (void)
{
    int size;
    int sum;

    printf ("Enter The Amount Of Numbers In Your Array: ");
    scanf("%i", &size);

    float myNumbers[size];
    int i;

    for (i = 0; i < size; ++i)
    {
        printf ("Enter the number:");
        scanf (" %f",&myNumbers[i]);
    }

    array_sum(myNumbers,size);
    array_average(myNumbers,size);

    return 0; 
}

我想要做的是在我的程序结束时同时显示结果array_sum和结果。此外,我认为通过函数而不是再次计算数组总和array_average会更有效吗?任何想法将不胜感激!array_sumarray_average

4

4 回答 4

2

首先,您可以从 average 函数中调用sum 函数,使其无需重复代码即可完成工作。其次,你用返回类型声明了这两个函数,但你不返回任何东西。您需要添加一个 return 语句,然后将返回的值设置为main().

于 2013-03-12T01:42:43.780 回答
2

而不是调用printf你的函数,你应该返回值:

return sum;

或者

return average;

float无论如何,您的两个函数都期望返回值。你的编译器应该已经警告你了。无论如何,您应该可以这样调用它(假设您在 中声明了相关变量main):

sum = array_sum(myNumbers,size);
average = array_average(myNumbers,size);
printf("sum = %5.2f", sum);
printf("avg = %5.2f", average);

显然,该函数在没有添加任何附加值的情况下array_average做了很多相同的工作,因此您应该通过调用而不是复制代码来计算总和。array_sumarray_sum

于 2013-03-12T01:43:36.730 回答
1

如果您的函数返回相关值,这可能是最好的。您的两个工作函数都没有返回语句,他们应该这样做。你的编译器应该警告你这一点;注意这些警告。

其次,如果您的函数不像当前那样打印值本身,那么您的函数将更具可重用性array_average。把这一切放在一起,修改你的工作函数以返回有问题的值;那么你可以使用这样的代码:

float sum = array_sum(myNumbers, size);
printf("sum = %5.2f; avg = %5.2f", sum, sum/size);

还值得注意的是,您的程序可能会出现溢出和精度损失。但是,根据您的问题,我会说这些问题应该在更高级的研究中得到解决。一定要回到他们身边!

于 2013-03-12T01:48:55.123 回答
0

我有一种感觉,您应该拥有这些功能,通过这些功能可以执行以下操作:

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

/* Pass pointer to a target variable where to save sum. 
 * Though it can be nice to return it, it all depends on usage.
 * Conventionally one often return status code, and edit params.
 */
int array_sum(float myNumbers[], int size, float *sum)
{
    int i;

    /* This would indicate you have asked for the sum of an empty set. */
    if (size < 1) {
        return 1;
    }
    *sum = 0;

    for (i = 0; i < size; ++i) {
        *sum += myNumbers[i];
    }
    return 0;
}

int array_average(float sum, unsigned int size, float *avg)
{
    /* Duh. */
    if (size == 0) {
        return 1;
    }
    *avg = sum / size;
    return 0;
}


/* A lot of redundant checks here, but only to show the concept.
 * Normally one would often call the function from context where
 * one does not have control on e.g. "size" before you actually 
 * are going to sum it.
 *
 * Here you would probably check if size is < 1 after scanf and
 * ask for new number or abort.
 */
int main(void /* int argc, char *argv[] */)
{
    int i;
    int size;
    float sum;
    float avg;
    float *myNumbers;

    printf ("Enter The Amount Of Numbers In Your Array: ");
    if (!scanf("%i", &size)) {
        fprintf(stderr,
            "Unable to read size.\n"
        );
        return 1;
    }

    if ((myNumbers = malloc(sizeof(float)* size)) ==NULL) {
        fprintf(stderr,
            "Memory error.\n"
        );
        return 2;
    }

    for (i = 0; i < size; ++i) {
        fprintf(stderr, "Enter the number: ");
        if (!scanf("%f", &myNumbers[i])) {
            fprintf(stderr,
                "Unable to read float.\n"
                "Try again."
            );
            scanf("%*s"); /* Empty out buffer. */
            --i;          /* Reset count. */
        }
    }

    if (array_sum(myNumbers, size, &sum) != 0) {
        fprintf(stderr,
                "Sum failed.\n"
        );
        return 2;
    }

    if (array_average(sum, size, &avg) != 0) {
        fprintf(stderr,
                "AVG failed.\n"
        );
        return 3;
    }

    fprintf(stdout,
        "Total numbers: %d\n"
        "Sum          : %.2f\n"
        "AVG          : %.2f\n"
        ,
        size,
        sum,
        avg
    );

    free(myNumbers);

    return 0;
}
于 2013-03-12T02:11:41.220 回答