Reverse the values of an 1D array in C++



#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
        int ar[11]={19,14,23,22,18,25,17,30,9,17,33}, n=11, i, j, tmp;
cout<<"Original Array:\n";
for(i=0;i<n;i++)
{
cout<<setw(4)<<ar[i];
}
//Logic of Array Reversing.
for(i=0,j=n-1;i<n/2;i++,j--)
{
tmp=ar[i];
ar[i]=ar[j];
ar[j]=tmp;
}
cout<<"\nReversed value of Array:\n";
for(i=0;i<n;i++)
{
cout<<setw(4)<<ar[i];
}
}



No comments

Post your comments

Powered by Blogger.