C++ recursion challenge. wanting help
My question is. how are you supposed to solve this test without cheating? looking to see how you guys solved it. and maybe some examples so i can wrap my head around this lol. Just completed the Recursion challenge. but i couldn't figure out a way to actually do it without cheating(I'm not really sure if it is or not. but i assume you are only meant to add bits where it specifies.), which really annoys me. the problem i was having. is I wasn't sure how to save variables for continued "recursions" of the function, while only adding code under the function itself, and adding code under the line asking me to call it. heres the actual test. "Pastry chefs are competing to win the battle of the cakes. For each additional cake made, the number of eggs required increases by 1 (1 egg for the first cake, 2 eggs for the second, etc.). Take the number of cakes that must be baked as the input, calculate (recursively) how many eggs were used to bake them by the end of the battle and output the result." and here's the Script. I'll make sure to put all my code and my comments between /* */, so it's easier to tell what is part of the original test. and what has been added. ===================== #include <iostream> using namespace std; /*int C = 1; int eggs;*/ /* here is where I added the actual variables i wanted to persist between function runs. but as you can see. theres no comments saying that i was allowed to do so */ /*i wanted to add a second parameter to this function, so it could be tracked.*/ int recSum(int n) { //complete the function /* if (n < 1) { return eggs; } else { eggs += C; /* C, stands for Cakes lol. not the language.*/ C++; return recSum(n-1); }*/ } int main() { //getting input int n; cin >> n; //call the recursive function and print returned value /*int cakes = n; cout << recSum(cakes);*/ }