0

I wanna do simple recursion to print all the elements in an array. But I got random weird result like 3 5116816 2 -1114130 16812392 1820204365. This is the code that I used:

#include <stdio.h>

int print(int array[],int size) {
  if(size>=0) {
    printf(" %d",array[size]);
    return print(array[6],size-1);
  }
}

int main() {

  int arr[]={1,4,6,9,0,3};
  print(arr,5);

return 0;
}
2
  • 1
    you pass element to array. change that and also order everything should be fine Commented Feb 26, 2017 at 3:28
  • 1
    You should get a compiler warning if all recommended warnings are enabled. Why do you ignore it? Commented Feb 26, 2017 at 3:51

2 Answers 2

2

Just change array[size] in your function to array

Code [to print last to first] :

#include <stdio.h>

int print(int array[],int size) {
  if(size>0) { // changed this too
    printf(" %d",array[size-1]);
    return print(array,size-1); // note this carefully.
  }
}

int main() {

  int arr[]={1,4,6,9,0,3};
  print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]

return 0;
}

code [to print first to last]

#include <stdio.h>

int print(int array[],int size) {
  if(size>0) { // changed this too
    print(array,size-1); // note this carefully.
    printf(" %d",array[size-1]);
    }
}

int main() {

  int arr[]={1,4,6,9,0,3};
  print(arr, sizeof(arr)/sizeof(int)); // changed to correct size [sizeof is generic than just mentioning in the size]

  return 0;
}

at OP's request, explanation of how it works first to last.

Mathematical Explanation : Let print (arr,size) be the function that prints 0 to size array. Now print(arr,size+1) would be print(arr,size); printf(arr[size]) Now see the code again.

Intuitively, if you printing first to last, you have to print lower elements first and the highest element at last.

If you see the original code, you had printed nth element first and hence the reverse printing.

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

6 Comments

Yes, but he didn't mention first to last or last to first.
UG_ Can you please explain a little bit about the code that prints the array from first to last?. It's cool but I don't understand how code can do that.
Added. Let me know if that helps
Thanks . I'm wrong to think that the 'print' function keeps calling itself until 'size=0' so it doesn't even have a chance to execute 'printf'.
It will call itself and then printf. print 3 calls 2 calls 1 and then 0. After that 1 will complete by calling printf, then 2 then 3
|
1

Your code seems good. Only problem it has is, during recursive call of function you are passing the array element instead of array and it prints garbage value as it is referring to some other memory location i.e array[6] which is undefined

In the above code snippet,

return print(array[6],size-1);

change this to

return print(array,size-1);

Comments

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.