-1
# include <iostream>
using namespace std;
const int size=5;

void inputdata(int arr[], int n);    //function prototype
void display(int arr[],int n);    //function prototype
void Reverse(int arr[],int n);    //function prototype


int main()    //start of main function
{
    int list[size];    //array declaration
    inputdata(list ,size);    //fuction call
    display(list,size);     //fuction call         
    Reverse(list,size);    //fuction call



}
void inputdata(int arr[], int n)    //function definition that takes input from user
{
    int index;
    for(index=0;index<n;index++)    //loop to take input from user
    {
        cout<<"Enter element ["<<index<<"]"<<endl;
        cin>>arr[index];
    }
}

void display(int arr[],int n)    //displays the input  
{
    int index;
    for(index=0;index<n;index++)    //loop to display output
    {
        cout<<"Element on ["<<index<<"] is:"<<arr[index]<<endl;
    }
}
void Reverse(int arr[],int n)    //function to find reverse
{
    int i,temp;    //here i have taken a variable temp of integer type for swapping
    for(i=0;i<n/2;i++)
    {
        temp=arr[i];
        arr[i]=arr[n-i-1];
        arr[n-i-1]=arr[i];

    }
    cout<<"the reverse order array is:"<<endl;
    for(i=0;i<n;i++)    //this loop is used to display the reverse order
    {
        cout<<arr[i]<<endl;
    } 
return 0;
}

this above c++ code is meant to find the reverse of the elements of array which is taken as input from user.Input data function is used to take input from the user.Display function is used to display that input.Then there is a function reverse which finds the reverse. But it does not gives proper reverse(output) e.g if i enter 5 array elements as 1,2,3,4,5 its output should be as 5,4,3,2,1.But this results as 5,4,3,4,5.

sehr
  • 31
  • 1
  • 1

1 Answers1

2

Your swap code looks like:

temp=arr[i];
arr[i]=arr[n-i-1];
arr[n-i-1]=arr[i];

But it should be:

temp=arr[i];
arr[i]=arr[n-i-1];
arr[n-i-1]=temp;

A cleaner and simpler option would be to use the swap function in the algorithm library.

scohe001
  • 15,110
  • 2
  • 31
  • 51