C
C#•2mo 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•2mo ago
Do it with a loop, first
Array
Array•2mo ago
for (int i = 0; i < n; i++) { string Result = Repeat(c,n); } I think something like this
Angius
Angius•2mo ago
No, not really This is mixing recursion with loops Just use a loop
Array
Array•2mo ago
string.Empty = Result for (int i = 0; i < n; i++) { Result += C; } return Result; ?
Angius
Angius•2mo ago
Almost
Array
Array•2mo ago
😦 whats the error
Angius
Angius•2mo ago
string.Empty = Result
Array
Array•2mo ago
Hmm I'm a bit lost
Angius
Angius•2mo ago
type name = value Not value = name
Array
Array•2mo ago
Result = string.Empty ?
Angius
Angius•2mo ago
Assuming Result variable already exists If not, it's missing a type
Array
Array•2mo ago
What do you mean by type?
Angius
Angius•2mo 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
Array•2mo ago
Result can be a string then right?
Angius
Angius•2mo ago
Sure
Array
Array•2mo ago
Now we have to look at recursion I have to practice because I am a bit lost with that
Angius
Angius•2mo ago
You seem to be lost with C# in general So, yeah, true
Array
Array•2mo ago
Yeah 😭 It's rough out here
Angius
Angius•2mo ago
Well, the general gist of recursion, is that it's a method that calls itself and uses its own return value
Array
Array•2mo 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•2mo 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
Array•2mo 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•2mo 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
Array•2mo 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•2mo ago
Yeah, that
Array
Array•2mo ago
im just a bit confused on how to implement the return
Angius
Angius•2mo ago
if (theString.Length < n) Or > n Or whichever else works
Array
Array•2mo ago
Wouldn't it be = ? == 1
Angius
Angius•2mo ago
If you want to check exact equality, sure Probably not 1 though
Array
Array•2mo ago
Actually I don't even fully understand the base case Hmmm
Angius
Angius•2mo ago
Well, you need to start with something
Array
Array•2mo 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•2mo ago
You can have zero characters That's what string.Empty is Or an empty string like ""
Array
Array•2mo ago
I guess that could maybe be a basecase too should i make 2 if statements?
Angius
Angius•2mo ago
Why?
Array
Array•2mo ago
for the two base cases, if they have 0 or 1 character
Angius
Angius•2mo 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
Array•2mo ago
So if n == 0; I will return an string.empty string?
Angius
Angius•2mo ago
Yeah
Array
Array•2mo 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•2mo ago
Yep
Array
Array•2mo ago
wow I am actually choked shocked thank you i cant believe i kinda got one
Core
Core•2mo ago
The else statement is redundant, because if n != 0 it will simply execute the code after the if (n == 0) statement
Array
Array•2mo ago
this was the correct ans, got it wrong
No description