簡體   English   中英

將結構數組傳遞給 function 並在 C 中計算平均值?

[英]Passing struct array to function and calculating average in C?

這里的初學者,我已經嘗試了幾個小時,但無法讓它工作,也在網上搜索並找不到答案。

我正在嘗試編寫一個程序,在其中輸入人的年齡和身高,然后計算每個人的平均值以及年齡與身高的平均比率。 每當我將結構傳遞給我的 getAverage function 時,它都會返回地址(我認為)而不是平均值。

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

typedef struct person
{
    int age;
    double height;
} Person;

double getAv (Person people[50], int max)
{

    int i;
    double total, retval;
    total = 0;
    for (i=0; i<=max; i++)
    {
        total = total + people[i].age;
    }

    retval = total / max;
    return retval;
}


int main (void)
{
    Person people[50];
    int i;

    while (i++, people[i].age>=0)
    {
        printf ("Person #%d\n", i);

        printf ("enter an age: ");
        scanf ("%d", &people[i].age);

        if (people[i].age<0)
            break;

        printf ("enter a height: ");
        scanf ("%lf", &people[i].height);

        printf ("\n");
    }

    double averageAge;
    averageAge = getAv (&people[50], i);
    printf ("%.1lf\n", averageAge);
}

還沒有實現平均身高或比例,只是想得到現在工作的平均年齡。 當我嘗試從 averageAge = getAv(&people[50], I); 中取出 & 時我收到一個編譯錯誤,告訴我它需要 &。 任何幫助,將不勝感激。

如果這樣做,則需要在 function (Person& people[], int n) 中接收其中 n 是數組的長度。 我們使用 '&' 不做數組的副本,而且你只傳遞 function 中索引為 50 的元素,只做 (people, n)

這段代碼有錯誤,首先你應該在mian function中將int int i初始化為int i=0

當您沒有掃描任何輸入時,如何while (i++, people[i].age>=0)中使用此條件,就像使用未初始化的變量一樣。 你需要一個do-while

    do 
    {
        printf("Person #%d\n", i);

        printf("enter an age: ");
        scanf("%d", &people[i].age);

        if (people[i].age < 0)
            break;

        printf("enter a height: ");
        scanf("%lf", &people[i].height);

        printf("\n");
        i++;
    } while (people[i-1].age >= 0);

你也發送錯誤的 arguments 到你的getAv function 你應該使用averageAge = getAv (people, i); .

在您的 function 中,您必須在此循環中刪除= for (i=0; i<=max; i++)這應該是for (i=0; i<max; i++) ,否則輸入的負年齡也將是加到總數中並會影響平均值。

您的代碼中有幾個問題,第一個:

Person people[50];
int i;

while (i++, people[i].age>=0)

您正在使用i未初始化,因此,遞增包含垃圾的變量(或預期的 0,但無法保證)

為避免將來出現此類問題,請在編譯器上啟用警告,編譯器會告訴您以下內容:

‘i’ is used uninitialized in this function [-Wuninitialized]

所以切換到

Person people[50] = {0};
int i = 0;

似乎(基於預增量運算符++i )您想使用以 1 為底的 arrays ,這沒有任何問題,但是在您的平均 function 中,您是從0開始的,因此for使用 a :

for (i = 0; ; i++)
{
   ...
   if (people[i].age <= 0) break;
   ...
}

另一個問題:

double averageAge;
averageAge = getAv (&people[50], i);

通過這種方式,您只傳遞不屬於您的數組的元素 50(請記住 arrays 在 C 中是基數 0),以傳遞整個數組:

averageAge = getAv (people, i);

或者

averageAge = getAv (&people[0], i);

最后一個:

for (i=0; i<=max; i++)

您包括在檢查age > 0時丟棄的元素,切換到:

for (i=0; i<max; i++)

一個小問題:

如果用戶為people的第一個元素輸入 0,您最終會除以 0

double averageAge;
averageAge = getAv (&people[50], i);
printf ("%.1lf\n", averageAge);

應該

if (i > 0)
{
    double averageAge;
    averageAge = getAv (people, i);
    printf ("%.1f\n", averageAge); // No need to use lf (long double)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM