[英]I don't understand passing arrays to functions in C
The program I have works.我的程序有效。 Once I try to clean it up with functions I get totally lost.
一旦我尝试用函数清理它,我就完全迷失了。 Can someone please review my code and help me understand what I'm doing wrong?
有人可以查看我的代码并帮助我了解我做错了什么吗?
The program output needs to look like this (please note, this photo was taken prior to adding functions):程序输出需要看起来像这样(请注意,这张照片是在添加功能之前拍摄的):
Ideally, after the update_level
loop, I would then print the totals.理想情况下,在
update_level
循环之后,我会打印总数。
Below is my code with functions that I don't understand at all.以下是我的代码,其中包含我根本不了解的功能。
#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
With some help, I was able to debug my program.在一些帮助下,我能够调试我的程序。 Below is the correct code.
下面是正确的代码。
#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.