Show Array Values in Reverse Order C++ (.cpp)
Show Array Values in Reverse Order C++ (.cpp)
“How to Show Array Values in Reverse Order C++ (.cpp)” is the today topic of discussion in this tutorial.
What is reverse order?
Suppose, the user enters the numbers as follows;
Entered numbers: 4, 2, 9, 1
In this case, the reverse order will be looks like;
Reverse Numbers: 9, 4, 2, 1
Now, Let’s see the program.
Show Array Values in Reverse Order in C++ using Arrays
In this example, we have created the two arrays;
int array1[100];
int reverse[100];
Let’s see the “Show Array Values in Reverse Order in C++ using Arrays”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include<iostream> using namespace std; int main() { int array1[100]; int reverse[100]; int size; int num; cout<<"please enter number of values"<<endl; cin>>size; cout<<"please enter values"<<endl; int i=0; do { cin>>array1[i]; i++; } while(i<size); cout<<"reverse of array"<<endl; for(int i=size-1, num=0; i>=0; i--, num++) { reverse[i]=array1[num]; } for(int i=0; i<size; i++) { cout<<" "<<reverse[i]<<" "; } } |
Output
Show Array Values in Reverse Order in C++ using for loop
Let’s see the “Show Array Values in Reverse Order in C++ using for loop”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<iostream> using namespace std; int main() { int array1[100]; int reverse[100]; int size; int num; cout<<"please enter number of values"<<endl; cin>>size; cout<<"please enter values"<<endl; for(int i=0; i<size; i++) { cin>>array1[i]; } cout<<"reverse of array"<<endl; for(int i=size-1, num=0; i>=0; i--, num++) { reverse[i]=array1[num]; } for(int i=0; i<size; i++) { cout<<" "<<reverse[i]<<" "; } } |
Show Array Values in Reverse Order in C++ using While loop
Let’s see the “Show Array Values in Reverse Order in C++ using While loop”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include<iostream> using namespace std; int main() { int array1[100]; int reverse[100]; int size; int num; cout<<"please enter number of values"<<endl; cin>>size; cout<<"please enter values"<<endl; int i=0; while(i<size) { cin>>array1[i]; i++; } cout<<"reverse of array"<<endl; i=size-1; num=0; while(i>=0) { reverse[i]=array1[num]; num++; i--; } i=0; while(i<size) { cout<<" "<<reverse[i]<<" "; i++; } } |
Show Array Values in Reverse Order in C++ using User Define Functions
In this example, we have created the user defines a function. The user define function is declared, defined and call as mentioned below;
Function Declaration: int fun(int, int[], int[]);
Function Definition:
int fun(int size, int array1[100], int ReverseNumbers[100])
{for(int i=size-1, num=0; i>=0; i–, num++)
{
ReverseNumbers[i]=array1[num];
}}
Function Calling: fun(size,array1,ReverseNumbers);
Let’s see the “Show Array Values in Reverse Order in C++ using user define functions”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include<iostream> using namespace std; int fun(int, int[], int[]); int main() { int array1[100]; int ReverseNumbers[100]; int size; int num; cout<<"please enter number of values"<<endl; cin>>size; cout<<"please enter values"<<endl; for(int i=0; i<size; i++) { cin>>array1[i]; } cout<<"ReverseNumbers of array"<<endl; fun(size,array1,ReverseNumbers); for(int i=0; i<size; i++) { cout<<" "<<ReverseNumbers[i]<<" "; } } int fun(int size, int array1[100], int ReverseNumbers[100]) { for(int i=size-1, num=0; i>=0; i--, num++) { ReverseNumbers[i]=array1[num]; } } |
Topic Covered
How to Show Array Values in Reverse Order C++ (.cpp).