recent

C program to reverse an array-recursive method

Recursive Way:
1) Initialize low and high indexes
low = 0, high = n-1
2) Swap a[low] with a[high]
3) Recursively call reverse for rest of the array.

#include<stdio.h>
void ReverseArray(int a[],int low,int high)
{
   int temp;
   if(low >= high)
     return;
   temp = a[low];
   a[low] = a[high];
   a[high] = temp;
   ReverseArray(a, low+1, high-1);
}  


void print(int a[],int size)
{
  int i;
  for (i=0;i<size;i++)
   {
    printf("%d\t",a[i]);
   }

  printf("\n");
}


main()
{

int a[20],low,high,i,n;

printf("Enter Number of Array element:\n");
scanf("%d",&n);

printf("Enter Array element:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
low=0;
high=n-1;

  ReverseArray(a,low,high);

  printf("Reverse Array is \n");
  print(a,n);
}
Powered by Blogger.