0

What em trying to do is pass the array to a function which will add all the array elements and return the output. Please help me. i dont know what i am doing wrong in this :/

#include <stdio.h>
#define MAX 5

int arraySum(int *dArr,int lim);

int main()
{
    int array[MAX] = {9,7,4,2,10};
    printf("%d", arraySum(array, MAX));
    return 0;
}
int arraySum(int *dArr,int lim)
{
    int Ans;
    if(lim>0)
    Ans = dArr[lim] + arraySum(*dArr, lim--);
    return Ans;
} 
3
  • dArr is a pointer to an integer. Dereferencing it gives you an integer. Anyway, for practical purposes, use std::accumulate. Commented Nov 6, 2012 at 21:26
  • 6
    C and C++ are not interchangeable technologies. Commented Nov 6, 2012 at 21:27
  • There is absolutely no reason to use recursion for this, in real-world applications. Commented Nov 6, 2012 at 23:10

5 Answers 5

4

There are several problems with your code:

  1. You're accessing array[MAX], which is undefined behaviour.
  2. Your function returns the uninitialized Ans when lim is zero.
  3. The first argument to arraySum in the recursive call is wrong.
  4. The use of lim-- is wrong.

Since this looks like homework, I'll let you figure out how to fix these problems. If this isn't homework, you might want to consider whether recursion is the right tool for the job.

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

3 Comments

i did set '--lim' and the value to lim to be passed to 4. (removed array[MAX]) initialized global value of Ans = 0; dont get ur 3rd point
@user1804407: Think about the types of the arguments to arraySum().
okay so i am passing just arraySum(dArr, lim--). still i am getting an error saying operation on lim undefined
1

You run into undefined behavior on dArr[lim], because lim is 5 and the array has elements 0...4.

You also get undefined behavior when lim==0, because you return an un-initialized Ans. When you declare it, initialize it to dArr[0].

After you fix this, you'll want to pass dArr itself further in the recursion, as dArr only returns an int.

Comments

0

Remember that computers treat 0 as the first number, so your array will number from element[0] to element[4]. your code starts from five and counts down to one, which means elements[5] in this case will return garbage, because the index does not exist. pass Lim - 1 into the function or manually changed the value in your function.

ArraySum(Array, MAX - 1);

OR

ArraySum(//....)
{
lim--;
//code here....
}

EDIT: you also need to initialize ans to some value, so if an array of zero elements is passed the function wont return an uninitialized variable.

1 Comment

I think it is OK to not initialize the value of Ans because the value is assigned to Ans and not summed up
0
int arraySum(int *dArr,int lim)
{
    int Ans;
    if(lim>=0) // note the change here
    Ans = dArr[lim] + arraySum(dArr, --lim); // note the --lim change here
    return Ans;
}
  1. You should invoke this with lim as 4 and not 5. Because the array has 5 integers starting from index 0 to index 4. 5th index is out of bounds.
  2. --lim instead of lim-- because lim-- is post decrement. That means the value is first passed and then decremented. Hence everytime your arraySum function gets the value as 4 instead of 3, 2, 1 and 0 (as per your expectation). --lim is pre-decrement.

Comments

0

Change MAX to 4 and change the if(lim>0) condition as if(lim>=0)

This will make your recursion to add as dArr[4]+dArr[3]+dArr[2]+dArr[1]+dArr[0] i.e. all 5 elements of the array.

EDIT: Corrected program:

   int main()
   {
     int array[MAX] = {9,7,4,2,10};
     printf("%d", arraySum(array, MAX-1));
     return 0;
   }

    int Ans = 0;
    int arraySum(int *dArr,int lim)
    {
       if(lim>=0){
          Ans = dArr[lim] + arraySum(dArr, lim-1);
        }
       return Ans;
    } 

4 Comments

#include <stdio.h> int arraySum(int *dArr,int lim); int main() { int array[5] = {9,7,4,2,10}; printf("%d", arraySum(array, 4)); return 0; } int Ans=0; int arraySum(int *dArr,int lim) { printf("l%d ", lim); if(lim>0) Ans = dArr[lim] + arraySum(*dArr, --lim); return Ans; } NO i still have a problem
@user1804407: if (lim >= 0) and not if (lim > 0). Also dArr[lim] + arraySum(*dArr, --lim); should be dArr[lim] + arraySum(dArr, --lim);
@user1804407: If you are using --lim in place of lim-- then start the index at 5 not 4. Otherwise it ignore your dArr[4] and starts from dArr[3]. Remove * from arraySum(*dArr, --lim);.
okay so i am passing just arraySum(dArr, lim--). still i am getting an error saying operation on lim undefined. everything is as u said

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.