我有这个简短的C代码。但是当我尝试为下面的文件运行C程序时,它显示以下错误
#define MAX 1000 - use of undeclared identifier
roundrobin(schedule, n); too long for functional call
我哪里错了?
编辑完整的代码以制作一个ROUNDROBIN(参考:http://www.math.niu.edu/~rusin/known-math/97/roundrobin)
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
int roundrobin(int schedule[MAX][MAX], int n);
int main(void)
{
int schedule[MAX][MAX];
int n, r, i, rounds;
/* Input number of teams in the schedule. */
printf("Enter the number of teams that you want a schedule for: ");
scanf("%d", &n);
/* If the number of teams is even, requires n-1 rounds; if odd, requires n. */
if (n % 2)
rounds = n;
else
rounds = n-1;
roundrobin(schedule, n);
/* Print a nice table. */
printf("\n team\n \\ ");
for (i = 0; i < n; i++)
printf("%6d", i+1);
printf("\n");
printf("round \\");
for (i = 0; i < 6 * n; i++)
printf(".");
printf("\n");
for (r = 0; r < rounds; r++)
{
printf("%6d:", r+1);
for (i = 0; i < n; i++)
printf("%6d", schedule[r][i] + 1);
printf("\n");
}
printf("\n");
/* Check the schedule and print whether or not it's valid. */
if (check(schedule, n))
printf("Schedule is valid.\n");
else
printf("Schedule is not valid.\n");
}
/*************************************************************************
Compute the round-robin tournament schedule for n teams. If n is even,
then there are n-1 rounds; if n is odd, there are n rounds and each team
is idle in exactly one round. A team being idle is indicated by the
schedule saying that it plays team number -1 in a round.
*************************************************************************/
int roundrobin(int s[MAX][MAX], int n)
{
int rounds, m, r, i;
/* m is the lowest even number greater than or equal to n. */
if (n % 2)
m = n + 1;
else
m = n;
/* If the number of teams is even, requires n-1 rounds; if odd, requires n. */
if (n % 2)
rounds = n;
else
rounds = n-1;
/* Fill in the table with a nice diagonal pattern. */
for (r = 0; r < rounds; r++)
{
for (i = 0; i < r; i++)
s[r][i] = ((rounds+r-i+1) + m) % m;
for (i = r; i < n; i++)
s[r][i] = ((rounds+r-i) + m) % m;
}
/* Now, do knight-like moves with the 0 in the first row. Every time the 0
lands on a number, put that number in the first column. */
r = 0;
for (i = m-2; i > 0; i--)
{
r = ((r - 2) + rounds) % rounds;
s[r][0] = s[r][i];
s[r][i] = 0;
}
/* If m != n, then remove team n from all the games, and replace with -1. */
if (m != n)
for (i = 0; i < rounds; i++)
s[i][i] = -1;
}
/*************************************************************************
Looks at a schedule and determines whether it is valid.
*************************************************************************/
int check(int s[MAX][MAX], int n)
{
int game[MAX][MAX];
int rounds, r, i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
game[i][j] = 0;
/* If the number of teams is even, requires n-1 rounds; if odd, requires n. */
if (n % 2)
rounds = n;
else
rounds = n-1;
/* Count the number of times every team plays every other team. */
for (r = 0; r < rounds; r++)
for (i = 0; i < n; i++)
{
j = s[r][i];
if (j > -1)
/* A value of -1 would mean that team i is idle this round. */
{
/* Record that teams i and j played. */
game[i][j] ++;
game[j][i] ++;
/* Note that we will double-count the games, because we record it
when team i plays j, as well as when j plays i. */
}
}
/* Make sure that every pair played exactly once, and nobody ever played
themselves. */
for (i = 0; i < n; i++)
{
if (game[i][i] != 0)
/* If a team plays themselves, it's not a valid schedule. */
{ printf("Team %d played itself.\n", i); return 0; }
for (j = i+1; j < n; j++)
/* We have to check for a 2, because games are double-counted. */
if (game[i][j] != 2)
/* If two teams didn't play exactly one time, it's not valid. */
return 0;
}
/* Make sure that each team plays at most once per round. */
for (r = 0; r < rounds; r++)
{
/* We will use game[0] to count the number of times each team appears in
a round. */
for (i = 0; i < n; i++)
game[0][i] = 0;
/* Count number of times each teams appears in round r. */
for (i = 0; i < n; i++)
{
j = s[r][i];
/* Team i played team j in round r. */
if (j >= 0)
/* -1 means that team i was idle this round. */
game[0][j] ++;
}
/* Make sure each team appears at most once. */
for (i = 0; i < n; i++)
if (game[0][i] > 1)
/* Team i appeared more than once in round r. Not valid. */
return 0;
}
/* Make sure that when team i plays team j, team j also plays team i. */
for (r = 0; r < rounds; r++)
for (i = 0; i < n; i++)
{
j = s[r][i];
/* Team i played team j in round r. */
if (j >= 0)
/* -1 means that team i was idle this round. */
if (s[r][j] != i)
/* If team j didn't play team i, not valid. */
return 0;
}
/* Otherwise, the schedule is valid. */
return 1;
}
答案 0 :(得分:0)
在行中:
if (check(schedule, n))
check
是未声明的标识符。也许这就是你的错误信息实际上所说的。要解决此问题,您需要在文件开头附近添加此行:
int check(int s[MAX][MAX], int n);
你可以在声明roundrobin
。
除此之外,此代码中没有编译错误。