Site icon T4Tutorials.com

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”.

#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

Figure: How to Show Array Values in Reverse Order C++ (.cpp)

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”.

#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”.

#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”.

#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).

Exit mobile version