Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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/4/c/64.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_Sorting - Fatal编程技术网

Arrays C数组的更新

Arrays C数组的更新,arrays,c,sorting,Arrays,C,Sorting,关于这一问题— 出于某种原因,如果我不更新第二个数组中的元素,第一个数组中的元素就不会相应地更新 int array[] = {1, 34, 3}; int newArrayWithSamePower[3] = {11, 34, 33}; // Sort initial array based on power array elements for (int i = 0; i <= 2; i++) { for (int j = i + 1; j <= 2; j++) {

关于这一问题—

出于某种原因,如果我不更新第二个数组中的元素,第一个数组中的元素就不会相应地更新

int array[] = {1, 34, 3};

int newArrayWithSamePower[3] = {11, 34, 33};

// Sort initial array based on power array elements
for (int i = 0; i <= 2; i++) {
    for (int j = i + 1; j <= 2; j++) {
        if (newArrayWithSamePower[j] > newArrayWithSamePower[i]) {
            int temp2 = newArrayWithSamePower[i];
            newArrayWithSamePower[i] = newArrayWithSamePower[j];
            newArrayWithSamePower[j] = temp2;

            // Unsure why array indexes go haywire if you dont update power array
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            
        }
    }
}

for (int i = 0; i <= tempIndex; i++) {
    printf("%d\n", array[i]);
}
int数组[]={1,34,3};
int newarraywhithsamepower[3]={11,34,33};
//基于幂数组元素对初始数组进行排序
对于(int i=0;i

如果不在循环中交换
newarraywhithsamepower
数组的元素,则在不同索引上使用
If(newarraywhithsamepower[j]>newarraywhithsamepower[i])
条件将为true,因此
数组
将以不同的方式进行交换。算法可以正常工作-它必须修改元素才能正常工作。

如果要更新名为
数组
的数组,为什么要在
If
语句中检查
powerArray
中的元素?使用
tempIndex
看起来非常困难d、 …请对此表示适当的歉意,我对这个平台非常陌生@AnttiHaapalai,我正试图根据第二个数组中元素的大小,根据它们的索引@LazarĐ或đević@JeraldLim对数组进行排序,请回答这个问题,并在其中提供一个完整的独立示例。
    if (newArrayWithSamePower[j] > newArrayWithSamePower[i]) {
        int temp2 = newArrayWithSamePower[i];
        newArrayWithSamePower[i] = newArrayWithSamePower[j];
        newArrayWithSamePower[j] = temp2;