Wednesday 14 January 2015

Cpp Program to reverse all the elements in an array.

This is a Cpp Program which reverses all the elements present in an array.

Here we read  all the elements from the array and again read them from last element and print them back.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int i,n,d,c;
    
    cout << "\nEnter the no of Elements: \t";
    cin >> n;
    int a[n],b[n];
    for(i=0;i<n;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> a[i];
    }
    cout << "\nOriginal array is :\t";
    for(i=0;i<n;i++)
    cout << a[i] << "\t";
    for (c = n - 1, d = 0; c >= 0; c--, d++)
        b[d] = a[c];
    
    
    for (c = 0; c < n; c++)
        a[c] = b[c];
    
    cout << "\n\nReversed array is :\t";
    
    for (c = 0; c < n; c++)
        cout << a[c] << "\t";
    return 0;
}

OUTPUT : 
Cpp - Reverse the elements in an array

No comments:

Post a Comment