0

I need to write a function, to input the size of array and allocate memory, and read (scanf) values to this array.

I wrote this function but it does not work:

void getss(int array[], int size)
{

    int counter = 0;
    if (size == 0)
        return;
    if (counter < size) {
        scanf("%d", &array[i]);
        counter++;
    }
    getss(array, size - 1);
}
4
  • 1
    How does this not work? Where does it fail? Commented Dec 24, 2011 at 8:47
  • Background information for others: stackoverflow.com/q/8623471/377270 -- yes, this is almost a duplicate, but how far should a question "migrate" from where it started? Seems fine to me so far... Commented Dec 24, 2011 at 8:48
  • 2
    It does not work because i is undeclared. Should probably be size instead. But even then, array[0] is never written to. Commented Dec 24, 2011 at 8:52
  • Unless this is a homework "get to know recursion" question, you really don't need to write this function recursively. It's actually a rather horrible idea. Commented Dec 24, 2011 at 9:45

7 Answers 7

1

It is not exactly clear what you are trying to achieve. But I would guess that your array parameter is not correct in the recursive call. Now all calls to getss get same array but different size. (Where does variable i come from? Variable counter does not have much use in posted code.)

I would assume that you need to update the array pointer to point to next element in array in the recursive call. In other words, pass address of second element in the array instead of the first element (current behaviour). That would be in line with the size-1 in the recursive call.

The implementation is trivial but is left as an exercise to the poster as this looks like homework to me.

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

Comments

1

You have unneeded variables. Is this what you are trying to accomplish:

The following will fill in the array so that the first value read with scanf() will be stored in the last element.

void getss(int array[], int size)
{
    if (size == 0)
        return;
    scanf("%d", &array[size-1]);
    getss(array, size - 1);
    return;
}

The following will fill in the array so that the first value read with scanf() will be stored in the first element.

void getss(int array[], int size)
{
    if (size == 0)
        return;
    scanf("%d", &array[0]);
    getss(&array[1], size - 1);
    return;
}

Comments

1

If you were to do this iteratively, how would you do it? I would assume something like this:

void getss(int array[], int size) {
  int i;
  for (i = 0; i < size; i++) {
    scanf("%d", array[i]);
  }
}

Our i there just serves to count our way through the array, so if we change our function slightly we can get rid of it:

void getss(int* array, int size) {
  for (; size > 0; size--, array++) {
    scanf("%d", array);
  }
}

Now our "counter" is the size variable itself and instead of indexing in our array we just keep stepping forward our pointer to it.

Now this is in a perfect form to be turned into a recursive function. Instead of size-- and array++ we can pass the new values into another call to getss at the end of the function:

void getss(int* array, int size) {
  if (size > 0) {
    scanf("%d", array);
    getss(array + 1, size - 1);
  }
}

So our loop's terminating condition has moved into an if and instead of array++ and size-- we pass in array+1 and size-1 to our next call. It achieves the same result as looping but without an explicit loop construct.

Comments

0

Along with the other problems, what's the point of using counter? It is either 0 or 1 and never passed to the next call. Should it have been declared static?

Comments

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

void getss(int* array, int size){
    if (size == 0)
        return;
    scanf("%d", array);
    getss(++array, size - 1);
}

int main(){
    int *array;
    int i, size;

    printf("input array size:");
    scanf("%d", &size);
    array = (int*)malloc(sizeof(int)*size);
    getss(array, size);
    for(i=0;i<size;i++){
        printf("array[%d]=%d\n", i, array[i]);
    }
    return 0;
}

Comments

0
#include<bits/stdc++.h>
using namespace std;
int a[100];
void input(int n)
{
    if(n==0)
    {
        return;
    }
    input(--n);
    cin>>a[n];
}
int main()
{
        int n=4;
        input(n);
        for(int i=0;i<n;i++)
        {
            cout<<a[i]<<" ";
        }


} 

Comments

-2
void getss(int array[], int size , int counter)
{
    if (size == 0)
        return;
    if (counter < size) {
        scanf("%d", &array[i]);
        counter++;
    }
    getss(array, size - 1, counter);
    }

4 Comments

Brother after observing your problem and I can see that the value of count get 0 in every recursion because every time you call the function count set to 0 that's why the value in array save in same index value which is 0 you should try by adding the value count in parameter of the function like this,
Please add some explanation to your answer such that others can learn from it
"counter" seems a useless variable. Manipulating the existing "size", and replacing undeclared variable "i" would be a better solution.
Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. This is especially important when answering old questions (this one is nearly 11 years old) with existing answers. Please edit your answer and explain how it answers the specific question being asked, and how it differs from and improves upon the answers that are already here. See How to 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.