简体   繁体   English

生成随机不同数字的数组

[英]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 : 我想在C中创建一个程序,该程序创建N对(x,y)随机数,并遵循以下条件:

  • N is a random number between 5 and 13; N是5到13之间的随机数;
  • all couples (xi, yi) are different; 所有夫妇(xi,yi)都不一样;
  • absolute difference between all x and y elements is at least 2. 所有x和y元素之间的绝对差至少为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; 一个函数将计算所计算的x或y元素与先前元素之间的差;
  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! 先感谢您! :) :)

  1. Change 5 to 13 generator: 5,6,7,8,9,10,11,12,13 is 9 numbers 更改5到13生成器:5,6,7,8,9,10,11,12,13是9个数字

     // N = rand()%8 + 5; N = rand()%9 + 5; 
  2. Do compare, not assignment in different () 做比较,不要在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. 如果适合您的样式,请对该结构进行Typedef定义。 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 然后,而不是使一个复杂while -condition只是,写这样一个函数

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. 在此函数内部,您可以坚持使用函数,但是目前看来,正确的C语法存在一些问题。 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. 您可以将数组直接传递给此函数,而无需获取其地址或任何类似内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM