Reversing an array of numbers using Recursion

Problem Statement
Write a c program to reverse elements of an array of numbers using recursive function
#include<stdio.h>
void reverse(int a[],int i,int n);
int main()
{
  int a[55],i,z,n;
  printf("enter the array and enter ctrl-d in the end \n");
  i=0;
  while(scanf("%d",&z)!=EOF)
    {
      a[i]=z;
      i++;
    }
  n=0;
  reverse(a,i,n);
  for(n=0;n<i;n++)
    {
      printf("%d ",a[n]);
    }
  return 0;
}
void reverse(int a[],int i,int n)
{
  int temp;
  if(n==i||n>i)
    {
      return;
    }
  else
    {
      temp=a[n];
      a[n]=a[i-1];
      a[i-1]=temp;
      reverse(a,i-1,n+1);
    }
  return ;
}

1 comment:

  1. Thanks a lot for giving everyone an extraordinarily brilliant possiblity to read critical
    reviews from this site. It's always so nice and also packed with a good time for me and my office colleagues to search your website the equivalent
    of three times in one week to study the newest tips you will have.
    And lastly, I am also always fulfilled with your gorgeous principles served by
    you. Selected 2 tips in this post are surely the very best we have ever
    had.

    My web page :: Winnipeger

    ReplyDelete