Any better draw square algorithms?

I am trying to create a square and in fact I did succeed, however I know there are other simple ways. My way seems a little off the chart and I am sure someone can do better. I remember seeing C++ square code before, but it was created in a 2D array wrapped up in a function that only took two parameters, one for width and height. Wish I can remember how it was done... but oh well, here is my result:

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
#include <iostream>

#define BLOCK std::cout<<(char)'\xdb';

int main(){
	for(int top=1;top<=10;top++){
		BLOCK;
    }
    std::cout<<std::endl;

	for(int height=1;height<=5;height++){
		for(int c=1;c<=1;c++){
			BLOCK;
        }

        for(int right_edge=1;right_edge<=8;right_edge++){
            std::cout<<' ';
        }
		BLOCK;
        std::cout<<std::endl;
    }

	for(int bottom=1;bottom<=10;bottom++){
		BLOCK;
	}

	std::cin.sync();
	std::cin.ignore();
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printHollowSquare(unsigned int width, unsigned int height)
{
	for (int i = 0; i < height; ++i)
	{
		for (int j = 0; j < width; ++j)
		{
			if (i == 0 || j == 0 || i == height - 1 || j == width - 1)
				std::cout << '*';
			else
				std::cout << ' ';
		}
		std::cout << std::endl;
	}
}
closed account (j3Rz8vqX)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <ctime>
#define BLOCK std::cout<<(char)'\xdb';
const int YMAX=10,XMAX=10;//GLOBAL - small simple program!
void option_one()
{
    for(int y=0;y<YMAX;y++)
    {
        for(int x=0;x<XMAX;x++)
        {
            if(y==0||y==XMAX-1)
            {
                BLOCK;
            }
            else
            {
                if(x==0||x==XMAX-1)
                {
                    BLOCK;
                }
                else
                {
                    std::cout<<" ";
                }
            }
        }
        std::cout<<std::endl;
    }
}
void option_two()
{
    char arry[YMAX][XMAX]=
    {
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb'}
    };
    for(int y=0;y<YMAX;y++)
    {
        for(int x=0;x<XMAX;x++)
        {
            std::cout<<arry[y][x];
        }
        std::cout<<std::endl;
    }
}
int main()
{
    clock_t t1,t2;

    t1 = clock();
    option_one();
    t1 = clock()-t1;
    std::cout<<"Option 1 took: "<<float(t1)/CLOCKS_PER_SEC<<" seconds."<<std::endl;

    t2 = clock();
    option_two();
    t2 = clock()-t2;
    std::cout<<"Option 2 took: "<<float(t2)/CLOCKS_PER_SEC<<" seconds."<<std::endl;
    std::cout<<"\nCustom exit: press Enter to exit"<<std::endl;
    std::cin.get();
    return 0;
}
@fg109
Real simple and straight forward, I was trying to aim for something like how you did, but ended up doing it the noobish way.

@Dput
I like option2 and how you recorded the amount of seconds it took to build. Option2 is a lot faster then option1.

closed account (j3Rz8vqX)
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
#define END '\n';
//option_one...
//option_two...
void option_three()//Implemented by OP
{
    for(int top=1;top<=10;top++){
        BLOCK;
    }
    std::cout<<END;

    for(int height=1;height<=8;height++){//Modified to 8 to match..
        for(int c=1;c<=1;c++){
            BLOCK;
        }

        for(int right_edge=1;right_edge<=8;right_edge++){
            std::cout<<' ';
        }
        BLOCK;
        std::cout<<END;
    }
    for(int bottom=1;bottom<=10;bottom++){
        BLOCK;
    }
    std::cout<<END;
}
void option_four()//For additional competition
{
    char str[2][11] = {
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\0'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb','\0'}
    };
    std::cout<<str[0]<<END;
    for(int i=0;i<YMAX-2;i++)
        std::cout<<str[1]<<END;
    std::cout<<str[0]<<END;
}

Improved stub:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    clock_t t;

    //Array of pointers to functions with no arguments and no return type:
    void (*option[])() = {&option_one,&option_two,&option_three,&option_four};

    for(int i=0;i<4;i++)
    {
        t = clock();
        option[i]();
        t = clock()-t;
        std::cout<<"Option "<<i+1<<" took: "<<float(t)/CLOCKS_PER_SEC<<" seconds."<<END;
    }

    std::cout<<"\nCustom exit: press Enter to exit";
    std::cin.get();
    return 0;
}

Edit: Forgot to include newline definition.
Last edited on
closed account (j3Rz8vqX)
I wanted to test the speeds, so here are the results:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <ctime>
#include <cstdlib>

#define BLOCK std::cout<<(char)'\xdb';
#define END '\n';
const int YMAX=10,XMAX=10;//GLOBAL - small simple program!
void option_one()
{
    for(int y=0;y<YMAX;y++)
    {
        for(int x=0;x<XMAX;x++)
        {
            if(y==0||y==XMAX-1)
            {
                BLOCK;
            }
            else
            {
                if(x==0||x==XMAX-1)
                {
                    BLOCK;
                }
                else
                {
                    std::cout<<" ";
                }
            }
        }
        std::cout<<END;
    }
}
void option_two()
{
    char arry[YMAX][XMAX]=
    {
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb'},
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb'}
    };
    for(int y=0;y<YMAX;y++)
    {
        for(int x=0;x<XMAX;x++)
        {
            std::cout<<arry[y][x];
        }
        std::cout<<END;
    }
}
void option_three()//Implemented by OP
{
    for(int top=1;top<=10;top++){
        BLOCK;
    }
    std::cout<<END;

    for(int height=1;height<=8;height++){
        for(int c=1;c<=1;c++){
            BLOCK;
        }

        for(int right_edge=1;right_edge<=8;right_edge++){
            std::cout<<' ';
        }
        BLOCK;
        std::cout<<END;
    }
    for(int bottom=1;bottom<=10;bottom++){
        BLOCK;
    }
    std::cout<<END;
}
void option_four()//For additional competition
{
    char str[2][11] = {
        {'\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\0'},
        {'\xdb',' ',' ',' ',' ',' ',' ',' ',' ','\xdb','\0'}
    };
    std::cout<<str[0]<<END;
    for(int i=0;i<YMAX-2;i++)
        std::cout<<str[1]<<END;
    std::cout<<str[0]<<END;
}
void option_five()//naraku933's
{
	std::cout << std::string(XMAX, '*') << END;
	for(int i = 0; i < YMAX - 2; ++i)
		std::cout << '*' << std::string(XMAX - 2, ' ') << '*' << END;
	std::cout << std::string(XMAX, '*') << END;
}
int main()
{
    clock_t t;

    //Array of pointers to functions with no arguments and no return type:
    void (*option[])() = {&option_one,&option_two,&option_three,&option_four,&option_five};

    std::string choice="";
    std::cout<<"How many cycles should we test: ";
    getline(std::cin,choice);
    int rounds = atoi(choice.c_str());
    float *table[rounds];                   //Array of pointers, size = rounds
    for(int test=0;test<rounds;test++)
    {
        table[test] = new float[5];         //5 options: each array pointing to 5 new floats
        for(int i=0;i<5;i++)                //5 options
        {
            t = clock();
            option[i]();
            table[test][i] = t = clock()-t;
            std::cout<<"Option "<<i+1<<" took: "<<float(t)/CLOCKS_PER_SEC<<" seconds."<<END;
        }
    }

    std::cout<<"-----"<<END;
    float *sums = new float[5]{0,0,0,0,0};
    for(int i=0;i<rounds;i++)//Delete embedded pointers
    {
        for(int j=0;j<5;j++)
        {
            sums[j]+=table[i][j];
        }
        delete []table[i];//deleting the 5 heaps per index
    }

    for(int i=0;i<5;i++)
    {
        std::cout<<"Option "<<i+1<<" took: "<<(float(sums[i])/CLOCKS_PER_SEC)/rounds<<" seconds, on average, out of "<<rounds<<" tests."<<END;
    }
    delete []sums;

    std::cout<<"\nCustom exit: press Enter to exit";
    std::cin.get();
    return 0;
}
Option 1 took: 0.007045 seconds, on average, out of 1000 tests.
Option 2 took: 0.007111 seconds, on average, out of 1000 tests.
Option 3 took: 0.006936 seconds, on average, out of 1000 tests.
Option 4 took: 0.002425 seconds, on average, out of 1000 tests.
Option 5 took: 0.003394 seconds, on average, out of 1000 tests.

Custom exit: press Enter to exit

As of posting, do not see any memory leak if you see any let me know.

Also, using '\n' to reduce buffer flush compared to endl;

Also, I'm sure option two would have been a lot faster if I had a external table, instead of recreating it every cycle.
Thanks everyone for all the different techniques. Very useful.
return EXIT_SUCCESS;
More stuff that's hard to do in a console, yet easy to do with a graphic lib:

1
2
3
4
5
6
7
// assuming you have 'x', 'y', 'width', 'height'
// and a 'window' to draw to

sf::RectangleShape rect( sf::Vector2f(width,height) );
rect.setPosition(x,y);

window.draw(rect);
Topic archived. No new replies allowed.