Write a program that asks the user for a positive integer no greater than 15. The program should then display a rectangle on the screen using the character ‘X’. The number entered by the user will be the length of each side of the rectangle. For example, if the user enters 5 X, the program should display the following:
I really do not know how to go about working on this problem.
int main(int argc, char** argv) {
//Set the random number seed
//Declare Variables
int sideSize = 0;
char l;
//Initialize or input i.e. set variable values
cin>> sideSize;
cin>> l;
//Map inputs -> outputs
for (int r = 1; r <= sideSize; r++ )
{
for (int c = 1; c <= sideSize; c++)
{
cout<<l;
}
}
cout<<endl;
//Display the outputs
I understand the requirement for the 'X's, but I don't quite see how you get the rectangle of "Y"s, as you say that the user simply enters an integer (1-15) and then you create an equal sided shape comprised of the letter "X". How does "Y" get into the problem?
"Write a program that asks the user for a positive integer no greater than 15. The program should then display a rectangle on the screen using the character ‘X’. The number entered by the user will be the length of each side of the rectangle."
I must be misunderstanding the question somehow, but from what you've said, surely it's as simple as something like this (although you still need to validate the input, etc):
You get two inputs. One is for the number and second is for the character to be used to display the rectangle.
1 2 3 4
int num;
char symbol;
cin >> num;
cin >> symbol;
Now that you have the number and the symbol, and you know that you need to print a square, try to solve it by yourself and if you aren't able to then show us what you've done and what didn't work.
Hint: Do you know how to display a matrix? It requires two for-loops. Here every element of the matrix is a 'symbol'.
Another big hint:
Here's how you print the pattern
I intentionally obfuscated part of the code but you're to find the pattern (look at how many times the for-loops are iterating), know that you need two for-loops.
Just for clarification. I cannot turn the problem in anymore, I posted this because I was not able to figure it out and I want to be able to understand it.
The only problem with your original program is that the cout << endl is in the wrong place. Just swap it with the line above it to print a newline after each row instead of after all the characters.