Reputation: 43
#include<iostream>
#include<cstdio>
using namespace std;
void print_array_1(int a[], int n , int i) // n = size of array, i = start index
{
if(i>=n) return;
printf("%d\n",a[i]);
print_array_1(a, n, i++);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
print_array_1(arr, n, 0);
return 0;
}
Upvotes: 0
Views: 72
Reputation: 8851
Assuming this is C, you should use ++i
. Why?
i++
increments i, but returns i
.
++i
increments i and returns 1+1
.
With i++
, you keep calling the recursion function with the same i
value.
Upvotes: 1
Reputation: 1842
i++ increments i after use in the call to print_array_1, so every call gets i=0.
Upvotes: 0