Reverse Array Elements Using Function in C++
Advertisements
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
Google Advertisment