當前位置:編程學習大全網 - 編程軟體 - C++編程:將壹個數組中的值按逆序重新存放,例如,原來順序為:8,6,5,4,1。要求改為:1,4,5,6,8。

C++編程:將壹個數組中的值按逆序重新存放,例如,原來順序為:8,6,5,4,1。要求改為:1,4,5,6,8。

這樣寫擴展性會更好,數組中的個數可以是任意的,只需修改arrSize 即可

#include <iostream>

using namespace std;

int main()

{

unsigned int i = 0, j = 0, t = 0;

const unsigned int arrSize = 5;

int array[arrSize];

cout<<"enter the origil array:"<<endl;

for(i = 0; i < arrSize; i++)

{

cin >> array[i];

}

for (i = 0, j = 0; j < arrSize /2; ++i, ++j)

{

t = array[i];

array[i] = array[arrSize - 1 - j];

array[arrSize - 1 - j] = t;

}

cout<<"the opposite array:"<<endl;

for(i = 0; i < arrSize; i++)

cout<<array[i]<<" ";

system("pause");

return 0;

}

  • 上一篇:為什麽要學linux網絡編程知乎
  • 下一篇:worker線程最大同時激活數是多少
  • copyright 2024編程學習大全網