I've got an assignment due in about 5 days, and this is the last task I can't solve. I've gotten it to print
****
***
**
*
*
but I need 3 more lines,
**
***
****
so it's a symmetrical pattern.
My code so far is below. If anyone could give me pointers or examples to solve this, that'd be great. The instructor also told us to AVOID any FOR LOOPS.
void OutputStarPattern(ofstream& fout, int numberOfLines)
{
char symbol;
//The symbol to be output is a star
symbol = '*';
//Set width to amount of lines and fill to symbol
fout << setfill(symbol) << setw(numberOfLines);
fout << symbol << endl;
//Determine if the numberOfLines is greater than 0
if (numberOfLines > 0)
{
//Output symbol to the output file and recursively call
fout << setfill(symbol) << setw(--numberOfLines);
fout << symbol << endl;
OutputStarPattern(fout, --numberOfLines);
}
//Else exit the function
else
{
return;
}
}
Please use code tags when posting code. Highlight the code and click the <> button to the right of the edit window.
When writing recursive functions, I always code them like this:
1 2 3 4 5
if (base_case) {
do_the_base_case;
} else {
do_the_recursive_case;
}
By structuring the code like this, it's clear which part is which. More important, but putting the base case first, you won't forget it. Part of what's wrong in your code is confusion over what gets printed in which case.
For your code, I'd pass the number of stars instead of the number of lines.
So in pseudocode, you want to do this:
1 2 3 4 5 6 7 8 9 10 11
void
OutputStarPattern(std::ostream & fout, int numberOfStars)
{
if (numberOfStars <= 0) {
return; // base case
} else {
// print numberOfStars on a line
OutputStarPattern(fout, numberOfStars-1);
// print numberOfStars again.
}
}
That just returns this pattern
****
****
***
***
**
**
*
*
*
*
*
**
***
but i need 4. I really don't understand how to do this because you can't increment numberOfStars again without messing up the function and having infinite recursion.
I've seen methods like the one you're showing me, but they all use two for loops.
void OutputStarPattern(ofstream& fout, int numberOfStars)
{
char symbol;
//The symbol to be output is a star
symbol = '*';
//Set width to amount of lines and fill to symbol
fout << setfill(symbol) << setw(numberOfStars);
fout << symbol << endl;
//Determine if the numberOfLines is greater than 0
if (numberOfStars <= 0)
{
return;
}
//Else exit the function
else
{
//Set width to amount of lines and fill to symbol
fout << setfill(symbol) << setw(numberOfStars) << symbol << endl;
OutputStarPattern(fout, --numberOfStars);
fout << setfill(symbol) << setw(numberOfStars) << symbol << endl;
}
}
Looks like frustration got the better of me and had me overlooking that first fout statement. I managed to get it to work with this code. Thanks for the pseudocode and line of thought.
void OutputStarPattern(ofstream& fout, int numberOfStars)
{
char symbol;
//The symbol to be output is a star
symbol = '*';
//Determine if the numberOfLines is greater than 0
if (numberOfStars <= 0)
{
return;
}
//Else exit the function
else
{
//Set width to amount of lines and fill to symbol
fout << setfill(symbol) << setw(numberOfStars) << symbol << endl;
OutputStarPattern(fout, --numberOfStars);
fout << setfill(symbol) << setw(numberOfStars+1) << symbol << endl;
}
}
What if, hypothetically, the setfill+setw is implemented with for loops?
You could use recursion to print numberOfStars stars to a line. Then you would have not one, but two recursive functions, first to produce one line of stars and second to create the desired lines.