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.
#include <iostream>
usingnamespace 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;
}
}
#include <iostream>
usingnamespace 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 << " ";
#include <iostream>
usingnamespace 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()
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.
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.
#include <iostream>
usingnamespace 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++;
}
}