Reverse Array Elements Using Function in C++


Prev Tutorial Next Tutorial

C++ Program to Reverse Array Elements Using Function

An array is used to store a collection of data, but it store same type of data. In simple word Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. Here we write simple code for reverse array elements using function in C++.

Reverse Array Elements Using Function in C++

#include<iostream.h>
#include<conio.h>

void Reverse_Array(int array[],int size);

void main()
{
int i,a[10],size;
clrscr();
cout<<"Enter size of Array ( Max:10 ): ";
cin>>size;
cout<<"\nEnter any "<<size<<" elements: \n";

for(i=0; i<size; i++)
{
 cin>>a[i];
}

cout<<"\nStored Data in Array: \n\n";

for(i=0;i<size;i++)
{
cout<<" "<<a[i]<<" ";
}
// Calling Reverse Array Values Function
Reverse_Array(a,size);
cout<< "\n\nReversed Array Elements are: \n\n";
 for(i=0;i<size;i++)
 {
  cout<<" "<<a[i]<<" ";
 }
cout<<"\n";
getch();
}

//------Reverse Array Function---------------

void Reverse_Array(int array[],int size)
{
int temp;
size--;
for (int i=0;size>=i;size--,i++)
{
temp=array[i];
array[i]=array[size];
array[size]=temp;
}
}

Output

Enter size of Array ( Max:10 ): 5
Enter any 5 elements
4
7
8
3
9
Stored Data in Array:
4 7 8 3 9
Reversed Array Elements are:
3 4 7 8 9

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements