Tuesday, 8 March 2011

Reverse the content of the array using C++

#include <iostream.h>

#define ARR_SIZE 11

int main()
{
    int arr[ARR_SIZE] = {1,2,3,4,5,6,7,8,9,0,11};
    int i = 0;

    // Dispay the content of the array initially
    cout<<"Array content as input"<<endl;
    for(i=0;i<ARR_SIZE;i++)
        cout<<arr[i]<<"\t";
    cout<<endl;

    // Swap the array elements each from the first and the last.
    // It handles automatically the odd and the even no of elements
    for(i=0;i<ARR_SIZE/2;i++)
    {
        int temp = arr[i];
        arr[i] = arr[ARR_SIZE - i-1];
        arr[ARR_SIZE - i-1] = temp;
    }

    // Dispay the content of the array after the swap.
    cout<<"Array content as output"<<endl;
    for(i=0;i<ARR_SIZE;i++)
        cout<<arr[i]<<"\t";
    cout<<endl;

    return 0;
}

No comments:

Post a Comment