Hello! I have a program that generates a rectangle based on width and height.

---I just need to figure out how to center this if the screen was 80 characters wide.------

``````void drawRect(int width, int height) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if ((j == 0) || (j == width - 1)) {
cout << "*";
} else if ((i == 0) || (i == height - 1)) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}
``````

## All 6 Replies

This requires some basic algebra.

80 = 2m + w, where w is the width of the rectangle, m is the width of the margin.

Okay, I think I understand the logic.

I'm so tired right now.....where would that fit in?? would I have to make another function?????

``````void displayStars (int, int);

int main ()
{
int row, column;
char again;

do
{

cout << "Enter the number of rows and columns: ";
cin >> row >> column;

while (column < 2 || column > 60 && row < 2 || row > 60)
{
cout << "Invalid values for rows and columns" <<endl;
cout << "Please enter values between 2 - 60: " ;
cin >> row >> column;
}
cout << endl << endl;
displayStars(row, column);										cout << endl << endl;
cout << " Do you want to create another rectangle? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y');
return 0;
}

void displayStars(int width, int height)
{
for (int across = 0; across < height; across++)
{
for (int down = 0; down < width; down++)
{
if ((down == 0) || (down == width - 1))
{
cout << "*";
}
else if ((across == 0) || (across == height - 1))
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
}``````

Thanks soooo much :)

Think harder. A good night's sleep helps.

Think harder. A good night's sleep helps.

i have thought long and hard..........

It's due tomorrow at 8 AM.

I would love to sleep on it.....but seriously my mental capacity for figuring this out is less than a two-year old.

I've been trying for the past hour......how sad is that !!