how to print an array backwards

后端 未结 6 1477
太阳男子
太阳男子 2021-01-17 09:20

The user enteres a number which is put in an array and then the array needs to be orinted backwadrds

int main()
{
    int numbers[5];
    int x;

    for (i         


        
6条回答
  •  轮回少年
    2021-01-17 09:43

    #include
    
    using namespace std;
    int main()
    {
        //get size of the array
        int arr[1000], n;
        cin >> n;
        //receive the elements of the array
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }
        //swap the elements of indexes
        //the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places 
        for (int  i = 0; i*2< n; i++)
        {
            //variable x as a holder for the value of the index 
            int x = arr[i];
            //index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
            arr[i] = arr[n - 1 - i];
            arr[n - 1 - i] = x;
        }
        //loop for printing the new elements
        for(int i=0;i

提交回复
热议问题