I want to create a program in C which creates N couples (x,y) of random numbers and respects following conditions :
I'm kind of breaking my head to solve this problem. I think that I should create three functions :
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! :)
Change 5 to 13 generator: 5,6,7,8,9,10,11,12,13 is 9 numbers
// N = rand()%8 + 5; N = rand()%9 + 5;
Do compare, not assignment in different ()
// if (array[k][0]=array[i][0] && array[k][1]=array[i][1]) if (array[k][0]==array[i][0] && array[k][1]==array[i][1])
First of all I would recommend doing something like this (although it is not mandatory):
struct complex_number {
int real;
int img;
};
Typedef this struct, if it suits your style. Generate one complex value at a time, the way you already do it. Then, instead of making a complex while
-condition just, write a function like
int is_candidate(struct complex_number *array, size_t size, struct complex_number candidate)
where you collect all the logic that determines whether a number is a candidate.
Inside this function you can stick to your functions, but you seem to have some problems with the right C syntax at the moment. There are two major issues: you never pass the actual array, and you use =
for comparison, which is assignment, where you should use ==
.
Just as example:
//impelement your functions - left out
int is_same(struct complex_number c1, struct complex_number c2);
int check_distance(struct complex_number c1, struct complex_number c2);
int is_candidate(struct complex_number *array, size_t size, struct complex_number candidate){
int i;
for (i = 0; i < size; ++i){
if (is_same(array[i], candidate)
|| !(check_distance(array[i], candidate)){
return false;
}
}
return true;
}
You could pass the candidate by pointer, but in this case the struct is so small that it is probably not worth the effort. If you candidate is valid, you can append it to the back of the array. You can see how to declare an array as function argument in this example (array) and how it is necessary to pass the size along. This information is not associated with the array. You can pass an array directly into this function without taking its address or anything of the likes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.