•5.20 (Displaying a Rectangle of Any Character)
–Modify the function created in Exercise 5.19 to form the rectangle out of whatever character is contained in character parameter fillCharacter.
–Thus if the sides are 5 and 4, and fillCharacter is "@", then the function should print the following:
Enter a character to fill the rectangle: a
Enter sides: 4 5
a a a a a
a a a a a
a a a a a
a a a a a
#include <iostream>
using namespace std;
void displaySquare(int rows, int cols, char fillCharacter) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << fillCharacter;
if (j == cols - 1) cout << "\n";
else cout << " ";
}
}
}
int main() {
int rows, cols;
char c;
cout << "Enter a character to fill the rectangle: ";
cin >> c;
cout << "Enter sides: ";
cin >> rows >> cols;
displaySquare(rows, cols, c);
return 0;
}
Comments
Leave a comment