1

I am trying to print a array in Reverse order by using the recursive function but I am getting so many error can someone guide me through the process please where I am making the mistakes.

#include<stdio.h>
int PrintArray(int a[],int k) {
    int z;
    while(k>0) {
        z=k-1;
        PrintArray(a[],z);
        printf("%d ",a[k]);
    }
}
int main() {
    int n,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++) 
        scanf("%d",a[i]);
    PrintArray(a[],n);
    return 0;
}
2
  • PrintArray(a[],n) should be PrintArray(a,n). But if you know n, you don't need recursion to print the array backwards. A simple loop will do. So I think there's something about the assignment that you didn't understand. Commented Nov 11, 2015 at 20:45
  • 1
    this code has several issues. @user3386109 has already mentioned several. Apart from these there's aswell a logical error in the code: printArray is recursive, but aswell uses a loop to print the array in reverse order. This leads to misbehaviour, since element x in the array will be printed n - x times this way. Commented Nov 11, 2015 at 21:00

3 Answers 3

2

I edited your code . Here is a new one:

#include<stdio.h> 
void PrintArray(int a[],int k) 
{
    int z;
    if (k>0) 
    {
        z= k-1;
        printf("%d ",a[z]);
        PrintArray(a,z);
     }
 return; 
 } 
 int main() 
 {
     int n,i;
     scanf("%d",&n);
     int a[n];
     for(i=0;i<n;i++) 
         scanf("%d",&a[i]);
     PrintArray(a,n);
     return 0; 
 }

Now let me highlight your errors:

  1. While using scanf("%d", a[i]) you have to use address of the input location. i.e. &a[i].

  2. Your recursive function was of type int and was not returning anything. therefore use void instead of int.

  3. The function calling was also syntactically incorrect. While calling the function you should not place [] when you are passing an array. Just Simply pass the name of the array. eg. PrintArray(a,z);

  4. Your logic in the function is absolutely wrong .The printf("%d ",a[k]); will never get executed because it is placed after the function call, so either the the next recursive function will be called or the while loop condition will not be satisfied.

Sign up to request clarification or add additional context in comments.

Comments

0

This is not work, you need to use this code:

#include<stdio.h> 
void PrintArray(int a[],int k) 
{
   int z;
   if (k>0) 
   {
      z= k-1;
      printf("%d ",a[z]);
      PrintArray(a,z);
   }
return; 
} 
int main() 
{
 int n,i;
 scanf("%d",&n);
 int a[n];
 for(i=0;i<n;i++) 
     scanf("%d",&a[i]);
 PrintArray(a,n);
 return 0; 
}

Comments

-1

function

This function will receive address of array in pointer variable and it will receive the size of array... Then the function displays array in reverse order.

void R_output(int *arr, int s) {
  int l;
  if(s > 0)
  {
    l = s - 1;
    cout << arr[l] << " ";
    R_output(arr, l);       
  } 
  return;
}

1 Comment

This is a C question. Do not provide a C++ answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.