Write a recursive function writeLine() that writes a character repeatedly to form a line of n characters. For example, writeLine('*',5) should produce the line *****.
1 2 3 4 5 6 7 8 9
int writeLine(char x, int num)
{
cin >> x >> num;
if (num == 1) //anchor case
return x;
elsefor (int i = 0; i <= num; i++)
return writeLine(x, i); //inductive case
}
is this right? I just started recursion and im legitimately confused by it. :S
The for loop shouldn't be necessary with recursion; that's how you would do it iteratively.
Basically, the algorithm for this is to have the function write one character, then call itself with the number i reduced by 1. When i get's low enough, simply end the function (the "base case" or "anchor case" as you called it).
I don't see why you should return anything, why isn't it a void function? Moreover, you are returning a character if num == 1 and an integer otherwise...
Your third line doesn't make a sense. You probably wanted to print something (std::cout) and not get something from the input.
Please reconsider the algorithm Zhuge suggested.
it returns x if num = 1 (because it writes a line of 'x', num times. ) therefor if num = 10, the function would write xxxxxxxxxx. It doesnt return an integer otherwise because it would return a character 'num' times. im confused by your explanation.
Nope. Not correct. Recursion is basically a function recalling itself. First of all, you wanted to have the output as: *****. This means for each recursive function call, only one of the '*' characters should be printed. So obviously, you don't need a std::cin; rather a std::cout for output.
You correctly identified the anchor case if(num == 1) . The anchor case is the last condition that will terminate the recursion and since you had it as 1, it meant that you should be thinking of decreasing your num variable.
[Hint: anytime you implement a recursive function with an iteration, it's almost always an indication that you are doing it wrongly]
Below is a simple recursive program to help you understand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//program to multiply two non-negative numbers:
#include <iostream>
int multiply(int firstNumber, int secondNumber)
{
if(secondNumber == 1)
return firstNumber;
return firstNumber + multiply(firstNumber, secondNumber - 1);
}
int main(int argc, char* argv[])
{
std::cout<<multiply(2,5)<<std::endl;
return 0;
}
I didn't actually explain anything. I just pointed out that the return value you specified for the function is int while you actually return a character. I suggested you make the function a void function because you don't really need to return anything.
Better? Almost. You might want to step through it with a debugger to check your logic and understand what your code is actually doing at the inductive step.
It is better but again, your assignment asks you to print the characters and not return them. Thats the reason I suggest you use void, but you kept the return statements which is wrong. Think about how you could replace your return statements with printing.
I just learned about recursion a few weeks ago and never really spent much time on it, so I decided to code this just for a little practice and review. Only took me about 5 minutes, here's my final code...
/***************************************************************
Write a recursive function writeLine() that writes a character
repeatedly to form a line of n characters. For example...
writeLine('*',5) should produce the line *****.
****************************************************************/
#include <iostream>
usingnamespace std;
void WriteLine(char, int);
int main()
{
char myChar;
int myInt;
cout << "Please enter a character to print: ";
cin >> myChar;
cout << "How many times to print: ";
cin >> myInt;
WriteLine(myChar, myInt);
cout << endl;
return 0;
}
void WriteLine(char i, int j)
{
if (j >= 1)
{
cout << i;
WriteLine(i, j - 1);
}
}