Need help with this code.

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:

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

If input is 8 Y

YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY

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

//Exit stage right or left!
return 0;
}
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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main() {
    int isize=0;
    cout << "Enter integer (1-15):\t";
    cin >> isize;

    string myX (isize, 88);  // 88 = ASCII code for 'X'
    for (int i=0; i<isize; i++) cout << myX << endl;
    
return 0;
}

Enter integer (1-15):	10
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX

I actually think of it more as a square really, given that the number of columns and rows is equal, even though it doesn't really look much like one.

Anyway, please clarify if I'm not understanding your problem correctly, and maybe someone else can also help you.
Last edited on
It is a problem from my computer science homework and it asks for any letter and any number from 1-15
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
**
**

This code:
1
2
3
4
5
for(int i=-40, i<-38;i++) {
for(int i=-40; i<-38;i++)
cout << *;
cout << '\n';
}

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;

int main() {
    int size=0;
    int a = 0;

    do {
      cout << "Enter integer (1-15):\t";

      cin >> size;
      if (size >=1 && size <= 15){
        for (size_t j = 0; j < size; j++) {
          for (int i=0; i<size; i++){
            cout << "X" ;
          }
          std::cout << endl;
        }
        a = 1;
      }
      else{
        a = 0;
      }
    }while (a == 0);
return 0;
}


this will do it and control if size is between 1 - 15

good luck :D
Cmon guys it's homework.. well okay. If only people were so eager to solve my math homework ;'(
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.
Topic archived. No new replies allowed.