+ 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
29 Respuestas
+ 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);
}
}
}
+ 5
Brian Ya, I was just re-reading the question. (Too many distractions here RN). I missed the part where it asked for recursion.
+ 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 }.
+ 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.
+ 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/
+ 4
Just implement that as a recursive function:
int Points(int levels)
{
if (levels == 1)
return 1;
return levels + Points(levels - 1);
}
+ 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.
+ 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);
}
}
}
}
+ 3
static int Points(int levels)
{
int sum=0;
//your code goes here
for(int i=1; i<=levels; i++){
sum+=i;
}
return sum;
}
+ 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?).
+ 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?
+ 2
Play again you 'll scusse
+ 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.
+ 1
static int Fact(int num) {
if (num == 1) {
return 1;
}
return num + Fact(num + 1);
}
0
Right?
0
More help , i am noob
0
Therefore?
0
Ok ChaoticDawg but can you send me a code and i study it . Thank you in advance
0
One line solution using lambdas and ternary operator
static Func<int, int> Points = levels => levels == 1 ? 1 : levels + Points(levels - 1);