【问题标题】:I don't understand passing arrays to functions in C我不明白将数组传递给 C 中的函数
【发布时间】:2020-10-04 00:59:58
【问题描述】:

我的程序有效。一旦我尝试用函数清理它,我就会完全迷失方向。有人可以检查我的代码并帮助我了解我做错了什么吗?

程序输出需要看起来像这样(请注意,这张照片是在添加功能之前拍摄的):

理想情况下,在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

【问题讨论】:

  • 你显示的代码有什么问题?
  • 好吧,它不能编译。我不明白如何将数组传递给函数。
  • 您是否尝试过使用update_level(players, healthpoints)
  • 如果它不能编译,编译器应该告诉你原因。你读了吗?错误信息是否有您不理解的地方?
  • 是的,有很多我不明白的地方。您之前在不使用 for 循环的情况下尝试的评论帮助我找到了一些解决问题的方法。感谢您帮助我修复程序。

标签: arrays c function


【解决方案1】:

在一些帮助下,我能够调试我的程序。下面是正确的代码。


#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 2012-07-25
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多