-2

The user get me the size of the array, and i am allocate with function "MALLOC" the memory allocation and get input the array with recursion only, without loops!!

I do not know how to create a "loop" with recursion function.

I hope happy to get help please.

thanks.

2
  • It's really hard to understand what you are asking, can you please clarify ? Commented Dec 24, 2011 at 8:11
  • I need input from user size of array and create allocated memory this array. And scanf to this array the values. I write this function but this not worked. <br/>void getss(int array[], int size) <br/>{ int counter=0; if(size==0) return; if(counter<size) { scanf("%d",&array[i]; counter++; } getss(array,size-1); } Commented Dec 24, 2011 at 8:30

2 Answers 2

1

That's not a good use of recursion - a loop is more "natural" in this case - but the idea would be a function like this:

void getInput(int howMany)
{
  if (howMany == 0)
    return;
  // get user input
  getInput(howMany-1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

i agree with you, but is require of my lecturer. but i need create a memorey allocetion, i dont need send the point of the function?
1

Because this seems like homework, I'll give a homework-oriented answer -- in pseudo-code.

define loop(array, index)
    if index == 0
        return array
    array[index] = get input from user
    loop (array, index-1)

Call it with loop(array, array_size).

I'd like to echo Mat's sentiment that this is not a good use of recursion.

Update

Since you'd like to use scanf(3) to read the input, I'll give a stronger hint on how to use it. Above, I wrote:

    array[index] = get input from user

You could write this line with the following scanf(3) call:

scanf("%d", &array[index]);

This will store a decimal integer into the array[index] location -- the & returns the address of the array slot, rather than evaluating the array subscription.

2 Comments

i need the scanf input from user values in the array. i dont understand how can i to scanf input without the loops.
+1 You're putting more effort into this than the OP deserves.

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.