Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在C中更改数组中的数字时出现问题_Arrays_C - Fatal编程技术网

Arrays 在C中更改数组中的数字时出现问题

Arrays 在C中更改数组中的数字时出现问题,arrays,c,Arrays,C,首先我生成一些随机数,然后我需要像下面几行描述的那样交换它们。 我用iut试过了,因为我是疯子 我必须交换阵列的号码 更改编号1和2,3和4,…29和30 更改编号1和3、4和7、…27和30 谢谢你的帮助 srand(时间(空)); 对于(i=0;i

首先我生成一些随机数,然后我需要像下面几行描述的那样交换它们。 我用iut试过了,因为我是疯子

我必须交换阵列的号码

  • 更改编号1和2,3和4,…29和30
  • 更改编号1和3、4和7、…27和30 谢谢你的帮助

  • srand(时间(空));
    对于(i=0;i
    这似乎简单得多:

    声明辅助函数:

    void swap_int(int* x, int *y)
    {
        int tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    然后在需要洗牌数组的代码中:

    int i, j; // declare this up top with all your other variable declarations
    
    // for each pair of elements swap them
    for (i=0, j=1; j < SIZE; i+=2, j+=2)
    {
       swap_int(&mainArray[i], &mainArray[j]);
    }
    
    // swap array[0] with array[2], then swap array[3] with array[5], etc...
    for (i=0, j=2; j < SIZE; i+=3, j+=3)
    {
       swap_int(&mainArray[i], &mainArray[j]);
    }
    
    inti,j;//将其与所有其他变量声明一起声明
    //对于每对元素,交换它们
    对于(i=0,j=1;j
    此代码与您要执行的操作有何关联?您提到的数字是否已存在于阵列中?你说你需要“改变”这些数字?你需要把它们改成什么?有随机数,我需要交换它们。请发一个帖子,你说你需要交换这些数字吗?你需要用什么来交换它们?
    int i, j; // declare this up top with all your other variable declarations
    
    // for each pair of elements swap them
    for (i=0, j=1; j < SIZE; i+=2, j+=2)
    {
       swap_int(&mainArray[i], &mainArray[j]);
    }
    
    // swap array[0] with array[2], then swap array[3] with array[5], etc...
    for (i=0, j=2; j < SIZE; i+=3, j+=3)
    {
       swap_int(&mainArray[i], &mainArray[j]);
    }