C - 将函数值传递给另一个函数

时间:2013-03-12 01:35:50

标签: c

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

#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_sumarray_average结果。此外,我认为通过array_sum函数传递array_average而不是再次计算数组总和会更有效吗?任何想法都会非常感激!

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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_sum执行大量相同的工作而不添加任何其他值,因此您应该通过调用array_sum而不是复制代码来计算总和

答案 2 :(得分:1)

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

其次,如果您的函数不打印值本身,则会更加可重用,如array_average目前所做的那样。将这些全部放在一起,修改您的工作函数以返回有问题的值;然后你可以使用这样的代码:

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

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

答案 3 :(得分: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;
}