Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
How to Reverse an Array in C Programming (With Example)
Intro
Reversing an array is one of the most basic and important problems in C programming. In this post, we will learn how to reverse array elements using a simple two pointer approach. This example is easy to understand and helps beginners improve their understanding of arrays and logical thinking.
Problem Statement
You are given an integer value that represents the size of an array, followed by the elements of the array. The task is to reverse the order of the array elements using C programming. After reversing, the program should display the updated array. This problem helps beginners understand array traversal, swapping elements, and the use of loops in C programming.
Algorithm / Logic Explanation
To reverse an array efficiently, we use the two-pointer technique. This approach avoids using extra memory and works directly on the given array.
Step-by-step logic:
-
First, read the size of the array (
n) from the user. -
Declare an array of size
nand read all elements into it. -
Initialize two variables:
-
leftstarting from index0 -
rightstarting from indexn - 1
-
-
While
leftis less thanright:-
Swap the elements at positions
leftandright. -
Move
leftone step forward. -
Move
rightone step backward.
-
-
Continue this process until both pointers meet.
-
Finally, print the reversed array.
Why this algorithm works:
-
Each swap places two elements in their correct reversed positions.
-
The loop runs only
n/2times, making it efficient. -
No extra array is used, so memory usage is minimal.
Pseudocode:
Read n
Read array elements
left = 0, right = n - 1
while left < right
swap(arr[left], arr[right])
left++, right--
print array
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array:\n");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int left = 0, right = n - 1;
while(left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
printf("Array after reversing:\n");
for(int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Sample Input
Explanation:
The array elements are printed in reverse order by swapping the first and last elements repeatedly using the two-pointer approach.
This C program reverses the elements of an array using the two-pointer technique. First, the program reads the size of the array and stores it in the variable n. Based on this size, an integer array arr is declared to hold the elements. The user then enters n elements, which are stored sequentially in the array.
Two integer variables, left and right, are used to control the reversing process. The left pointer starts from the first index of the array (0), while the right pointer starts from the last index (n - 1). These pointers move toward each other.
Inside the while loop, the condition left < right ensures that swapping continues only until the middle of the array is reached. A temporary variable temp is used to swap the elements at the left and right positions safely. After each swap, left is incremented and right is decremented, moving both pointers closer.
This process continues until all elements are placed in reverse order. Finally, a for loop is used to print the reversed array elements. This approach is efficient because it reverses the array in place without using extra memory. The time complexity is O(n), and the space complexity is O(1), making this solution optimal and beginner-friendly.
Edge Cases / Notes
- If the array size is
0or1, no reversal is needed because the array remains the same. - Very large array sizes may cause memory issues if system limits are exceeded.
- The program assumes valid integer input from the user.
- This logic works only for one-dimensional arrays.
- Input validation can be added for better robustness.
FAQs
1. What does reversing an array mean?
Reversing an array means changing the order of elements so that the first element becomes the last, the last becomes the first, and so on.
2. What is the two-pointer technique?
The two-pointer technique uses two indices that move toward each other to solve problems efficiently, commonly used in arrays and strings.
3. Can we reverse an array without using extra memory?
Yes, the two-pointer approach reverses the array in place without using any additional array.
4. What is the time complexity of this program?
The time complexity is O(n) because each element is accessed only once.
5. Is this method suitable for interviews?
Yes, this is a standard and efficient approach frequently asked in technical interviews.
Keywords
reverse array in c
-
c program to reverse array
-
array reversal in c programming
-
two pointer technique in c
-
c array example
-
array manipulation in c
-
beginner c programming
-
c programming interview questions
-
reverse array using pointers
-
hackerank c array problems
#clanguage
#array
#coding
#programming
#datastructures
#codingforbeginners
#learnc
#codinginterview
#1printf
Related: C Program to Reverse an Array Using Recursion
-
Related: C Program to Find the Largest Element in an Array
-
Related: C Program to Check Palindrome Number
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment