+ 11

How can i solve?

Passing the first level of a video game awards the player 1 point. For each subsequent level passed, the points awarded increment by 1 (2 for the 2nd level, 3 for the 3rd, and so on). The program you are given takes the number of passed levels as input. Complete the given function to take that number as an argument, and recursively calculate and return the total number of points given for all passed levels. Sample Input 3 Sample Output 6 Explanation Level 1: 1 point Level 2: 2 points Level 3: 3 points Total: 1+2+4 = 6 points. https://code.sololearn.com/c0h5UHEZ3srl/?ref=app https://code.sololearn.com/c0h5UHEZ3srl/?ref=app

14th Dec 2020, 10:22 PM
Antonio
Antonio - avatar
29 odpowiedzi
+ 14
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { if (levels == 1) return 1; return levels + Points(levels - 1); } } }
25th Apr 2021, 4:25 PM
Manali Bane
Manali Bane - avatar
+ 5
Brian Ya, I was just re-reading the question. (Too many distractions here RN). I missed the part where it asked for recursion.
14th Dec 2020, 11:33 PM
ChaoticDawg
ChaoticDawg - avatar
+ 5
Antonio sorry to say, that will not work at all. Do take a few minutes to think about how recursion is supposed to work. Take out the Fact() function. Then use the Fact() function as a pattern for how to code the Point() function. And that first returm somehow slipped outside the main() end brace }.
14th Dec 2020, 11:43 PM
Brian
Brian - avatar
+ 5
Antonio So, with recursion you need to start with your base case(s). What to return at the limits of the recursion. For instance if the user passed 0 levels or only 1 level or when the change in the recursive call to the method has reached a point that it should no longer recursively call the method. (Again a value of 1 or 0. Then you need your recursive call of the method. The recursive call needs to make a change in the value moving the value closer toward a base case with each recursive call. In this case if the levels value entered was 5 you'd need to call the method again with a lower value than 5 each time until the value has reached a base case. So, if you add the current levels value to the next lower value with each recursive call you will reach your total desired value.
14th Dec 2020, 11:46 PM
ChaoticDawg
ChaoticDawg - avatar
15th Dec 2020, 12:13 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Add a few lines in Points() to make a recursive calculation. Hint: levels + (levels-1) + ((levels-1)-1) + ... + 0. To review recursion see: https://www.sololearn.com/learn/CSharp/2610/
14th Dec 2020, 11:24 PM
Brian
Brian - avatar
+ 4
Just implement that as a recursive function: int Points(int levels) { if (levels == 1) return 1; return levels + Points(levels - 1); }
16th Dec 2020, 2:43 AM
Erick Ruh Cardozo
Erick Ruh Cardozo - avatar
+ 3
Make an int variable to hold the total or sum of the levels. Use a loop to loop from 1 to level. In the loop add the value of the level to the total. Return the total.
14th Dec 2020, 11:27 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
There is a problem on first Return. Mmm... using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } return static int Points(int levels) { //your code goes here static int Fact(int num) { if (num == 1) { return 1; } return num * Fact(num + 1); } } } }
14th Dec 2020, 11:36 PM
Antonio
Antonio - avatar
+ 3
static int Points(int levels) { int sum=0; //your code goes here for(int i=1; i<=levels; i++){ sum+=i; } return sum; }
30th Jun 2021, 4:03 PM
Shanmuga Priya
Shanmuga Priya - avatar
+ 2
ChaoticDawg your idea would work, but the Code Coach asks for it to be recursive. Antonio looks like you understand the idea. But the levels argument in Points() needs to be decremented with each call, instead of incremented. And it should return 0 at the end if levels is zero (or less?).
14th Dec 2020, 11:32 PM
Brian
Brian - avatar
+ 2
Fill in the blanks to select the element with id="text" and change its content to "Hi". ob = document.getElementById(" "); .innerHTML = "Hi"; can you help me out please?
16th Dec 2020, 6:42 PM
Elizebeth Cartmel
+ 2
Play again you 'll scusse
16th Dec 2020, 7:57 PM
Salma Ahmed Haren Timuni
Salma Ahmed Haren Timuni - avatar
+ 2
Muhammad Burhan, Elizebeth Cartmel your comments are off topic. Please start a new Q&A post if you want help with your own program.
16th Dec 2020, 8:06 PM
Brian
Brian - avatar
+ 1
static int Fact(int num) { if (num == 1) { return 1; } return num + Fact(num + 1); }
14th Dec 2020, 11:25 PM
Antonio
Antonio - avatar
0
Right?
14th Dec 2020, 11:25 PM
Antonio
Antonio - avatar
0
More help , i am noob
14th Dec 2020, 11:28 PM
Antonio
Antonio - avatar
0
Therefore?
14th Dec 2020, 11:32 PM
Antonio
Antonio - avatar
0
Ok ChaoticDawg but can you send me a code and i study it . Thank you in advance
14th Dec 2020, 11:53 PM
Antonio
Antonio - avatar
0
One line solution using lambdas and ternary operator static Func<int, int> Points = levels => levels == 1 ? 1 : levels + Points(levels - 1);
20th Aug 2021, 7:44 AM
David García Prados