Dave_Anderson
asked on
While loop blues
I must be missing something easy, here, but I can't see it offhand.
I'm building a scheduler for a sports select type website. This scheduler will take a certain number of teams, and schedule them to play a set amount of games on various days throughout the season.
I have a two dimensional array of type entry (contains three integer values).
entry Schedule[16][20];
Now when I find out the day the team is scheduled to play it's nth game, I want to check the rest of the array for this team to determine if the day has already been assigned. (denoted by an integer) Here is the code I used:
while (blnLegalDay == FALSE)
{
tmpRand1 = rand();
tmpDay = (tmpRand1 % NumberOfDays) + 1;
Counter = 0;
for (b=0;b<NumberOfGames;b++)
{
if (tmpDay == Schedule[i][b].day)
{
Counter++;
}
}
if (Counter > 0)
{
blnLegalDay = FALSE;
}
else
blnLegalDay = TRUE;
}
It never seems to check the array to make sure the tmpDay equals the day value in the array. blnLegalDay always equates to TRUE, giving me more than one game scheduled on the same day. Any ideas on what I might have missed?
I'm building a scheduler for a sports select type website. This scheduler will take a certain number of teams, and schedule them to play a set amount of games on various days throughout the season.
I have a two dimensional array of type entry (contains three integer values).
entry Schedule[16][20];
Now when I find out the day the team is scheduled to play it's nth game, I want to check the rest of the array for this team to determine if the day has already been assigned. (denoted by an integer) Here is the code I used:
while (blnLegalDay == FALSE)
{
tmpRand1 = rand();
tmpDay = (tmpRand1 % NumberOfDays) + 1;
Counter = 0;
for (b=0;b<NumberOfGames;b++)
{
if (tmpDay == Schedule[i][b].day)
{
Counter++;
}
}
if (Counter > 0)
{
blnLegalDay = FALSE;
}
else
blnLegalDay = TRUE;
}
It never seems to check the array to make sure the tmpDay equals the day value in the array. blnLegalDay always equates to TRUE, giving me more than one game scheduled on the same day. Any ideas on what I might have missed?
Where do you initialize the value in bInLegalDay?
I ask that because if bInLegalDat is TRUE the while loop will be bypassed entirely.
ASKER
It is initialized to FALSE before the loop is entered.
Have you run this in the DEBUGGER?
Sorry, but I don't believe you when you say it's FALSE. Based on what you're saying, the loop is not getting run even once.
Sorry, but I don't believe you when you say it's FALSE. Based on what you're saying, the loop is not getting run even once.
Along those same lines, what is the initial value of NumberOfGames?
There is an awful lot going on here that you've chosen to keep to yourself....
There is an awful lot going on here that you've chosen to keep to yourself....
ASKER
The loop is getting run, I can set a breakpoint in there, and it gets hit at least once. Here is the code that I have: (forgive sloppiness, I'm just trying to get it to work, and will clean it up after)
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
int NumberOfGames = 16;
int NumberOfDays = 20;
int NumberOfTeams = 20;
entry Schedule[20][16];
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHa ndle(NULL) , NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
srand(GetTickCount());
int i, j;
int tmpOpponent, tmpDay;
int tmpRand;
int tmpRand1;
BOOL blnLegalTeam = FALSE;
BOOL blnLegalDay = FALSE;
int TeamCounter;
int a,b;
int x,y;
// initialize array
for (x=0;x<NumberOfTeams;x++)
{
for (y=0;y<NumberOfGames;y++)
{
Schedule[x][y].day = -1;
Schedule[x][y].opponent_nu mber = -1;
Schedule[x][y].team_number = -1;
}
}
for (i = 0; i < NumberOfTeams; i++) {
for (j = 0; j < NumberOfGames; j++)
{
blnLegalTeam = FALSE;
blnLegalDay = FALSE;
if (Schedule[i][j].day != -1)
continue;
while (blnLegalTeam == FALSE)
{
tmpRand = rand();
tmpOpponent = (tmpRand % NumberOfTeams) + 1;
TeamCounter = 0;
if (tmpOpponent == i)
{
blnLegalTeam = FALSE;
continue;
}
for (a=1;a<NumberOfGames;a++)
{
if (tmpOpponent == Schedule[i][a].opponent_nu mber)
{
TeamCounter++;
}
}
if (TeamCounter > (NumberOfGames/NumberOfTea ms + 1))
{
blnLegalTeam = FALSE;
}
else
blnLegalTeam = TRUE;
}
while (blnLegalDay == FALSE)
{
tmpRand1 = rand();
tmpDay = (tmpRand1 % NumberOfDays) + 1;
TeamCounter = 0;
for (b=0;b<NumberOfGames;b++)
{
if (tmpDay == Schedule[i][b].day)
{
TeamCounter++;
}
}
if (TeamCounter > 0)
{
blnLegalDay = FALSE;
}
else
blnLegalDay = TRUE;
}
Schedule[i][j].team_number = i;
Schedule[i][j].opponent_nu mber = tmpOpponent;
Schedule[i][j].day = tmpDay;
Schedule[tmpOpponent][j].t eam_number = tmpOpponent;
Schedule[tmpOpponent][j].o pponent_nu mber = i;
Schedule[tmpOpponent][j].d ay = tmpDay;
}
}
// display schedule to verify no duplicates
for (x=1;x<=NumberOfTeams;x++)
{
for (y=1;y<=NumberOfGames;y++)
{
cout << "Day: " << Schedule[x][y].day << endl;
cout << "Opponent: " << Schedule[x][y].opponent_nu mber << endl;
cout << "Team #" << Schedule[x][y].team_number << endl;
}
}
}
return nRetCode;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
int NumberOfGames = 16;
int NumberOfDays = 20;
int NumberOfTeams = 20;
entry Schedule[20][16];
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHa
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
srand(GetTickCount());
int i, j;
int tmpOpponent, tmpDay;
int tmpRand;
int tmpRand1;
BOOL blnLegalTeam = FALSE;
BOOL blnLegalDay = FALSE;
int TeamCounter;
int a,b;
int x,y;
// initialize array
for (x=0;x<NumberOfTeams;x++)
{
for (y=0;y<NumberOfGames;y++)
{
Schedule[x][y].day = -1;
Schedule[x][y].opponent_nu
Schedule[x][y].team_number
}
}
for (i = 0; i < NumberOfTeams; i++) {
for (j = 0; j < NumberOfGames; j++)
{
blnLegalTeam = FALSE;
blnLegalDay = FALSE;
if (Schedule[i][j].day != -1)
continue;
while (blnLegalTeam == FALSE)
{
tmpRand = rand();
tmpOpponent = (tmpRand % NumberOfTeams) + 1;
TeamCounter = 0;
if (tmpOpponent == i)
{
blnLegalTeam = FALSE;
continue;
}
for (a=1;a<NumberOfGames;a++)
{
if (tmpOpponent == Schedule[i][a].opponent_nu
{
TeamCounter++;
}
}
if (TeamCounter > (NumberOfGames/NumberOfTea
{
blnLegalTeam = FALSE;
}
else
blnLegalTeam = TRUE;
}
while (blnLegalDay == FALSE)
{
tmpRand1 = rand();
tmpDay = (tmpRand1 % NumberOfDays) + 1;
TeamCounter = 0;
for (b=0;b<NumberOfGames;b++)
{
if (tmpDay == Schedule[i][b].day)
{
TeamCounter++;
}
}
if (TeamCounter > 0)
{
blnLegalDay = FALSE;
}
else
blnLegalDay = TRUE;
}
Schedule[i][j].team_number
Schedule[i][j].opponent_nu
Schedule[i][j].day = tmpDay;
Schedule[tmpOpponent][j].t
Schedule[tmpOpponent][j].o
Schedule[tmpOpponent][j].d
}
}
// display schedule to verify no duplicates
for (x=1;x<=NumberOfTeams;x++)
{
for (y=1;y<=NumberOfGames;y++)
{
cout << "Day: " << Schedule[x][y].day << endl;
cout << "Opponent: " << Schedule[x][y].opponent_nu
cout << "Team #" << Schedule[x][y].team_number
}
}
}
return nRetCode;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I didnt' see your post. Also I see that I forgot to insert:
return(-1); // no days avail for team
before the final curly brace to indicate no days avail.
-- Dan
return(-1); // no days avail for team
before the final curly brace to indicate no days avail.
-- Dan
ASKER
It's sloppy, but it now works. I used DanRollins' suggestion and created a function. I then changed the while loop to read:
while (blnLegalDay == FALSE)
{
blnLegalDay = IsDayAvailForTeam(i, tmpDay);
}
I also created a second struct:
typedef struct taken
{
int day;
bool istaken;
} taken;
and created an array DateChecker[NumberOfTeams] [365];
and in IsDayAvailForTeam() I checked the team and date, and it now works. So thank you all for helping, I really appreciate it.
Dave
while (blnLegalDay == FALSE)
{
blnLegalDay = IsDayAvailForTeam(i, tmpDay);
}
I also created a second struct:
typedef struct taken
{
int day;
bool istaken;
} taken;
and created an array DateChecker[NumberOfTeams]
and in IsDayAvailForTeam() I checked the team and date, and it now works. So thank you all for helping, I really appreciate it.
Dave