简体   繁体   中英

Passing Array to a function and Adding it through Recursion

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;
} 

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.

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 .

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] ie 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;
    } 

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM