C
C#•7mo ago
Array

Recursion, help finding the base case?

private void btnRepeat_Click(object sender, EventArgs e)
{


}

string Repeat(char c, int n)
{
return string.Empty;

}
private void btnRepeat_Click(object sender, EventArgs e)
{


}

string Repeat(char c, int n)
{
return string.Empty;

}
I think the idea is that if c is 1 letter, it is the base case and then after it increases. Not sure exactly how to implement it, very new to coding
No description
44 Replies
Angius
Angius•7mo ago
Do it with a loop, first
Array
ArrayOP•7mo ago
for (int i = 0; i < n; i++) { string Result = Repeat(c,n); } I think something like this
Angius
Angius•7mo ago
No, not really This is mixing recursion with loops Just use a loop
Array
ArrayOP•7mo ago
string.Empty = Result for (int i = 0; i < n; i++) { Result += C; } return Result; ?
Angius
Angius•7mo ago
Almost
Array
ArrayOP•7mo ago
😦 whats the error
Angius
Angius•7mo ago
string.Empty = Result
Array
ArrayOP•7mo ago
Hmm I'm a bit lost
Angius
Angius•7mo ago
type name = value Not value = name
Array
ArrayOP•7mo ago
Result = string.Empty ?
Angius
Angius•7mo ago
Assuming Result variable already exists If not, it's missing a type
Array
ArrayOP•7mo ago
What do you mean by type?
Angius
Angius•7mo ago
The type of the variable C# is a statically and strongly-typed language Everything has a type It could be an int, or a bool, or a DateTime or a Person
Array
ArrayOP•7mo ago
Result can be a string then right?
Angius
Angius•7mo ago
Sure
Array
ArrayOP•7mo ago
Now we have to look at recursion I have to practice because I am a bit lost with that
Angius
Angius•7mo ago
You seem to be lost with C# in general So, yeah, true
Array
ArrayOP•7mo ago
Yeah 😭 It's rough out here
Angius
Angius•7mo ago
Well, the general gist of recursion, is that it's a method that calls itself and uses its own return value
Array
ArrayOP•7mo ago
Yeah My teacher did an example with factorial that helped me Bascially when it was 0 or 1 it was the special case to where it stored those values on the stack for cases so it could trace back to it So like factorial 2 would use the base case of factorial 1 times 2 then 3 would take from prior
Angius
Angius•7mo ago
Yep So, adding to string should be even easier Each iteration of the method would simply return a string one longer Until a desired lenght is reached Then the method will not recurse
Array
ArrayOP•7mo ago
Yeah
private void btnFact_Click(object sender, EventArgs e)
{
long n = long.Parse(txtNumber.Text);
long Result = Factorial(n);

MessageBox.Show(n + "!= " + Result);

}

private long Factorial(long n)
{
//base case
if (n == 1)
return 1;
else
{
//recursive case
return n * Factorial(n - 1);
}


}

private void btnFact_Click(object sender, EventArgs e)
{
long n = long.Parse(txtNumber.Text);
long Result = Factorial(n);

MessageBox.Show(n + "!= " + Result);

}

private long Factorial(long n)
{
//base case
if (n == 1)
return 1;
else
{
//recursive case
return n * Factorial(n - 1);
}


}

@ZZZZZZZZZZZZZZZZZZZZZZZZZ Here is how my teacher did it So we had a base case of 1
Angius
Angius•7mo ago
Yeah Adapting it to a string should be easy, then a base of an empty string And instead of multiplying, you'd be adding
Array
ArrayOP•7mo ago
I'm a bit confused on how to format the if statement thoug I said earlier the base case is like if you have 1 letter it is the base case if not you increase
Angius
Angius•7mo ago
Yeah, that
Array
ArrayOP•7mo ago
im just a bit confused on how to implement the return
Angius
Angius•7mo ago
if (theString.Length < n) Or > n Or whichever else works
Array
ArrayOP•7mo ago
Wouldn't it be = ? == 1
Angius
Angius•7mo ago
If you want to check exact equality, sure Probably not 1 though
Array
ArrayOP•7mo ago
Actually I don't even fully understand the base case Hmmm
Angius
Angius•7mo ago
Well, you need to start with something
Array
ArrayOP•7mo ago
I was thinking if it was 1 character but really I don't know what it would return - like for factorial I know that if you do factorial 1 or 0 it is just 1 and then AFTER you can extend off that I guess if you have 1 character you can always draw back to that?
Angius
Angius•7mo ago
You can have zero characters That's what string.Empty is Or an empty string like ""
Array
ArrayOP•7mo ago
I guess that could maybe be a basecase too should i make 2 if statements?
Angius
Angius•7mo ago
Why?
Array
ArrayOP•7mo ago
for the two base cases, if they have 0 or 1 character
Angius
Angius•7mo ago
Why would there be two? You start with an empty string Then you keep adding to it Until its length reaches a desired length One base — empty string One check — string reached the desired length
Array
ArrayOP•7mo ago
So if n == 0; I will return an string.empty string?
Angius
Angius•7mo ago
Yeah
Array
ArrayOP•7mo ago
Okay
string Repeat(char c, int n)
{
//base case - if there are no characters, return an empty string (zero)
if (n == 0)
{
return string.Empty;
}

}
string Repeat(char c, int n)
{
//base case - if there are no characters, return an empty string (zero)
if (n == 0)
{
return string.Empty;
}

}
I hav this dowm, is it okay? @ZZZZZZZZZZZZZZZZZZZZZZZZZ now i do an e;se statement saying return c + Repeat(c + 1) ?
Angius
Angius•7mo ago
Yep
Array
ArrayOP•7mo ago
wow I am actually choked shocked thank you i cant believe i kinda got one
Core
Core•7mo ago
The else statement is redundant, because if n != 0 it will simply execute the code after the if (n == 0) statement
Array
ArrayOP•7mo ago
this was the correct ans, got it wrong
No description
Want results from more Discord servers?
Add your server