on Leave a Comment

C++ Program to Reverse an Array

This article contains c++ program to reverse array. 

/*C++ Program to Reverse an Array*/

#include<iostream>
using namespace std;
class ARR
{
    int a[10];
    int n;
public:
    void getdata()
    {
        int i;
        cout<<"Enter the length of array (length must be less than or equal to 10) : ";
        cin>>n;
        for(i=0;i<n;i++)
        {
            cout<<"Enter the value of element no "<<i+1<<" : ";
            cin>>a[i];
        }
    }
    void revarray()
    {
        int i,t;
        int l=n-1;
        for(i=0;i<l;i++,l--)
        {
            t=a[i];
            a[i]=a[l];
            a[l]=t;
        }
    }
    void putdata()
    {
        int i;
        for(i=0;i<n;i++)
        {
            cout<<endl<<"Value of element no "<<i+1<<" is "<<a[i];
        }
    }
};
int main()
{
    ARR obj;
    obj.getdata();
    obj.revarray();
    obj.putdata();
    return 0;
}

OUTPUT:

Enter the length of array (length must be less than or equal to 10) : 5
Enter the value of element no 1 : 1
Enter the value of element no 2 : 2
Enter the value of element no 3 : 3
Enter the value of element no 4 : 4
Enter the value of element no 5 : 5

Value of element no 1 is 5
Value of element no 2 is 4
Value of element no 3 is 3
Value of element no 4 is 2
Value of element no 5 is 1

0 comments:

Post a Comment