Reverse Array Elements Using Function in C++

Advertisements

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 Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements