Answer to Question #340556 in C++ for whitewalker

Question #340556

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


1
Expert's answer
2022-05-13T03:26:13-0400
#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;
}

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