I'm making a basic football (soccer) manager game as my school project. Most of the other stuff is sorted out, but the scheduler is not working...**Background:**

I'm simulating the English Premier League,which has 20 teams. Each team plays 38 matches (exactly one match per week over 38 weeks) in a double round robin format.There are 20 teams, so 10 matches are played every week. First each team plays the other exactly once over the first 19 weeks, then the whole thing repeats it self.

I'm scheduling the first 19 weeks and then duplicating the schedule for the next 19 weeks.

I've written the code for the first 19 weeks (as soon as it works I'll write the code to duplicate the the next 19):

I HAVE TO use Turbo C++ to compile it (as per the school rules/syllabus)... hence the old headers..

```
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int main()
{
unsigned int seedval;
time_t ti;
seedval =(unsigned)time(&ti);
srand( time(&ti) );
int matchlist[21][39]; //[Team id][Week number]
for (int i=1;i<=20;i++)
{
for (int j=1;j<=38;j++)
{
matchlist[i][j]=0;
}
}
cout<<"Schedule init completed";
int t;
int flag;
for (int i=1;i<=20;i++)
{
for (int j=1;j<=19;j++)
{
if ((matchlist[i][j]==0))
{
flag=1;//So that the while loop is run atleast once
while (flag==1)
{
flag=0;
t=rand()%20 + 1; //Generate random opponent 't' for team 'i' on week 'j'
if (t==i)
{
flag=1;
}
if (flag==0)
{
for (int p=1;p<=20;p++) //Check if team 't' is playing anyone else the same week..
{
if (matchlist[p][j]==t)
{
flag=1;
break;
}
}
}
if (flag==0)
{
for (int z=1;z<=19;z++) //Check if same teams are playing each other more than once in the 19 weeks
{
if (matchlist[i][z]==t)
{
flag=1;
break;
}
}
}
if (flag==0)
{
matchlist[i][j]=t;//Team i plays t
matchlist[t][j]=i;//So Team t plays i
}
}
}
}
}
cout<<"Scheduled";
int listed[21];
//LOOP TO DISPLAY Week-wise Schedule (first five weeks):
for (int i=1;i<=5;i++)
{
for (int z=1;z<=20;z++)
{
listed[z]=0;
}
cout<<"\n\nWeek "<<i<<"\n\n";
for (int j=1;j<=20;j++)
{
if (listed[j]==0)//One match should be displayed just once
{
cout<<"\n\n"<<j<<" "<<matchlist[j][i];
listed[j]=1;
listed[matchlist[j][i]]=1;
}
}
}
}
```

The code is compiling , but the loop on line 59 is leading to an infinte loop or some other problem .. I guess it's some type of *logical* error.

The program runs completely without that particular loop.. but without it, the same match may get scheduled again for other weeks. This musn't happen as each team plays the other exactly once..

Appreciate any help in identifying the mistake in the code..