In: Computer Science
This is a straightforward program that will "draw" a rectangle using any character selected by the user. The user will also provide the width and height of the final rectangle. Use a nested for loop structure to create the output.
Input:
This program will draw a rectangle using your character of
choice.
Enter any character: [user types: *]
Enter the width of your rectangle: [user types: 20]
Enter the height of your rectangle: [user types: 5]
Output:
********************
********************
********************
********************
********************
Notes and Hints:
1) You MUST use a nested For Loop in your solution
Starter Code:
// VARIABLES
// INPUT
std::cout << "This program will draw a rectangle using your
character of choice.\n";
std::cout << "Enter any character: ";
std::cout << std::endl;
std::cout << "Enter the width of your rectangle: ";
std::cout << std::endl;
std::cout << "Enter the height of your rectangle: ";
std::cout << std::endl;
// DRAW RECTANGLE WITH NESTED LOOP
std::cout << YOUR_CODE;
std::cout << std::endl; // Creates new row after inner loop
finishes all columns in the current row
Code:
// Online C++ compiler to run C++ program online
#include <iostream>
int main() {
// VARIABLES
int width;
int height;
char ch;
// INPUT
std::cout << "This program will draw a rectangle using your character of choice.\n";
std::cout << "Enter any character: ";
std::cin>>ch;
std::cout << std::endl;
std::cout << "Enter the width of your rectangle: ";
std::cin>>width;
std::cout << std::endl;
std::cout << "Enter the height of your rectangle: ";
std::cin>>height;
std::cout << std::endl;
// DRAW RECTANGLE WITH NESTED LOOP
for(int i = 1; i <= height; i++)
{
for(int j = 1; j <= width; j++ )
{
std:: cout<<ch;
}
std::cout << std::endl; // Creates new row after inner loop finishes all columns in the current row
}
return 0;
}
Screenshots: