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

Dani AI

Generated

A compact, practical fix that plugs into the existing code: compute a left margin once (based on an 80-character screen), print that many spaces before each rectangle row, and clamp the margin to zero if the rectangle is wider than the screen. This directly implements the algebra suggested by (80 = 2*m + w) and matches 's plain-English point that a margin is just blank space to the left.

Compute the margin inside the drawing function (or pass screen width in), then print the margin before each row. Example (keeps the drawing logic simple and different from the code already posted):

void displayStars(int width, int height) {
    const int screen = 80;
    int margin = (screen - width) / 2;
    if (margin < 0) margin = 0;
    std::string pad(margin, ' ');

    for (int r = 0; r < height; ++r) {
        std::cout << pad;
        if (r == 0 || r == height - 1)
            std::cout << std::string(width, '*') << '\n';
        else {
            int inner = (width > 2) ? width - 2 : 0;
            std::cout << '*' << std::string(inner, ' ');
            if (width > 1) std::cout << '*';
            std::cout << '\n';
        }
    }
}

Two quick fixes that address gaps in the thread: (1) the while validation in main uses mixed &&/|| and is incorrect because of operator precedence; use while (row < 2 || row > 60 || column < 2 || column > 60). (2) variable naming/order is confusing: the code reads row, column but the function expects (width, height). Either rename variables to cols, rows or call displayStars(column, row) so width/height are consistent.

Notes: integer division yields a left bias when (80 - width) is odd; to bias the other way add 1 before dividing. If the actual terminal width is unknown, allow it to be passed in instead of hard-coding 80 or detect it with platform APIs.

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.