c++ - 反向数组不按顺序

标签 c++ arrays swap

<分区>

我试图反转数组中的元素。例如,我得到:

    15 69 94 52 97 51 17 18 50 18

它会在:

    18 50 18 17 51 97 52 94 69 15 (which is reversed)

但是,这是我从我的代码中得到的:

    51 69 94 52 97 17 18 50 18 15 (the sequence are jumbled out which I have no idea why)

这是我的代码:

     void reverse(int num_array[], const int& size);
int main ()
{
const int size = 10;
int num_array[size];

srand (time(NULL));

for (int count = 0; count< size ; count++){
    /* generate secret number between 1 and 100: */
    num_array[count] = rand() % 100 + 1;
    cout << num_array[count] << " " ;
}

reverse(num_array,size);

cout << "\n\n" ;
for(int index = 0; index< size; index++){
    cout << num_array[index] << " " ;
}

cout << endl;

system("PAUSE");
return 0;
}

void reverse(int num_array[], const int& size)
{
for (int count =0; count< size/2; count++){
    int temp = num_array[0];
    num_array[0] = num_array[size-count-1];
    num_array[size-count-1] = temp;
}
}

我猜我的反向方法有问题。有人可以修复它吗?

提前致谢。

最佳答案

我猜你是说 num_array[count] 而不是 num_array[0]

关于c++ - 反向数组不按顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359576/

相关文章:

c++ - 我该如何表达呢?

c++ - 如何使用 main() 构建支持多个目标的 makefile

c++ - OpenGL实际上是如何读取索引的?

python - 如何在一维数组中找到峰

Python 值交换什么都不做

c++ - 具有自动生成源的 Cmake 可执行文件

arrays - 如何使用 NSUserDefaults 在 Swift 中保存 Int 数组?

arrays - Angular 2 pipe - 计算对象数组的摘要

linux - 交换分区中的读取错误如何影响正在运行的进程?

go - 如何在Golang中按索引交换值