here is the full program:
#include
#include
#include
#include
int home[6],away[6], used[6];
int count=0, loop=-1, verifyer;
void CheckMatches(void);
/* Randomizes Matches */
int main(void)
{
randomize();
for(; count <= 6; count++)<br>
{
do
{
home[count] = random (14);
away[count] = random (14);
loop++;
used[loop] = home[count];
loop++;
used[loop] = away[count];
CheckMatches();
if (home[count] == away[count]) loop = loop - 2;
}
while ((home[count] == away[count]) || (verifyer == 0));
printf(" %d plays %d
",home[count], away[count]);
}
loop = getch();
return 0;
}
void CheckMatches(void)
{
int round=0;
for (; round <= count; round++)<br>
{
if (home[count] == used[round])
verifyer = 1;
else
if (away[count] == used[round])
verifyer = 1;
}
}
Can you help?
Michael I am looking in your direction here.
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
If you want to generate a sequence of random numbers (integers I'm assuming) and if the range of said numbers is small, I would just do something like this:
#define COUNT 14
int num[COUNT];
for(int i=0;i
{
// Yes, these braces aren't needed but the
// webpage loses the formatting
num[i]=i;
}
// Now scramble the array of numbers
int j,k,m;
#define MIX COUNT*4 // or whatever you find better
srand((unsigned)time(NULL));
for(i=0;i
{
j=rand(COUNT)
k=rand(COUNT)
m=num[j];
num[j]=num[k];
num[k]=m;
}
// Now with the array sufficeintly mixed you can
// get the random numbers by sequentially
// indexing the array
// you could use odd indices for home, and even
// indices for away
for(i=0;i
{
printf("%i plays %i
",num[i],num[i+1]);
}
The other way to do it is like this (similar to what you did except I only use one array):
#define COUNT 14
#define MAX_RANDOM 14 // or whatever your range is
srand((unsigned)time(NULL));
int j,k;
for(int i=0;i
{
do
{
j=rand(COUNT)
for(k=0;k
{
if(j==num[k])
{
j=-1;
continue;
}
}
}while(j<0)<br>
num[i]=j;
}
// Now again, I'd just use odd indices for home
// and even indices for away
for(i=0;i
{
printf("%i plays %i
",num[i],num[i+1]);
}
Mastaba
If you want to generate a sequence of random numbers (integers I'm assuming) and if the range of said numbers is small, I would just do something like this:
#define COUNT 14
int num[COUNT];
for(int i=0;i
{
// Yes, these braces aren't needed but the
// webpage loses the formatting
num[i]=i;
}
// Now scramble the array of numbers
int j,k,m;
#define MIX COUNT*4 // or whatever you find better
srand((unsigned)time(NULL));
for(i=0;i
{
j=rand(COUNT)
k=rand(COUNT)
m=num[j];
num[j]=num[k];
num[k]=m;
}
// Now with the array sufficeintly mixed you can
// get the random numbers by sequentially
// indexing the array
// you could use odd indices for home, and even
// indices for away
for(i=0;i
{
printf("%i plays %i
",num[i],num[i+1]);
}
The other way to do it is like this (similar to what you did except I only use one array):
#define COUNT 14
#define MAX_RANDOM 14 // or whatever your range is
srand((unsigned)time(NULL));
int j,k;
for(int i=0;i
{
do
{
j=rand(COUNT)
for(k=0;k
{
if(j==num[k])
{
j=-1;
continue;
}
}
}while(j<0)<br>
num[i]=j;
}
// Now again, I'd just use odd indices for home
// and even indices for away
for(i=0;i
{
printf("%i plays %i
",num[i],num[i+1]);
}
Mastaba
There are several approaches to generating sequences of unique random numbers. Your approach is close to one of them but seems to be too much.
If you want to generate a sequence of random numbers (integers I'm assuming) and if the range of said numbers is small, I would just do something like this:
#define COUNT 14
int num[COUNT];
for(int i=0;i < COUNT;i++)
{
// Yes, these braces aren't needed but the
// webpage loses the formatting
num[i]=i;
}
// Now scramble the array of numbers
int j,k,m;
#define MIX COUNT*4 // or whatever you find better
srand((unsigned)time(NULL));
for(i=0;i < MIX;i++)
{
j=rand(COUNT)
k=rand(COUNT)
m=num[j];
num[j]=num[k];
num[k]=m;
}
// Now with the array sufficeintly mixed you can
// get the random numbers by sequentially
// indexing the array
// you could use odd indices for home, and even
// indices for away
for(i=0;i < COUNT;i++)
{
printf("%i plays %i
",num[i],num[i+1]);
}
The other way to do it is like this (similar to what you did except I only use one array):
#define COUNT 14
#define MAX_RANDOM 14 // or whatever your range is
srand((unsigned)time(NULL));
int j,k;
for(int i=0;i < COUNT;i++)
{
do
{
j=rand(COUNT)
for(k=0;k < i;k++)
{
if(j==num[k])
{
j=-1;
continue;
}
}
}while(j < 0)
num[i]=j;
}
// Now again, I'd just use odd indices for home
// and even indices for away
for(i=0;i < COUNT;i++)
{
printf("%i plays %i
",num[i],num[i+1]);
}
Mastaba
j=rand(COUNT);
It should be:
j=rand(MAX_RANDOM);
j=rand(COUNT);
It should be:
j=rand(MAX_RANDOM);