Draw square

Hello There,

How do i Write a program that draw a square of stars ( * ), the length of square will be entered by the user. and i have to use a void function to do it inside the function "void draw_square(int len)", where len is the formal parameter that will be used to specify the length of the square.

Please check the image of that illustrates an example of the intended output
https://imgur.com/AG3kMq8

i literally have no idea how to start coding it, all i know that i need to use for loop i think, so some help here?
Hello RaiN3772,

No you need 2 for loops or more to the point a nested for loops. The first will control the rows and the inner for loop will control the columns.

I would first get it to draw the square then work on adding the (/\). That should be adjusting the inner for loop to print the (/\) as needed based on what row you are printing.

Andy
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
int main()
{
    int numstars{};
    std::cin >> numstars;
    for(int i = 0; i < numstars; ++i) std::cout << std::string(numstars, '*') << '\n';
}
Last edited on
@TheToaster,

I like that idea, but based on
i literally have no idea how to start coding it, all i know that i need to use for loop i think
I tried to keep it simple for now until I see some code to work with.

Andy
@RaiN3372
Based on what Andy said above, I will explain what my code does. std::string is a type provided by the C++ standard that stores strings, hence the name. There is a "constructor" that initializes the string with a count argument and a char argument. A constructor is similar to a function, but you will learn about them when you learn about classes.

So, the code std::string(numstars, '*') will create a "string" by calling its constructor. This particular constructor has two parameters that take a count value and a character, and generates a string accordingly. Thus, the final string will be a string that has numstars characters of type '*'. We then append a newline at the end (using '\n') and repeat it in a for loop for numstars lines.

If you wanted to do it without using std::string, you could use a nested for loop as Andy said above:
1
2
3
4
5
for(int i = 0; i < numstars; ++i)
{
    for(int j = 0; i < numstars; ++j) std::cout.put('*');
    std::cout.put('/n');
}

The inner for loop will output numstars (*) characters. Once it breaks out of that loop, it will print a new line and restart the inner loop. This will happen numstars times. Thus you are left with a square of numstars*numstars, with '*' characters.
I think (as @Handy Andy) I'd use a nested for loop (outer loop for rows, inner loop for elements within a row), with
if ... else if ... else
structure to output chars (one of '\\', '/', '*').

Watch out for even- and odd-length sides.
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
29
30
31
32
#include <iostream>
using namespace std;

void draw_square(int len) {
	for (int a = 0, j = 0; a < len; a++) {
		for (int i = 0; i < len; i++) {
			if (i == j) {
				cout << '\\';
			}
			else if(i == (len - 1) - j) {
			    cout << '/';
			}
			else {
				cout << '*';
			}
		}
		j++;
		cout << endl;
	}
}



int main()
{
	int size;
	std::cin >> size;

	draw_square(size);

	return 0;
}
Last edited on
oh cmon guys, i didnt expect that much of replies and explanation, really much helpful,

also i coded as andy said, it was a good idea to put for loop inside a for loop, i coded it and it's working as i expected

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
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

void draw_square(int lenght) {

	for (int loop = 1; loop <= lenght; loop++) {
		for (int v = 1; v <= lenght; v++) {
			if (loop == v && v == (lenght + 1) - loop) {
				cout << "| ";
			}
			else if (loop == v) {
				cout << "\\ ";
			}
			else if (v == (lenght + 1) - loop) {
				cout << "/ ";
			}
			else {
				cout << "* ";
			}
		}
		cout << endl;
	}

}



int main() {
	int lenght;
	cout << "Please enter the Square Lenght: ";
	cin >> lenght;
	draw_square(lenght);


	return 0;
}

also i'm looking for a improved way to that code.

thank you guys

PS: Manga i saw your code late, its exaclty the same code i made, that what i was looking for lmao, thank you anyway
Last edited on
Hello RaiN3772,

Now that you have it working, if I may offer some suggestions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for (int row = 1; row <= length; row++)
{
	for (int col = 1; col <= length; col++)
	{
		if (row == col && col == (length + 1) - row)
		{
			std::cout << "| ";
		}
		else if (row == col)
		{
			std::cout << "\\ ";
		}
		else if (col == (length + 1) - row)
		{
			std::cout << "/ ";
		}
		else 
		{
			std::cout << "* ";
		}
	}

	std::cout << std::endl;
}

When it comes to giving the loop iterator a name "i" and "j" are most often used, but if you call then "row" and "col" you will find the rest of your code easier to understand and follow. Making the code easy to read and understand will first benefit you then others later. Also some well placed blank lines will make the code easier to read, e.g.,
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	int length;

	cout << "Please enter the Square's Length: ";
	cin >> length;

	draw_square(length);

	return 0;
}

With Time and practice you will figure out what works best for you.

I am guessing English is not your first language because it is spelled "length" to be correct. To the compiler is makes no difference as long as it is spelled the same everywhere you use it.

Your use of the {}s is fine. Just be consistent in their use. Do not mix different styles. As you notice the style I use is easier to read and that pert is what is most important.

You can see different styles of {}s at https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements The "Allman" style is what I prefer, but you are free to use what you have learned and are use to. I am not saying that you need to change what you have learned.

Just a small thing. When an if/else if/else only has one line the {}s are not needed. Some will use them in case later they need to add something. Either way it makes no difference. This also applies to for loops and while loops.

Andy
Hello Andy, yes my english isn't my main language, so sometimes its hard to understand the whole thing since its not my first language, and i try to understand any code by trying it out using some knowledge from here and there, also when it comes to the final code i try to optimize it as much as i can, rename every varble, make it easier, section it.

and i know if the "if statement" is only one line no need for "{}" but i prefer to use it anyway i still dont know why, maybe im used to it.

any way thank you very much
and i know if the "if statement" is only one line no need for "{}" but i prefer to use it anyway i still dont know why, maybe im used to it.

You're absolutely right to do so. It's good defensive coding practice.
Topic archived. No new replies allowed.