lundi 26 janvier 2015

Generate array of random different numbers

I want to create a program in C which creates N couples (x,y) of random numbers and respects following conditions :



  • N is a random number between 5 and 13;

  • all couples (xi, yi) are different;

  • absolute difference between all x and y elements is at least 2.


I'm kind of breaking my head to solve this problem. I think that I should create three functions :



  1. One function which will count the difference between the x or y element which is calculated and previous ones;

  2. another function to check that all couples are different;

  3. last function to compute the array.


For now I wrote these functions :



int different (int i, int N, int adress)
{
// write something to get the array back from the address of first element
int count = 0;
for (int k=0; k<i; k++)
{
if (array[k][0]=array[i][0] && array[k][1]=array[i][1])
count++;
}
return count;
}


/



int distance (int x, int i, int N, int adress)
{
// write something to get the array back from the address of first element
int count=0;
for (int k = 0; k < i; ++k)
{
if (abs(array[i][x]-array[k][x]) < 2)
count++;
return count;
}
}


/



type coordinates (void)
{
N = rand()%8 + 5;
int array[N][2];
for (int i = 0; i < N; ++i)
{
do
{
int x = rand()%60 - 30;
int y = rand()%60 - 30;
} while (different(i, N, adress)>0 || distance(x, i, N, adress) || distance (y, i, N, adress));
array[i][0] = x;
array[i][1] = y;
}
}


Actually I dont know how to give parameters from one function to another. I think I should use pointers but dont really know how.


If someone can help me, my brain would be happy. Because I try to change my point of view about this problem but there's always something wrong that i can solve.


Thank you in advance! :)





Aucun commentaire:

Enregistrer un commentaire