簡體   English   中英

我不明白將數組傳遞給 C 中的函數

[英]I don't understand passing arrays to functions in C

我的程序有效。 一旦我嘗試用函數清理它,我就完全迷失了。 有人可以查看我的代碼並幫助我了解我做錯了什么嗎?

程序輸出需要看起來像這樣(請注意,這張照片是在添加功能之前拍攝的):

前功能

理想情況下,在update_level循環之后,我會打印總數。

以下是我的代碼,其中包含我根本不了解的功能。

#include <stdio.h>

int update_level(int player[],int healthpoint);
int display_levels();

int main(void)
{
    int levels[6] = {1,2,3,4,5,6};  // levels is an array of 6 integers
    int players[6] = {0,0,0,0,0,0}; // players is an array of 6 integers
    int healthpoints;           // initialize variable
    
    
    printf("Enter total player health points (-1 to quit): ");  // user input
    scanf("%d", &healthpoints);         // reads user input
    
    for(j=0; j<=5; j++)
    {
        update_level(players[j],healthpoints);
    }
    
    return;
}   // end main

int update_level(int player[],int healthpoint);
{
    while (healthpoints != -1)
    {
        if ( healthpoints <= 9)
        {
            players[0] += 1;
        } //end if
        else if (healthpoints <= 19)
        {
            players[1] += 1;
        } //end else if
        else if (healthpoints <= 29)
        {
            players[2] += 1;
        } //end else if
        else if (healthpoints <= 39)
        {
            players[3] += 1;
        } //end else if
        else if (healthpoints <= 49)
        {
            players[4] += 1;
        } //end else if
        else
        {
            players[5] += 1;
        } // end else

        printf("Enter total player health points (-1 to quit): ");  // user input
        scanf("%d", &healthpoints);         // reads user input
        
    }   // end while
    
    return;
}   // end update_level


int display_levels()
{   
    int i;      // initialize variable
    
    printf("T O T A L S\n\n");
    
    for (i = 0; i < 6; ++i){
        printf("Level %u%13d\n", levels[i], players[i]);
    }       // end for
    
    return;
} // end display_levels

在一些幫助下,我能夠調試我的程序。 下面是正確的代碼。


#include <stdio.h>

int update_level(int players[],int healthpoints);
int display_levels(int levels[],int players[]);

int main(void)
{
    int levels[6] = {1,2,3,4,5,6};  // levels is an array of 6 integers
    int players[6] = {0,0,0,0,0,0}; // players is an array of 6 integers
    int healthpoints;           // initialize variable
    
    
    printf("Enter total player health points (-1 to quit): ");  // user input
    scanf("%d", &healthpoints);         // reads user input
    

    update_level(players,healthpoints);
    display_levels(levels,players);

    
    return;
}   // end main

int update_level(int players[],int healthpoints)
{
    while (healthpoints != -1)
    {
        if ( healthpoints <= 9)
        {
            players[0] += 1;
        } //end if
        else if (healthpoints <= 19)
        {
            players[1] += 1;
        } //end else if
        else if (healthpoints <= 29)
        {
            players[2] += 1;
        } //end else if
        else if (healthpoints <= 39)
        {
            players[3] += 1;
        } //end else if
        else if (healthpoints <= 49)
        {
            players[4] += 1;
        } //end else if
        else
        {
            players[5] += 1;
        } // end else

        printf("Enter total player health points (-1 to quit): ");  // user input
        scanf("%d", &healthpoints);         // reads user input
        
    }   // end while
    
    return;
}   // end update_level


int display_levels(int levels[],int players[])
{   
    int i;      // initialize variable
    
    printf("T O T A L S\n\n");
    
    for (i = 0; i < 6; ++i){
        printf("Level %u%13d\n", levels[i], players[i]);
    }       // end for
    
    return;
} // end display_levels

暫無
暫無

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

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