Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi I am trying to write a recursive function that adds an array of integers. When I run this it actually crashes the console window so I'm guessing I must be doing something wrong to create a stack overflow? Here is the main program with the function.

```
#include <iostream>
using namespace std;
int sumOfElements(const int arr[], int size);
int main()
{
int answer;
int arrSize;
cout << "how many numbers do you want to add" << endl;
cin >> arrSize;
int num[arrSize];
for (int i = 0; i < arrSize; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> num[i];
}
answer = sumOfElements(num,arrSize);
cout << "The sum of the numbers entered is: " << answer << endl;
return 0;
}
int sumOfElements(const int arr[], int size)
{
int sum = arr[0];
if (size == 1)
return sum;
else
return sum+= sumOfElements(arr, size + 1);
}
```

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

```
#include <iostream>
using namespace std;
int sumOfElements(const int arr[], int size, int sum);
int main()
{
int answer;
int arrSize;
int sum = 0;
cout << "how many numbers do you want to add" << endl;
cin >> arrSize;
int num[arrSize];
for (int i = 0; i < arrSize; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> num[i];
}
sum = num[0];
answer = sumOfElements(num,arrSize, sum);
cout << "The sum of the numbers entered is: " << answer << endl;
return 0;
}
int sumOfElements(const int arr[], int size, int sum)
{
if (size == 1)
return sum;
else
return sum+= sumOfElements(arr, size - 1, sum);
}
```

Think about the results you are getting and the numbers you are entering. Do you see any correlation in the fact that whenever you enter

int sumOfElements(const int arr[], int size, int sum)

{

if (size == 1)

return sum;

else

return sum+= sumOfElements(arr, size - 1, sum);

}

If you are trying to add elements of array, you should be passing only the index NOT sum and the it should return value of that element like

return arr[index];

instead of return sum

The backtracking part should work fine now as you are doing size - 1 so size value will eventually reach value 1.

If you are trying to add elements of array, you should be passing only the index NOT sum and the it should return value of that element likeI think there's a mix-up of terminology going on in that statement. I agree that you should be "passing" some sort of index (or indexed value), but the "return" is valid as is written = )

return arr[index];

instead of return sum

Function should only be

if ( size > 0)

return arr[size] + sumOfElements(arr, size - 1);

else

return arr[0];

So, if the array has 3 elements, you should call it with size 2 initially. There are sure other ways to do the same thing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialok so now I have the function as follows:

int sumOfElements(const int arr[], int size)

{

if (size > 0)

return arr[size]+ sumOfElements(arr, size - 1);

else

return arr[0];

}

But now this is producing some weird results if I choose 3 numbers to add no matter what numbers I put in the answer is always 4199524 and if I choose 4 numbers same thing happens give a massive number like 4469876. But if I choose 5 numbers to add it works perfectly.

```
#include<iostream>
using namespace std;
int sumOfElements(const int[], int);
int main()
{
int arr[3] = {3, 5, 7};
int sum = sumOfElements(arr, 2);
cout << "Sum : " << sum << endl;
//system("pause");
return 0;
}
int sumOfElements(const int arr[], int index)
{
if(index > 0)
{
return arr[index] + sumOfElements( arr, index -1);
}
else
{
return arr[0];
}
}
```

```
#include<iostream>
using namespace std;
int sumOfElements(const int[], int);
int main()
{
int arr[3] = {3, 5, 7};
int sum = sumOfElements(arr, 2);
cout << "Sum : " << sum << endl;
//system("pause");
return 0;
}
int sumOfElements(const int arr[], int index)
{
if(index > 0)
{
return arr[index] + sumOfElements( arr, index -1);
}
else
{
return arr[0];
}
}
```

Kindly tell me how would you write it. It produces the correct result in every situation.Ah, but in your newest code you are using

He has to pass the maximum index of array and it would reduce to 0 eventually. But in the terminal case, I do not want another recursive call to the function that it why I used return arr[0] alone. Makes sense?

```
int main()
{
int arr[] = {3, 5, 7};
int max_index = sizeof(arr)/sizeof(int) -1 ;
int sum = sumOfElements(arr, max_index);
cout << "Sum : " << sum << endl;
system("pause");
return 0;
}
```

This way you can change the array arr without changing anything else.

I have no issue with the logic you posted. I was just confused by the terminology. If there's better logic out there, I don't know what it is = )

C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.