# Thread: drawing a square with loops

1. ## drawing a square with loops

Hi all,

Pretty simple task to do, but for some reason when run the square is a little off. The code I have done so far is:

Code:
```#include <iostream>
#include <string>
#include <conio.h>

using std::cin;		using std::endl;
using std::cout;	using std::string;

int main()

{

const int cols = 10;
const int rows = 10;

for (int r = 0; r != rows; ++r)
{
int c = 0;

while (c != cols)
{
if (r == 0 || r == rows - 1 || c == 0)
{
cout << "*";
}

else if ( c == cols - 1)

{
cout << "*" << endl;
}

else
{
cout << " ";
}

++c;
}

}

getch();
return 0;

}```
When run, the only problem is the top row has 11 * symbols and then a space of about 10 and then one more * symbol. Could anybody shed any light as to where I have gone wrong.

Thanks,

Darren.

2. I hav sorted it now by drawing the top and bottom lines seperately.

Code:
```#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main()

{

// declare border length of square

const int sides = 10;

// draw top line
for (int r = 0; r != sides; ++r)
{
cout << "*";
}

cout << endl;

int row = 0;

// draw middle lines
while (row < sides)
{

int col = 0;

while (col < sides)
{
++col;
if (col == 1 || col == sides)
{
cout << "*";
}

else
{
cout << " ";
}

}
cout << endl;
++row;
}

// draw bottom line
for (int r = 0; r != sides; ++r)
{
cout << "*";
}

getch();
return 0;

}```
Is that a better way to do it?

Thanks.

3. Code:
```// draw top line
for (int r = 0; r != sides; ++r)
{
cout << "*";
}```
A little misleading when I first read it because I saw r thinking rows but in reality your only printing a single row.

Why not have one loop that controls ALL the rows and inside of the loop check which row you are on. An inner column loop can check exactly what * to print and how many depending on what row you are on. (You already have most of it)

I believe this is the first thing covered in the Accelerated C++ Book. They have a good solution there.

4. Originally Posted by gamer4life687
Code:
```// draw top line
for (int r = 0; r != sides; ++r)
{
cout << "*";
}```
A little misleading when I first read it because I saw r thinking rows but in reality your only printing a single row.

Why not have one loop that controls ALL the rows and inside of the loop check which row you are on. An inner column loop can check exactly what * to print and how many depending on what row you are on. (You already have most of it)

I believe this is the first thing covered in the Accelerated C++ Book. They have a good solution there.

Thanks for the response. It is Accelerated C++ that I am currently working through, however no solutions to the problems are provided.

Will have a go at doing it like you have suggested also.

Thanks.

5. The code needed for drawing the top and bottom rows is different than that needed for the middle rows. Therefore, it makes sense to separate it. It could be crammed into one loop, but it doesn't make the code any clearer. (If anything it runs a bit more slowly since it has to test for top/bottom rows on every row.)

Code:
```int width = 10, height = 10;

for (int h = 0; h < height; ++h) {
if (h == 0 || h == height - 1) {
// Top and bottom rows.
for (int w = 0; w < width; ++w) {
std::cout << "*";
}
std::cout << "\n";
} else {
// Middle rows.
std::cout << "*";
for (int w = 0; w < width - 2; ++w) {
std::cout << " ";
}
std::cout << "*\n";
}
}```

6. Hi all.

Code:
```#include<iostream.h>

int main()
{
int side;

cout <<"Enter Side: "; cin>>side;

side -= 1;

for (int i = 0; i<=side; i++)
{
for(int j=0; j<=side; j++)
cout<<(((i*j==0) || ((i*j)%side==0))? "*" : " ");

cout<<endl;
}

return 0;
}```

7. Seriously? You wanted to brag so bad that you found an 18 month old thread?

Soma

8. Originally Posted by Gabby
Hi all.

Code:
```#include<iostream.h>

int main()
{
int side;

cout <<"Enter Side: "; cin>>side;

side -= 1;

for (int i = 0; i<=side; i++)
{
for(int j=0; j<=side; j++)
cout<<(((i*j==0) || ((i*j)%side==0))? "*" : " ");

cout<<endl;
}

return 0;
}```
Should i be impressed by the fact that you solved it in such a complicated and unnecessary way or by that you're using the good ol' C++ libraries?

9. Ops, didn't notice it. Sorry!

10. Originally Posted by Gabby
Code:
```	int side;

cout <<"Enter Side: "; cin>>side;

side -= 1;```
that is such gorgeous code. now go write it in LISP in one line. someday our whole computers will be one giant ternary operation!

11. Ternary?! Why ternary?... Not sure i get what you mean.