![](/img/trans.png)
[英]Why does this function not correctly multiply the values in the array by the given multiple?
[英]Array in the function does not display correctly
该程序用于显示3x4阵列中3支球队中得分最高的获胜球队。
input
或teamscore
函数有问题。 我找不到错误,并且无法在teamscore
函数中存储和计算input
函数中的input
。
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
是模棱两可的。 您提示用户输入,然后尝试将team
存储为double
吗? 除非您期望自己的团队号是123.456
,否则请使您的团队成为一个int
,例如team 1
,team 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
,这意味着验证返回 -实际上发生了转换数(如format字符串中指定的)。 您还必须检查用户是否生成了手动EOF
(在windoze中为Ctrl + d或Ctrl + 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();
}
您的printf
和scanf
格式说明符不匹配已包含在注释中。
将所有这些部分放在一起,并假设您实际上希望将整数作为团队编号,而不是像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.
查看情况,让我知道我对您的团队类型的假设是否有误。如果您还有其他问题,请与我联系。
使用%d代替整数,而不是%lf。 %lf用于双无效输入(int a [NROW] [NCOL]){int x,y; 双人组合;
for(x=0;x<NROW;x++) { printf("\\nEnter your team: "); scanf("%i",&team); for(y=0;y<NCOL;y++) { printf("\\nEnter the score: "); scanf("%d",&a[x][y]);// use %d for integer instead of %lf. %lf for double } }
}
teamcore功能存在很多错误。 样例代码:
int teamscore(int a[NROW][NCOL]) { int x,y,highest,team; double sum; for(x=0;x<NROW;x++) { sum =0; for(y=0;y<NCOL;y++) { sum = sum + a[x][y]; } a[x][0] = sum; } highest = a[0][0]; team = 0; for(x=0;x<NROW;x++) if (a[x][0]>highest) { highest = a[x][0]; team=x; } return team;// return row number
}
在打印时仅使用团队变量,而不要在主&team
中使用&team
。 &team =团队变量的地址。
printf("\\nThe winniing team is Team %d",team);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.