You are on page 1of 2

CoCubes Coding Question - Reverse Array

Reverse array : CoCubes coding question


Ques. You are given a function,int* ReverseArray(int* arr, int length);
The function takes an integer array and its length as input. Implement the function
to return the array such that the array is reversed i.e. the first element of the
array occupies the last position, second element occupies the second last position
and so on.

Note:
The re-arrangement is to be done in-place i.e you cannot use another array.

Assumption:
You may assume that the array is of even length.

Example:

Input:
2 4 6 8 20 15 10 5

Output:
5 10 15 20 8 6 4 2

*********************************************************************************
Program
*********************************************************************************

#include<stdio.h>

/* Function to reverse arr[] from start to end*/


void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

/* Utility that prints out an array on a line */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);

printf("\n");
}

/* Driver function to test above functions */


int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
printf("Reversed array is \n");
printArray(arr, 6);
return 0;
}

You might also like