Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Created March 12, 2021 22:29
Show Gist options
  • Save chrisdel101/de539ee4453ab63da51fe2596fb80bfb to your computer and use it in GitHub Desktop.
Save chrisdel101/de539ee4453ab63da51fe2596fb80bfb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
int adder(vector<int> &myElements, int &sum)
{
if (myElements.size() == 0)
{
cout << "sum " << sum << endl;
return sum;
}
sum += myElements.back();
myElements.pop_back();
adder(myElements, sum);
}
// int adder(vector<int> elems)
// {
// if (elems.size() == 1)
// return elems[0];
// else
// {
// int elem = elems.back();
// elems.pop_back();
// return elem + adder(elems);
// }
// }
int main()
{
int sum = 0;
vector<int> nums1 = {1, 2, 3, 4};
vector<int> nums2 = {100, 200, 300, 400};
vector<int> nums3 = {1000, 2000, 3000, 4000};
cout << "Sum" << adder(nums1, sum) << endl;
cout << "Sum" << adder(nums2, sum) << endl;
cout << "Sum" << adder(nums3, sum) << endl;
// add(nums2, 0);
// add(nums3, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment