Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For BoulderBum

Status
Not open for further replies.

SOLIDKSINGH

Programmer
Nov 28, 2002
3
US
ya it is skol problem......but i don't have much time i have hand it tomorrow....but i did got some code....plese see that .if u can improve ................this is for 6 players and 32 week..........but have to make it in general.......
plese see this code.......

thanks....


#include<iostream.h>
#include<cstdlib>
#include<iomanip.h>
int main()
{
int p1,p2,p3,p4,p5,p6,w1,w2,w3,w4,w5,w6;
int a[32][6]={0},p[6][6]={0};
for(int i=0;i<32;i++)
{
p1 = (rand() % 6+1);
while(1)
{
p2= (rand() % 6+1);

if(p1==p2)
continue;
else
break;

}


while(1)
{
p3 = (rand() % 6+1);
if(p1==p3||p2==p3)
continue;
else
break;
}

while(1)
{
p4 = (rand() % 6+1);
if(p1==p4||p2==p4||p3==p4)
continue;
else
break;
}

while(1)
{
p5 = (rand() % 6+1);
if(p1==p5||p2==p5||p3==p5||p4==p5)
continue;
else
break;
}

while(1)
{
p6 = (rand()% 6+1);
if(p1==p6||p2==p6||p3==p6||p4==p6||p5==p6)
continue;
else
break;
}




a[0]=p1;
a[1]=p2;
a[2]=p3;
a[3]=p4;
a[4]=p5;
a[5]=p6;

cout<<&quot;week&quot;<<i+1<<&quot;:&quot;<<&quot;(&quot;<<a[0]<<&quot;,&quot;<<a[1]<<&quot;)&quot;<<setw(5)<<&quot;(&quot;

<<a[2]<<&quot;,&quot;<<a[3]<<&quot;)&quot;<<setw(5)<<&quot;(&quot;<<a[4]<<&quot;,&quot;<<a[5]<<&quot;)&quot;<<endl;


}


for(int q=0;q<32;q++)
{



w1=a[q][0];
w2=a[q][1];
w3=a[q][2];
w4=a[q][3];
w5=a[q][4];
w6=a[q][5];

p[w1-1][w2-1]=p[w1-1][w2-1]+1;
p[w2-1][w1-1]=p[w2-1][w1-1]+1;
p[w3-1][w4-1]=p[w3-1][w4-1]+1;
p[w4-1][w3-1]=p[w4-1][w3-1]+1;
p[w5-1][w6-1]=p[w5-1][w6-1]+1;
p[w6-1][w5-1]=p[w6-1][w5-1]+1;




}

cout<<setw(7)<<&quot;p1&quot;<<setw(5)<<&quot;p2&quot;<<setw(6)<<&quot;p3&quot;<<setw(4)
<<&quot;p4&quot;<<setw(6)<<&quot;p5&quot;<<setw(5)<<&quot;p6&quot;<<endl;

for(int l=0;l<6;l++)
{
cout<<&quot;p&quot;<<l+1<<setw(5);
for(int d=0;d<6;d++)

cout<<p[l][d]<<setw(5);
cout<<endl;
}
return 0;
}
 
The first step in making your code scalable is to have playerCount and weekCount variables. Hard code their values at first, then mess with making a double scripted array at runtime (which is a bit an undertaking). To start, just declare playerCount, and weekCount variables as const ints that you determine the values of in your code.

Declare the double scripted arrays according to those values, then initialize all of their values to 0.

You should have a currentPlayer variable, and a smallest variable. The currentPlayer will be the one you are trying to find a match for.

Have an
int getSmallest( int[playerCount] )
funtion that searches an array for the smallest number (larger than min) in the array that keeps track of match-ups between players.

When attempting to set up a match, you should have another array that keeps track of which players are already paired up (paired-up array). Starting with the first player, call getSmallest to determine the best match for that player. If player 1's best match is player 2, the use getSmallest for player 2 to determine if player 1 is the best match for the second player as well. If not, search for the next player with the smallest value, and check to see if the match-up works.

When the match-up works, set the appointment, increment the match-up array in the proper position, and flag the paired-up array to indicate you don't need to find a match for that player anymore.

Remember that you have to deal with duplicate values, so be sure to have your main block account for them when making match-ups.

Good luck. I'll help you tomarrow if you have more questions.
 
PS, your loop terminations should be based on the playerCount, and weekCount values.
 
This should get you started...

const int playerCount = 6;
const int weekCount = 20;

int matches[ playerCount ][ playerCount ] = { 0 },
smallest;

...
something I realized is that all of your searches for your match ups should exclude values that pair a player up with himself. For instance, if player 0 needs an opponent, you need to make sure he isn't paired up with player 0. The following function takes this into account.

int getSmallest( int a[ playerCount], const int exclude )
{
int small
j = 0;

while( j != exclude ) //increment if coordinate should not
++j; //be counted

small = a[ j ]; //set it to first possible value

for( int i = 0; i < playerCount; i++ )
{
if ( i != exclude )
if ( a < small )
small = a;
}
return small;
}
 
Of course if ( a < small )
small = a;

should be if ( a < small )
small = a;

It's a tek-tips annoyance to count as italics if you don't uncheck the TGML box. How did it go?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top