- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Reverse an Array
This article will teach you how to reverse an array entered by the user during runtime in a C++ program. Here is the list of programs for reversing an array:
- Print an array's inverse without actually reversing it
- Reverse an array, and then print
- Reverse an array using the pointer
- Reverse an array using a user-defined function
Print Reverse of an Array in C++
This program just prints reverse of an array, without actually reversing it. The question is, write a program in C++ to print reverse of an array. Here is its answer:
#include<iostream> using namespace std; int main() { int arr[10], i; cout<<"Enter 10 Array Elements: "; for(i=0; i<10; i++) cin>>arr[i]; cout<<"\nThe Original Array is:\n"; for(i=0; i<10; i++) cout<<arr[i]<<" "; cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=(10-1); i>=0; i--) cout<<arr[i]<<" "; cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply 10 numbers as 10 elements for an array, one by one, to print its reverse as shown in the sample output given below:
When user enters any 10 numbers say 1, 2, 3, ..., 10, then these numbers gets stored in arr[] in following way:
- arr[0]=1
- arr[1]=2
- arr[2]=3
- and so on up until
- arr[9]=10
We printed the array from last to first, that is, from 9th to 0th index, to demonstrate the reverse of an array as output.
Reverse an Array in C++
This program reverses an array before printing. When the array gets reversed, then when you print the same array, it shows its elements in reverse order. This program also allows the user to define the size of the array.
To reverse an array in C++ programming, you have to ask the user to enter the array's size and then add elements (of the given size) to the array. Now to reverse, do these things:
- Move the element at the first index to the last, and the element at the last index to the first.
- Move the element at the second index to the second last, and the element at the second last index to the second.
- and so on.
Let's have a look at the program first; its explanation is given later on:
#include<iostream> using namespace std; int main() { int arr[100], tot, i, j, temp; cout<<"Enter the Size for Array: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; cout<<"\nThe Original Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; j = tot-1; for(i=0; i<j; i++, j--) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; cout<<endl; return 0; }
Here is its sample run, with the following user input:
- 5 in size
- 1, 2, 3, 4, 5 as five array elements.
After supplying exactly these inputs, press the ENTER key to reverse the array and print both the original and its reverse, as shown in the snapshot given below:
The following is the dry run of the above C++ program with the same user input (as provided in the previous output):
- tot = 5 at the start. Since I've already told about storing array elements. So arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, and arr[4] = 5.
- Now the original array gets printed using a for loop.
- After printing the original array, tot-1 or 5-1 or 4 gets initialized to j. So j=4
- Now 0 gets initialized to i, and the condition i<j or 0<4 evaluates to be true. Therefore, program flow goes inside the loop.
- And arr[i] or arr[0] or 1 is set to temp. Then arr[j] or arr[4] or 5 gets initialized to arr[i] or arr[0]. Finally, temp or 1 is assigned to arr[j] or arr[4].
- A normal swap operation gets performed with three lines of code. Now arr[0] = 5 and arr[4] = 1. As you can see, the element at the last index (4th index) gets moved to the first index (0th index), and the element at the first (0th) index gets moved to the last (4th) index.
- Now the program flow goes to the update part of the for loop, and the value of i gets incremented, whereas the value of j gets decremented. So i = 1 and j = 3.
- And then the condition gets evaluated again. Because the condition i<j or 1<3 evaluates to be true again, program flow goes inside the loop again.
- This process continues until the condition is evaluated as false.
- In this way, the array gets reversed. So print the array using another for loop to show the reverse of the given array.
Reverse an Array using Pointers in C++
Now let's reverse an array using a pointer. We've created a total of three programs using pointers to reverse an array given by the user. This program uses pointers everywhere, from printing to reversing an array:
#include<iostream> using namespace std; int main() { int arr[100], tot, i, arrTemp[100], *ptr; cout<<"Enter the Size for Array: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; ptr = &arr[0]; cout<<"\nThe Original Array is:\n"; for(i=0; i<tot; i++) { cout<<*ptr<<" "; ptr++; } ptr--; for(i=0; i<tot; i++) { arrTemp[i] = *ptr; ptr--; } ptr = &arrTemp[0]; for(i=0; i<tot; i++) { arr[i] = *ptr; ptr++; } ptr = &arr[0]; cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=0; i<tot; i++) { cout<<*ptr<<" "; ptr++; } cout<<endl; return 0; }
Here is its sample run with user input, 6 as size, and 10, 20, 30, 40, 50, and 60 as its six elements:
Note: The * is called as the value at operator, whereas & is called as the address of operator.
Note: The ptr++ initializes the address of the next index. For example, if ptr holds the address of the 0th index of an array arr, then after executing ptr++, ptr now holds the address of the 1st index of the same array arr.
Here is another program using a pointer to reverse an array. In this program, the pointer is used only to reverse the array. The printing of the original and reversed arrays is done normally:
#include<iostream> using namespace std; int main() { int arr[100], tot, i, arrTemp[100], *ptr; cout<<"Enter the Size for Array: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; cout<<"\nThe Original Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; ptr = &arr[tot-1]; for(i=0; i<tot; i++) { arrTemp[i] = *ptr; ptr--; } ptr = &arrTemp[0]; for(i=0; i<tot; i++) { arr[i] = *ptr; ptr++; } cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; cout<<endl; return 0; }
This program produces the same output as the previous program. Now here is the last program that reverses an array using a pointer. This program is based on swapping operations. This program doesn't use any other array like the previous program:
#include<iostream> using namespace std; int main() { int arr[100], tot, i, j, temp, *ptrOne, *ptrTwo; cout<<"Enter the Size for Array: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; cout<<"\nThe Original Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; ptrOne = &arr[0]; ptrTwo = &arr[tot-1]; j = tot-1; for(i=0; i<j; i++, j--) { temp = *ptrOne; *ptrOne = *ptrTwo; *ptrTwo = temp; ptrOne++; ptrTwo--; } cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; cout<<endl; return 0; }
Reverse an Array using a Function in C++
This is the end of this article. This program is created using a user-defined function, revArray(), to reverse an array entered by the user. The function takes two arguments. The first argument is an array, and the second is its size.
#include<iostream> using namespace std; void revArray(int [], int); int main() { int arr[100], tot, i, j, temp; cout<<"Enter the Size for Array: "; cin>>tot; cout<<"Enter "<<tot<<" Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; cout<<"\nThe Original Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; revArray(arr, tot); cout<<"\n\nThe Reverse of Given Array is:\n"; for(i=0; i<tot; i++) cout<<arr[i]<<" "; cout<<endl; return 0; } void revArray(int a[], int t) { int i, j, temp; j = t-1; for(i=0; i<j; i++, j--) { temp = a[i]; a[i] = a[j]; a[j] = temp; } }
This program does exactly the same job as the previous program.
The same program in different languages
« Previous Program Next Program »