Write a C++ program that reads in the dimensions of a rectangle and then prints
ID: 3823350 • Letter: W
Question
Write a C++ program that reads in the dimensions of a rectangle and then prints a filled rectangle of the specified dimension one character at a time. The outline of the rectangle should be of whatever characters the user specifies for the border and the interior respectively. The program should work for rectangles of all sizes between 3 and 25. For example, if your program inputs a size of 7 for length, 7 tor width, an asterisk (e.g. *) as the border character and a hat (e.g.^) for the fill character, it should output a 7 times 7 rectangle as: Ignore the fact that it does not look uniform -it is a 7 times 7 box. The program should prompt the user as follows: Would you like to draw a box (y/n)? If the user answers y or Y for yes, the program should then prompt the user for: The dimensions (e.g. row, column) of the rectangle. Assume that the user will input data within range. The character to draw the border of the box (any keyboard character is acceptable). The character to fill the box with (any keyboard character is acceptable). The program should then print out the specified sized box using the specified character for the border and should continue until the user enters an n or N when prompted to draw. Once you are satisfied that your program compiles and executes successfully, execute again after invoking the script command and e-mail the resulting file to me.Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n;
char c1, c2, choice = 'y';
while(choice =='y' || choice =='Y') {
cout<<"Enter the size: ";
cin >> n;
cout<<"Enter the border character: ";
cin >> c1;
cout<<"Enter the fill character: ";
cin >> c2;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==1 || i == n){
cout<<c1<<" ";
}
else{
if(j==1 || j == n){
cout<<c1<<" ";
}
else{
cout<<c2<<" ";
}
}
}
cout<<endl;
}
cout<<"Do you want to draw a box(y/n): ";
cin>>choice;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the size: 7
Enter the border character: *
Enter the fill character: ^
* * * * * * *
* ^ ^ ^ ^ ^ *
* ^ ^ ^ ^ ^ *
* ^ ^ ^ ^ ^ *
* ^ ^ ^ ^ ^ *
* ^ ^ ^ ^ ^ *
* * * * * * *
Do you want to draw a box(y/n): y
Enter the size: 5
Enter the border character: *
Enter the fill character: ^
* * * * *
* ^ ^ ^ *
* ^ ^ ^ *
* ^ ^ ^ *
* * * * *
Do you want to draw a box(y/n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.