Answer to Question #341786 in C++ for LM.

Question #341786

(iii) Write a function void rectangle(int w, int h) to print an open rectangle of asterisks (*). The parameters w and h are the width and the height of the rectangle, expressed in number of asterisks.


1
Expert's answer
2022-05-18T08:33:10-0400
#include <iostream>
using namespace std;
 

void rectangle(int, int);
 
int main()
{
    int width, height;
    
    cout << "Enter the width: " << endl;
    cin >> width;
    
    cout << "Enter the height: " << endl;
    cin >> height;
    
    rectangle(width, height);
}

void rectangle(int w, int h)
{
    
    for (unsigned int i = 1; i <= h; ++i)
    {
        for (unsigned int j = 1; j <= w; ++j)
        {
            if (i == 1 || i == h || j == 1 || j == w)        
                cout << "*";            
            else
                cout << " ";
        }
        cout << endl;
    }
 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS