(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.
#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;
}
}
Comments
Leave a comment