gpt4 book ai didi

c - 函数中的数组显示不正确

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:38 24 4
gpt4 key购买 nike

该程序用于显示3x4 阵列中3 个团队中得分最高的获胜团队。

inputteamscore 函数有问题。我找不到错误,我在 input 函数中的输入无法在 teamscore 函数中存储和计算。

void input(int a[NROW][NCOL])
{
int x,y;
double team;

for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);

for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}

}
}


int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;

for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}

for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}

return highest;
}

我所有的输出都是 0。

这是我的全部代码。

#include <stdio.h>
#define NROW 3
#define NCOL 4

void initialize(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
a[x][y]=0;
}
}

}

/* Display array in a matrix form*/
void disp_arr(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
printf("%i ",a[x][y]);
}
printf("\n");
}
}

/* Input the invidual score for 3 team*/
void input(int a[NROW][NCOL])
{
int x,y;
double team;

for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);

for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}

}
}

/* Calculate the total of score for each team and return the index for the
row with the highest team score */
int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;

for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}

for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}

return highest;
}

int main()
{
int ar[NROW][NCOL];
int team;
initialize(ar);

disp_arr(ar);

input(ar);

disp_arr(ar);

team=teamscore(ar);


printf("\nThe winniing team is Team %i",&team);

return 0;
}

如果能提供一些帮助,我将不胜感激。

谢谢!

最佳答案

关于您希望团队 成为什么,您的代码含糊不清。您提示用户输入,然后尝试将 team 存储为 double?除非您希望您的团队编号是,例如123.456,让你的团队成为一个int,例如团队 1,团队 10,等等。

您遇到的下一个问题是获取团队编号的输入,但没有提供任何方式让该条目可用于除 input 之外的任何其他函数。如果您不传递另一个参数来捕获 team 的用户输入,那么您的团队只是 a 的索引,并且没有必要提示该输入。要使用户输入在 main 中可用,只需传递另一个参数(或者只需将 +1 添加到 NCOL 并为团队使用 col 0数字)。以下是捕获用户输入的团队号码(例如团队 125, 190, ...)并将其返回给调用者的方法:

int main (void) {

int hi_index = 0,
teams[NROW] = {0}, /* note array of NROW for teams */
ar[NROW][NCOL] = {{0}};
...
input (ar, teams);

然后您可以通过将 teams 数组作为参数传递,使 input 捕获该信息,例如

/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])

您必须验证所有用户输入。当使用 scanf 时,这意味着验证 return —— 转换次数(在 format string 中指定)确实发生了.您还必须检查用户是否生成了手动 EOF(在 windoze 上为 Ctrl+dCtrl+z)。如果用户取消输入,您的代码应该优雅地处理该情况。如果用户刚刚提供了无效输入并且由于匹配输入失败导致转换失败,您需要确定是否通知用户并提示用户“再试一次”或者您是否将其视为取消。

要以这种方式处理用户输入,一种方法是循环直到您收到有效输入或用户取消,例如在您的 input 函数中,您可以执行以下操作:

        for (;;) {      /* loop until valid input or user canceled */
int rtn;

printf ("\nEnter your team: ");
rtn = scanf ("%d", &teams[x]);

if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
/* (you should empty stdin here) */
empty_stdin();
}

注意:要清空stdin,您可以简单地使用getchar()读取直到'\n'或者遇到EOF,例如

/* function to empty stdin */
void empty_stdin()
{
int c = getchar();

while (c != '\n' && c != EOF)
c = getchar();
}

您的 printfscanf format specifier 不匹配已包含在注释中。

将所有这些部分放在一起,并假设您实际上想要整数作为团队编号,而不是像 123.456 这样奇怪的团队编号,您可以执行类似于以下操作的操作:

#include <stdio.h>
#include <stdlib.h> /* for exit, EXIT_FAILURE */

#define NROW 3
#define NCOL 4

/* Display array in a matrix form*/
void disp_arr (int a[NROW][NCOL], int teams[NROW])
{
int x, y;

putchar ('\n');
for ( x = 0; x < NROW; x++)
{
printf ("team %3d : ", teams[x]);
for (y = 0; y < NCOL; y++)
printf (" %4d", a[x][y]);
putchar ('\n');
}
}

/* function to empty stdin */
void empty_stdin()
{
int c = getchar();

while (c != '\n' && c != EOF)
c = getchar();
}

/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])
{
int x, y;

for (x = 0; x < NROW; x++)
{
for (;;) { /* loop until valid input or user canceled */
int rtn;

printf ("\nEnter your team: ");
rtn = scanf ("%d", &teams[x]);

if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
empty_stdin();
}
for (y = 0; y < NCOL; y++)
{
for (;;) { /* same loop until valid or canceled */
int rtn;

printf ("Enter team[%d] score[%d]: ", teams[x], y + 1);
rtn = scanf ("%d", &a[x][y]);

if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
empty_stdin();
}
}
}
}

/* Calculate the total of score for each team.
* return the index for the row with the highest team score
*/
int teamscore (int a[NROW][NCOL])
{
int x, y,
highest = 0,
hi_index = 0,
sum[NROW] = {0};

for (x = 0; x < NROW; x++) {
for (y = 0; y < NCOL; y++)
sum[x] += a[x][y];
if (sum[x] > highest) {
highest = sum[x];
hi_index = x;
}
}

return hi_index;
}

int main (void) {

int hi_index = 0,
teams[NROW] = {0},
ar[NROW][NCOL] = {{0}};

// initialize (ar); /* ar is not a VLA, initialize at declaration */

disp_arr (ar, teams);

input (ar, teams);

disp_arr (ar, teams);

hi_index = teamscore (ar);

printf ("\nThe winning team is Team %d\n", teams[hi_index]);

return 0;
}

示例使用/输出

$ ./bin/team

team 0 : 0 0 0 0
team 0 : 0 0 0 0
team 0 : 0 0 0 0

Enter your team: 123
Enter team[123] score[1]: 10
Enter team[123] score[2]: 12
Enter team[123] score[3]: 14
Enter team[123] score[4]: 16

Enter your team: 138
Enter team[138] score[1]: 12
Enter team[138] score[2]: 14
Enter team[138] score[3]: 16
Enter team[138] score[4]: 18

Enter your team: 187
Enter team[187] score[1]: 11
Enter team[187] score[2]: 13
Enter team[187] score[3]: 15
Enter team[187] score[4]: 17

team 123 : 10 12 14 16
team 138 : 12 14 16 18
team 187 : 11 13 15 17

The winning team is Team 138

无效和取消输入示例

$ ./bin/team

team 0 : 0 0 0 0
team 0 : 0 0 0 0
team 0 : 0 0 0 0

Enter your team: The Mighty Ducks
error: invalid input.

Enter your team: user canceled input.

检查一下,如果我对您的团队类型等的假设有任何错误,请告诉我。如果您还有其他问题,请告诉我。

关于c - 函数中的数组显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com