生成随机不同数字的数组
Generate array of random different numbers
我想用 C 创建一个程序,它创建 N 对随机数 (x,y) 并遵守以下条件:
- N为5到13之间的随机数;
- 所有对(xi,yi)都不一样;
- 所有 x 和 y 元素之间的绝对差值至少为 2。
我有点想解决这个问题。
我认为我应该创建三个函数:
- 一个函数,计算计算出的 x 或 y 元素与之前的元素之间的差异;
- 检查所有情侣是否不同的另一个功能;
- 计算数组的最后一个函数。
现在我写了这些函数:
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;
}
}
实际上我不知道如何将一个函数的参数传递给另一个函数。我想我应该使用指针,但我真的不知道该怎么做。
如果有人能帮助我,我的大脑会很高兴。因为我试图改变我对这个问题的看法,但总有一些错误我可以解决。
提前致谢! :)
将5改为13生成器:5,6,7,8,9,10,11,12,13为9个数
// N = rand()%8 + 5;
N = rand()%9 + 5;
在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])
首先我建议做这样的事情(虽然这不是强制性的):
struct complex_number {
int real;
int img;
};
Typedef 这个结构,如果它适合你的风格。一次生成一个复杂的值,就像您已经这样做的那样。然后,不要只写一个复杂的 while
-condition,而是写一个像
这样的函数
int is_candidate(struct complex_number *array, size_t size, struct complex_number candidate)
您可以在其中收集确定某个数字是否为候选项的所有逻辑。
在此函数中,您可以坚持使用您的函数,但目前您似乎对正确的 C 语法有一些问题。有两个主要问题:你从来没有传递实际的数组,你使用 =
进行比较,也就是赋值,你应该使用 ==
.
举个例子:
//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;
}
您可以通过指针传递候选人,但在这种情况下,结构太小了,可能不值得付出努力。如果你的候选人是有效的,你可以将它追加到数组的后面。您可以在此示例(数组)中了解如何将数组声明为函数参数,以及如何传递大小。此信息与阵列无关。
您可以将数组直接传递给此函数,而无需获取其地址或任何类似内容。
我想用 C 创建一个程序,它创建 N 对随机数 (x,y) 并遵守以下条件:
- N为5到13之间的随机数;
- 所有对(xi,yi)都不一样;
- 所有 x 和 y 元素之间的绝对差值至少为 2。
我有点想解决这个问题。 我认为我应该创建三个函数:
- 一个函数,计算计算出的 x 或 y 元素与之前的元素之间的差异;
- 检查所有情侣是否不同的另一个功能;
- 计算数组的最后一个函数。
现在我写了这些函数:
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;
}
}
实际上我不知道如何将一个函数的参数传递给另一个函数。我想我应该使用指针,但我真的不知道该怎么做。
如果有人能帮助我,我的大脑会很高兴。因为我试图改变我对这个问题的看法,但总有一些错误我可以解决。
提前致谢! :)
将5改为13生成器:5,6,7,8,9,10,11,12,13为9个数
// N = rand()%8 + 5; N = rand()%9 + 5;
在
中进行比较,而不是赋值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])
首先我建议做这样的事情(虽然这不是强制性的):
struct complex_number {
int real;
int img;
};
Typedef 这个结构,如果它适合你的风格。一次生成一个复杂的值,就像您已经这样做的那样。然后,不要只写一个复杂的 while
-condition,而是写一个像
int is_candidate(struct complex_number *array, size_t size, struct complex_number candidate)
您可以在其中收集确定某个数字是否为候选项的所有逻辑。
在此函数中,您可以坚持使用您的函数,但目前您似乎对正确的 C 语法有一些问题。有两个主要问题:你从来没有传递实际的数组,你使用 =
进行比较,也就是赋值,你应该使用 ==
.
举个例子:
//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;
}
您可以通过指针传递候选人,但在这种情况下,结构太小了,可能不值得付出努力。如果你的候选人是有效的,你可以将它追加到数组的后面。您可以在此示例(数组)中了解如何将数组声明为函数参数,以及如何传递大小。此信息与阵列无关。 您可以将数组直接传递给此函数,而无需获取其地址或任何类似内容。