0

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);*/ }

23rd Jun 2021, 7:36 PM
AndXr
7 Answers
+ 1
LEARN before copy this :) #include <iostream> using namespace std; int recSum(int n) { //complete the function if (n == 1) { return 1; } else { return n + recSum(n-1); } } int main() { //getting input int n; cin >> n; //call the recursive function and print returned value cout << recSum(n); return 0; }
3rd Aug 2021, 5:47 AM
Octavian Malinici-Pruteanu
Octavian Malinici-Pruteanu - avatar
+ 1
"Recursion is a method of solving a problem where the solution depends on the solutions to smaller instances of the same problem." "To avoid having the recursion run indefinitely, you must include a termination condition." Like in factorial function, the termination condition (the base case) is when n==1. int recSum(int n) { if (n == 1) { return 1; } The if statement defines the exit condition. In this case, it's when n equals one, return 1 (recSum(1)=1) On the other hand we need to place the condition when int n >=2. else { return n + recSum(n-1); } So, when n == 2, recSum(2) will return (2 + recSum(2-1)) = (2 + recSum1) = 2 + 1 = 3 When n == 3, recSum(3) will return (3 + recSum(3-1)) = (3 + recSum2) =3 + 3 = 6 and so on... Like the factorial function, recSum function calls itself, and then continues to do so, until the argument equals 1. In the main() function, we get int n from the user cin >> n and print out the result cout << recSum(n).
4th Aug 2021, 11:27 AM
Octavian Malinici-Pruteanu
Octavian Malinici-Pruteanu - avatar
+ 1
Thanks lol. Didn't have much time earlier to read my post to remember what it was about, so I only read the reply. But this answer was exactly what I was looking for, so I really appreciate it. The other guys answer was not all that helpful. But I really liked yours. Wish I got it earlier. I'm pretty bad with maths lol. But yeah. Great, great answer
4th Aug 2021, 7:56 PM
AndXr
+ 1
The biggest problem with your answer was that you didn't put it in an example, So it was hard to figure out what you were saying. That could just be my personal problem. When I read your answer back. It had the same solution as his. But his had the whole code, and a break down of what's happening in an understandable way.
5th Aug 2021, 12:01 AM
AndXr
0
I'll have to re read my question and your answer lol. I can understand what you put there. But I have no idea why lol.
3rd Aug 2021, 2:48 PM
AndXr
0
Shadow Don't remove your answers. Your answer is good too! I didn't mention anything about cakes or eggs :) Our answers are complementary
5th Aug 2021, 3:52 AM
Octavian Malinici-Pruteanu
Octavian Malinici-Pruteanu - avatar
0
#include <iostream> using namespace std; int recSum(int n) { //complete the function if (n == 1) { return 1; } else { return n + recSum(n-1); } } int main() { //getting input int n; cin >> n; //call the recursive function and print returned value cout << recSum(n); return 0; } Good Luck
25th Jan 2022, 5:38 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar