Reverse An Array In C

We must invert the array and display the following:

We have the option to employ list slicing in Python by specifying the step as -1 -> list [::-1] to obtain the reversed list. Conversely, in C, there is an absence of a slicing mechanism, requiring us to delve into lower-level problem-solving techniques.

Possible Approaches:

  • Declaring another array
  • Iteration and Swapping
  • Using pointers
  • 1. Declaring another array

We have the option to define a new array, cycle through the initial array in reverse, and transfer the elements to the new array from the start.

Code:

Example

#include<stdio.h>
int main()
{
	int n, arr[n], i;
	printf("Enter the size of the array: ");
	scanf("%d", &n);
	printf("Enter the elements: ");
	for(i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	int rev[n], j = 0;
	for(i = n-1; i >= 0; i--)
	{
		rev[j] = arr[i];
		j++;
	}
	printf("The Reversed array: ");
	for(i = 0; i < n; i++)
	{
		printf("%d ", rev[i]);
	}
}

Output:

Output

Enter the size of the array: 5
Enter the elements: 1 2 3 4 5
The Reversed array: 5 4 3 2 1

Here is an example:

If the array is:

We generate a new array named rev with an identical size of 6 elements:

First iteration: i = n - 1 = 5, i >= 0, j = 0

rev[0] = arr[5]

In the same way:

Second iteration: rev[1] = arr[4]

Third iteration: rev[2] = arr[3]

Fourth iteration: rev[3] = arr[2]

Fifth iteration: rev[4] = arr[1]

Sixth iteration: rev[5] = arr[0]

Hence, the resultant reversed array:

Example

Enter the size of the array: Enter the elements: The Reversed array: [value]

2. Iteration and swapping

Iterating half of the array:

We loop through the array up to half of its size, and for each element at index i, we exchange it with the element at index (size - i - 1).

Here is an example:

If the array is:

size = 6, size/2 = 3

First iteration: i = 0, i < 3

We swap arr[i] with arr[n - i - 1]

arr[0] <-> arr[5]

Second iteration: i = 1, i < 3

We swap arr[i] with arr[n - i - 1]

arr[1] <-> arr[4]

Third iteration: i = 2, i < 3

We swap arr[i] with arr[n - i - 1]

arr[2] <-> arr[3]

Termination when i = 3 as i == n/2

The resultant reversed array:

Example

Enter the size of the array: Enter the elements: The Reversed array: [value]

Code:

Example

#include<stdio.h>
int main()
{
	int i, n, temp;
	printf("Enter the size of the array: ");
	scanf("%d", &n);
	int arr[n];
	printf("Enter the elements: ");
	for(i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	int end = n - 1;
	for(i = 0; i < n/2; i++)
	{
		temp = arr[i];
		arr[i] = arr[end];
		arr[end] = temp;
		end--;
	}
	printf("The reversed array: ");
	for(i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
}

Output:

Output

Enter the size of the array: 6
Enter the elements: 1 2 3 4 5 6
The reversed array: 6 5 4 3 2 1

Iterating from two ends of the array:

We have the option to continuously iterate and exchange elements from either end of the array until reaching the middle element.

Code:

Example

#include<stdio.h>
int main()
{
	int n, i;
	printf("Enter the size of the array: ");
	scanf("%d", &n);
	int arr[n];
	printf("Enter the elements: ");
	for(i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	int l = 0;
	int h = n - 1;
	int temp;
	while(l < h)
	{
		temp = arr[l];
		arr[l] = arr[h];
		arr[h]= temp;
		l = l + 1;
		h = h - 1;
	}
	printf("The Reversed array: ");
	for(i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
}

Output:

Output

Enter the size of the array: 6
Enter the elements: 1 2 3 4 5 6
The Reversed array: 6 5 4 3 2 1

Here is an example:

If the array is:

size = 6

l = 0, h = size - 1 = 5

First iteration: l = 0, h = 5

We swap arr[l] with arr[h]

arr[0] <-> arr[5]

Second iteration: l = 1, h = 4

We swap arr[l] with arr[h]

arr[1] <-> arr[4]

Third iteration: l = 2, h = 3

We swap arr[l] with arr[h]

arr[2] <-> arr[3]

Termination occurs when both l and h are equal to 3, since the next address that ptr will hold is obtained by incrementing ptr by 1.

Code:

Example

#include <stdio.h>
int main()
{
	int *ptr1, *ptr2;
	int n, i, temp;
	printf("Enter the size of the array: ");
	scanf("%d", &n);
	int arr[n];
	printf("Enter the elements into the array: ");
	for(i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	ptr1 = &arr[0];
	ptr2 = &(arr[n - 1]);
	
	while(ptr1 < ptr2)
	{
		temp = *ptr1;
		*ptr1 = *ptr2;
		*ptr2 = temp;
		ptr1 = ptr1 + 1;
		ptr2 = ptr2 - 1;
	}
	printf("The reversed array: ");
	for(i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	
}

Output:

Output

Enter the size of the array: 6
Enter the elements into the array: 1 2 3 4 5 6
The reversed array: 6 5 4 3 2 1

Understanding:

  • We declared two integer pointers, ptr1 and ptr2.
  • ptr1 points to the first element of the array, and ptr2 points to the last element of the array.
  • When we say ptr1 and ptr2 refer to the addresses of the first and last elements of the array.
  • An array stores elements in contiguous memory locations. The memory allocations for an array from the first element to the last element will be in increasing order; hence the condition ptr1 < ptr2 is used in the while loop.
  • Now, using ptr1 and ptr2, we swap the values at ptr1 and ptr2 with a temporary variable-temp.
  • We increment ptr1 to its succeeding element and decrement ptr2 to its preceding element using simple pointer arithmetic.
  • The swapping continues till both ptr1 and ptr2 reach the middle element or when ptr1 and ptr2 overtake the middle element of the array in their iterations.

Input Required

This code uses input(). Please provide values below: