Square with while statement

I am struggling with this program. It reads the size of the side of the square then it should print a hollow square of that size of asteriks and blanks.
I have no idea how I should write it to get the square.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using namespace std;

int main(){
  int x;
  int column;
  cout << "Please enter a number for the size of the side of the square : ";
  cin >> x;
  while(x >= 0){
    column = 1;
    while(column<=x){
      if(column%x==0){
        cout << "*";
      }
      else{
        cout << " ";
      }
      column++;
    }
    x--;
    cout << endl;
  }
}
if this were a rectangle, you would need two loops, and we could work off that.
it isnt. its a square.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
  int x;
  int column;
  cout << "Please enter a number for the size of the side of the square : ";
  cin >> x;

for(int i = 0; i < x*x; i++)
  {
      if((i+1)%x==0 || i%x==0 || i < x || i > x*x-x)
       cout << "*";
     else 
       cout << " ";    
    if( (i+1)%x ==0)  //+1 makes the modulo work properly. 
     cout << endl;
  }
}


that is the solid square. now can you make it hollow ? I did it with a single if / else around the cout << *:

if(what or what) //leaving this for you to do.
cout << "*";
else cout << " ";
Last edited on
Thank you for the help.
I did it as you advised. I got something which is a square but it is not the proper one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
using namespace std;

int main(){
  int x;
  int column;
  cout << "Please enter a number for the size of the side of the square : ";
  cin >> x;
  int  i=0;
  
  while(i< x*x){
    if (i/x<1 || (x*x)-x-1<i){
      cout << "*";
    }
    else{
        cout<< " ";
    }
    if( (i+1)%x ==0)  //+1 makes the modulo work properly. 
     cout << "*" << endl <<"*";
    i++;
  }
}
> if this were a rectangle, you would need two loops, and we could work off that.
I think that using just one loop is over-complicating the problem
you may use two loops and use the same limit for both


> if (i/x<1 || (x*x)-x-1<i)
I'm not going to bother with that.

to create the hollow square, you have two patterns
'***...**' for the top and bottom
'* ... *' for the middle
you may think of it like this
1
2
3
4
print_top() //a sequence of 'x' asterisk
repeat(¿how many times?)
   print_middle() //a sequence of one asterisk, then some spaces (¿how many?) and another asterisk
print_bottom() //a sequence of 'x' asterisk, same as print_top() 
¿can you code those functions?
Last edited on
overcomplicating the problem? It was intended to make it shorter/simpler!!
I have added the hollowed out code to mine. (or will in a moment if you are extra quick on the read).

Just extending the condition and you have the top, bottom, and sides covered.
Ill give you that the if statement is complex at first read, but break it down.
ask yourself "when should it print an *". Your answer will be 'all of the top row, all of the bottom row, the first character of each row, and the last character in each row (not counting the end of line). 4 conditions. Then turn each one into code, and or them together.

however you do it, see if the hollowed out code I put in helps.
It takes a while to be comfortable with the % operator. I advise you to get there asap though, it is extremely useful.
Last edited on
Building on what ne555 said, and if you've learned about functions, then try writing this function:
1
2
// Print ch to cout n times
printNtimes(char ch, int n);


Once you have this, you can use it to help implement ne555's print_top(), print_middle() and print_bottom()
Thank you for your help. I have not learned about "printNtimes" function yet but I think it would be more clearer for me. I am trying to do it with the if and while statement. That is why I was trying to do it as Jonnin advised. I got the bottom and the top of the square but I will need some help to get those sides.

Here is the bottom and the top of the square:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int main(){
  int x;
  int column;
  cout << "Please enter a number for the size of the side of the square : ";
  cin >> x;
  int  i=0;
  
  while(i< x*x){
    if (i/x<1 || (x*x)-x-1<i){
      cout << "*";
    }
    else{
        cout<< " ";
    }
    if( (i+1)%x ==0)  //+1 makes the modulo work properly. 
     cout << endl;
    i++;
  }
}
Last edited on
you almost have it, you just need to express the 4 conditions I said for when to draw your *s.

so you need 2 more conditions...

if (i/x<1 || (x*x)-x-1<i || i%x==0 || (i+1)%x==0)
notice how each condition correspond to a border
1
2
3
4
5
6
if (
   i/x<1 //top
   || (x*x)-x-1<i //bottom
   || i%x==0 //left
   || (i+1)%x==0 //right
)
Thank you, I got it
Topic archived. No new replies allowed.