Recursive

In this program, the main function will take the size of the array as input and create a dynamic array of that size. Users will enter the values of the array. Please complete the definition of findSum function which will use recursion to calculate the sum of the values of the number in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
using namespace std;

int findSum(int a[], int size);

int main()
{
    int n;
    cout << "Please enter the size of the array: ";
    cin >> n;
    
    int *array = new int[n];
    cout << "Please enter the values of the array: " << endl;
    for(int i=0; i < n ;i++)
    {
        cin >> array[i];
    }
    
    cout << "The sum of the values in the array is " << findSum(array, n) << endl;

    return 0;
}
your question is unclear, though you did post code.
It just looks like a school question, not what you specifically are stuck on, and it looks like school provided startup code, not something you tried and are struggling with.

how would you stop the recursion? Perhaps, if size passed in were zero?
something like return a[size-1]+findsum(a,size-1) unless its zero, then you just stop and return the value so far... its going to be 2-3 lines of code to do that.
This certainly looks like a question from school or homework. Something you should try to solve yourself first.

Have you even tried to code the function yourself before pasting the provided code here?

A hint. Can you sum a a bunch of numbers in your head or on paper? Think about the steps you'd use and write code to duplicate that effort.



Another hint...

Summing the elements of an array by recursion, as the topic title mentions, really is "there be dragons here" territory. Yes, it can be done, but iterating through the array within the confines of the function is a less cumbersome way of doing it. Using a for loop, for example.
In this assignment, please write a recursive function that will return the sum of all integer numbers in a given array.

That's all she gave me? so I'm confused about it as well
So I have to finish the findSum and make a recursion.
Last edited on
Have you tried to solve this yourself? If so show your attempt at writing the code.

Saying "I'm confused" without making an effort, expecting US to just give you the answer, is not going to help you learn.
if size is zero ... return zero
else ... return (first element plus sum of rest of array)

e.g. sum of array ( 10, 20, 30, 40 ) is 10 + sum of array (20, 30, 40 )


@lastchance, what might be considered an easier method for summing an array by recursion is going backwards through the array. From last to first.
Here's a brief tutorial on the basics of recursion:

https://www.learncpp.com/cpp-tutorial/recursion/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
int sos(int n)
{
  if (n==1)
    return 1;
  else
    return n+sos(n-1);
}
int main()
{
    int findSum(int *a, int size);
  int n;
    cout << "Please enter the size of the array: ";
    cin >> n;

    int* A= new int[n];
    cout << "Please enter the values of the array: " << endl;
    for (int i = 0; i < n;i++)
    {
        cin >> A[i];
    }
    cout << "The sum of the values in the array is " << findSum(A, n) << endl;
    return 0;
}


So sorry I did attempt to do it.
Last edited on
Close, but not quite it. Try this instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

// Return sum of elements in A[0..N-1]
// using recursion.
int findSum(int A[], int N)
{
   if ( N <= 0 ) { return 0; }

   return (findSum(A, N - 1) + A[ N - 1 ]);
}

int main()
{
   int A[] { 1, 2, 3, 4, 5 };

   int N { sizeof(A) / sizeof(A[ 0 ]) };

   std::cout << "The sum is " << findSum(A, N) << '\n';
}

http://coliru.stacked-crooked.com/a/86690ddd10adf59f

You should have enough to adapt this to be "an array on demand."
@George P, why do you say it’s “easier” to go backwards through the array? In the OP it’s a pointer to the beginning of a dynamic array: just increment it by 1 each recursion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int findSum(int a[], size_t size) {
	return size ? *a + findSum(a + 1, size - 1) : 0;
}

int main() {
	size_t n {};

	std::cout << "Please enter the size of the array: ";
	std::cin >> n;

	const auto A { new int[n] };

	std::cout << "Please enter the values of the array: ";
	for (size_t i {}; i < n; ++i)
		std::cin >> A[i];

	std::cout << "The sum of the values in the array is " << findSum(A, n) << '\n';

	delete[] A;
}


PS. Where did function sos() come from when the given function declaration is for findSum() ?
Last edited on
@IanTG: Could you explain how you got from A to B?

A:
IanTG wrote:
complete the definition of findSum function

int findSum(int a[], int size);
B:
IanTG wrote:
I did attempt to do it:
1
2
3
4
5
6
7
int sos(int n)
{
  if (n==1)
    return 1;
  else
    return n+sos(n-1);
}


(Explaining the thought process can help see the issue in new way.)
Topic archived. No new replies allowed.