Name: Anonymous 2011-02-03 17:31
im making a program using recursion to try and make a rectangle.
it .
heres the instructions
1. Rectangle Printer: Write a recursive function that draws a rectangle to the screen. Ask the user to enter positive integers for the height and width of the rectangle (error trap for bad input). Then ask the user for a character to use to print the rectangle (no error trap required). Then print the rectangle to the screen. No loops are allowed when printing the rectangle.
so if it said 5 * 5 with the filer a it would look like this
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
heres my code point me in the right direction
#include <iostream>
using namespace std;
void Rectangle(int height, int width, char filler);
void main()
{
int width;
int height;
char filler;
cout << "Printing a rectangle:\n\n";
cout << "Enter the height:";
cin >> height;
while (height <= 0)
{
cout << "The height should be greater than zero\n\n";
cout << "Enter the height:";
cin >> height;
}
cout << "Enter the width";
cin >> width;
while (width <= 0)
{
cout << "The width should be greater than zero\n\n";
cout << "Enter the width:";
cin >> width;
}
cout << "Enter a fill character:";
cin >> filler;
Rectangle(height, width, filler);
}
void Rectangle(int height, int width, char fill)
{
if(width > 0)
{
cout<<fill;
Rectangle(height, width - 1, fill);
}
if(height > 0)
{
cout << "\n";
Rectangle(height - 1, width, fill);
}
}
it .
heres the instructions
1. Rectangle Printer: Write a recursive function that draws a rectangle to the screen. Ask the user to enter positive integers for the height and width of the rectangle (error trap for bad input). Then ask the user for a character to use to print the rectangle (no error trap required). Then print the rectangle to the screen. No loops are allowed when printing the rectangle.
so if it said 5 * 5 with the filer a it would look like this
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
heres my code point me in the right direction
#include <iostream>
using namespace std;
void Rectangle(int height, int width, char filler);
void main()
{
int width;
int height;
char filler;
cout << "Printing a rectangle:\n\n";
cout << "Enter the height:";
cin >> height;
while (height <= 0)
{
cout << "The height should be greater than zero\n\n";
cout << "Enter the height:";
cin >> height;
}
cout << "Enter the width";
cin >> width;
while (width <= 0)
{
cout << "The width should be greater than zero\n\n";
cout << "Enter the width:";
cin >> width;
}
cout << "Enter a fill character:";
cin >> filler;
Rectangle(height, width, filler);
}
void Rectangle(int height, int width, char fill)
{
if(width > 0)
{
cout<<fill;
Rectangle(height, width - 1, fill);
}
if(height > 0)
{
cout << "\n";
Rectangle(height - 1, width, fill);
}
}