A C program to reverse an integer array

Reverse an array means suppose an array given i.e.

Input acTest [5] = { 1, 2, 3, 4, 5 };

then the output array should be:

acTest [5] = { 5, 4, 3, 2, 1 };

We can write a solution to this problem in more than one ways and here, I’m giving some of them.

  1. Using an extra array
  2. Without using an extra array
  3. Using pointers

Here is the C program to reverse the array using an extra array.

/* Method 1 : using an extra array. */
#include <stdio.h>
void reverseArray ( int arr[], int temp[], int arrSize )
{
int i, j;
/* Copying elements into array temp starting from end of array acTest */
for (i = arrSize - 1, j = 0; i >= 0; i--, j++)
temp [j] = arr [i];
/* Copying reversed array into the original */
for ( i = 0; i < arrSize; i++)
arr[i] = temp[i];
}
/* Driver function of the program*/
int main ( )
{
int i, sizeOfArr = 0;
int acTest [5] = { 1,2,3,4,5};
int acTestTemp [5] = {0};
sizeOfArr = sizeof ( acTest ) / sizeof ( acTest[0] );
/* Printing the original array elements before reversing the array */
printf ( "Before reversing array :\n" );
for (i = 0; i < sizeOfArr; i++)
printf("%d\n", acTest[i]);
/* reverseArray() call */
reverseArray ( acTest, acTestTemp, sizeOfArr );
/* Printing original array elements after reversing the array */
printf ( "After reversing array :\n" );
for (i = 0; i < sizeOfArr; i++)
printf("%d\n", acTest[i]);
return 0;
}

Here is the C program to reverse the array without using an extra array.

/* Method 2 : Without using an extra array */
#include <stdio.h>
void reverseArray ( int arr[], int arrSize )
{
int i, temp, end;
end = arrSize - 1;
for ( i = 0 ; i < arrSize / 2 ; i++ )
{
/* Swapping array element with the help of temp variable */
temp = arr [i];
arr[i] = arr[end];
arr[end] = temp;
end--;
}
}
/* Driver function of the program*/
int main ( )
{
int i, sizeOfArr = 0;
int acTest [5] = { 1,2,3,4,5};
sizeOfArr = sizeof ( acTest ) / sizeof ( acTest[0] );
/* Printing the original array elements before reversing the array */
printf ( "Before reversing array :\n" );
for (i = 0; i < sizeOfArr; i++)
printf("%d\n", acTest[i]);
/* reverseArray() call */
reverseArray ( acTest, sizeOfArr );
/* Printing original array elements after reversing the array */
printf ( "After reversing array :\n" );
for (i = 0; i < sizeOfArr; i++)
printf("%d\n", acTest[i]);
return 0;
}

Here is the C program to reverse the array using pointers.

/* Method 3 : Using pointer */
#include <stdio.h>
#include <stdlib.h>
void reverseArr ( int *arrPtr, int n )
{
int *arr, i, j;
arr = (int*) malloc (sizeof (int) * n );
if( arr == NULL )
exit ( EXIT_FAILURE );
for ( i = n - 1, j = 0 ; i >= 0 ; i--, j++ )
*(arr+j) = *(arrPtr+i);
for ( i = 0 ; i < n ; i++ )
*(arrPtr+i) = *(arr+i);
free(arr);
}
/* Driver function of program */
int main()
{
int n, i, *acTest;
scanf("%d",&n);
acTest = (int*) malloc (sizeof(int)*n);
if( acTest == NULL )
exit ( EXIT_FAILURE );
for ( i = 0 ; i < n ; i++ )
scanf("%d",(acTest+i));
reverseArr ( acTest, n );
printf("Array on reversal:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n",*(acTest+i));
free(acTest);
return 0;
}

To contribute :

If you like Advance Computing and would like to contribute, you can  mail your article to “computingadvance@gmail.com”. You will get credit as your name , email id, designation with article on this blog.

Leave a Reply