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;
    }
}

Recommended Answers

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 !!

Please help me.....!!! makes the difference between a C and a B.

I just don't understand where I would add a "Margin" to???

I've tried all of the variables

A margin is just blank space around an object on a page (or a screen). Since your output goes from left to right, the only margin you care about is the left-hand margin... How would you add a margin of, say, 5 spaces to a text document in Windows Notepad..?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.