1

i'm new to programming and i'm learning about recursion, i am doing exercise 4 from this page https://www.w3resource.com/c-programming-exercises/recursion/index.php, i am trying to print the elements of the array once the value of "i" in the recursive function gets to the value of "n", however once i run the program it prints garbage values i think.my question is why does it do that? why doesn't it print the values of the array?

#include <stdio.h>
#include <stdlib.h>

void printr(int n,int i);

int main (void) 
{   
    printf("NUMBER OF ELEMENTS:\n");
    int n;
    scanf("%i",&n);
    printr(n,0);

}

void printr(int n,int i)
{   
    int arr[n];

    if (i == n)
{
    for (int j = 0; j < n; j++)
    {
        printf("%d",arr[j]);
        return;
    }    
}

    printf("element %d :\n",i+1);
    int e;
    //scan for input
    scanf("%d",&e);
    //populate array
    arr[i]=e;
    //do it again
    printr(n,i + 1);
}

Then i solved it by passing the array defined in the mein function asan argument in the printr function it worked but i don't understand why my first attemp didn't?

#include <stdio.h>
#include <stdlib.h>

void printr(int arr[],int n,int i);

int main (void)
{   
    printf("NUMBER OF ELEMENTS:\n");
    int n;
    scanf("%i",&n);
    int arr[n];
    printr(arr,n,0);

}

void printr(int arr[],int n,int i)
{   

    if (i == n)
    {
        for (int j = 0; j < n;j++)
        {
            printf("%d ",arr[j]);
        }
        printf("\n");
        return;   
    }

    printf("element %d :\n",i+1);
    int e;
    //scan for input
    scanf("%d",&e);
    //populate array
    arr[i] = e;
    //do it again
    printr(arr,n,i + 1);
}

Thank you!

4
  • 1
    You don't initialize the values before printing. Why is this even recursive? This is the backwards way of doing it. Loop, populate, then print. The approach you have here is downright twisted and confused. Commented Nov 30, 2020 at 19:20
  • 1
    🔎🐞Did run your code in a debugger to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? Commented Nov 30, 2020 at 19:22
  • 1
    Each recursive call has its own int arr[n];. Unlike when passed from main, theye are separate arrays, not shared. Commented Nov 30, 2020 at 19:22
  • 1
    @WeatherVane Ah, I was only looking at the second one. Commented Nov 30, 2020 at 19:33

2 Answers 2

1

Because basic C: each invocation of printr has its own arr on the stack, initially uninitialized. And you print it without initializing it first.

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

Comments

1

When you print out the value, it isn't initialized first. When you create the variable to be printed, its pointing to a place in memory. This memory was probably freed by another program (so its up for grabs). As such, it contains "garbage" data. The way to deal with this is to initialize your values.
In order to avoid stuff like this in the future, get in the habit of setting your pointers to NULL when you don't initialize them so that your programs segfaults when your trying to read an uninitialized value.

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.