creating a picture with an array

So I'm trying to make a picture out of squares and I need 238 squares and I'm wondering how to put them all on the screen using the same object, Is there a better way to do this? I've been trying this for a few days now and don't know what to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void InitBottle(Bottle bottles[])
{
	for(int j = 0; j <= 11;j++)
	{
	        int bx = 165;
	        int by = 50;

		for(int i = 0; i <= 5;i++)
		{
			bottles[i].ID = BOTTLE;
			bottles[i].x = bx;
			bottles[i].y = by;
			bx += 15;
		}
		by += 15;
	}
}

I know the problem is that the by keeps resetting, so I guess I'm really wondering how to set by and then have it not reset, and if I put the declarations outside the for loop, then both bx and by just keep going up don't even show up on the window. I know this doesn't draw all 238 squares but I'm working up to it starting here. So if anyone's got a better idea or how to fix this I'd appreciate the help. Also if it's not to much to ask please don't just give me the code, just a starting point, I'm trying to learn.
closed account (Dy7SLyTq)
no the problem is that you do the same operation on the first five members 11 times
i think you got this really wrong.
this function doesn't print anything on the screen.

for a windows-only approach try these functions:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687410(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687407(v=vs.85).aspx

to get the handle for the console, use this line:
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

here's a sample program that uses these functions, you probably want to skip till the function BounceProc(), it uses these functions and shows how to use them:
http://msdn.microsoft.com/en-us/library/esszf9hw.aspx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void InitBottle(Bottle bottles[])
{
        int by = 50; // **** moved here

	for(int j = 0; j <= 11;j++)
	{
	        int bx = 165;
	        // int by = 50;

		for(int i = 0; i <= 5;i++)
		{
			bottles[i].ID = BOTTLE;
			bottles[i].x = bx;
			bottles[i].y = by;
			bx += 15;
		}
		by += 15;
	}
}
DTSCode: I'm trying to, I need 238 squares in total, Bottle is a square that what I hope to accomplish with this bit of code is to create 238 squares and have Allegro paint them to the screen in the shape of a bottle

Rechard3: I'm not talking about the console at all, I'm using Allegro to drive the graphics. I guess I should have stated that. sorry.

JLBorges: I tried that, but then it still only paints one line just really far down the screen, the idea here is to paint 11 lines of 6 squares.
*bump*
closed account (N36fSL3A)
Did you try moving both out the for loop?
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
#include<iostream>

struct bottle
{
    int x = 0 ;
    int y = 0 ;
    // ....
};

std::ostream& operator<< ( std::ostream& stm, const bottle& b )
{ return stm << '(' << b.x << ',' << b.y << ')' ; }

// adjust the values of these as required
constexpr int NROWS = 8 ;
constexpr int NCOLS = 6 ;
constexpr int start_x = 25 ;
constexpr int start_y = 15 ;
constexpr int delta_x = 8 ;
constexpr int delta_y = 11 ;

using bottle_array_type = bottle[NROWS][NCOLS] ;
// typedef bottle bottle_array_type[NROWS][NCOLS] ;

void initialize_bottle_array( bottle_array_type& bottle_array )
{
    for( int row = 0 ; row < NROWS ; ++row )
        for( int col = 0 ; col < NCOLS ; ++col )
        {
            bottle_array[row][col].x = start_x + col * delta_x ;
            bottle_array[row][col].y = start_y + row * delta_y ;
        }
}

int main()
{
    bottle_array_type bottle_array ;
    initialize_bottle_array(bottle_array) ;
    for( const auto& row : bottle_array )
    {
        for( const bottle& b : row ) std::cout << b << ' ' ;
        std::cout << '\n' ;
    }
}

http://ideone.com/x6JlpN
FredBill30: no offense, but, did you even read my original post? I clearly stated in it that I had tried moving both of them out of the for loop, and I also stated that when I did that, the i and j kept going up and never rendered outside the widow.

JLBorges: Some of that code is above my head, so I'm going to look up constexpr, operator, and const, and auto, and I'll get back to you on this

You might find yourself in more familiar waters with this rewrite:

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

struct bottle
{
    int x ;
    int y ;
    // ....
};

void print( const bottle& b ) { std::cout << '(' << b.x << ',' << b.y << ") " ; }

// adjust the values of these as required
const int NROWS = 8 ;
const int NCOLS = 6 ;
const int start_x = 25 ;
const int start_y = 15 ;
const int delta_x = 8 ;
const int delta_y = 11 ;

typedef bottle bottle_array_type[NROWS][NCOLS] ;

void initialize_bottle_array( bottle_array_type& bottle_array )
{
    for( int row = 0 ; row < NROWS ; ++row )
        for( int col = 0 ; col < NCOLS ; ++col )
        {
            bottle_array[row][col].x = start_x + col * delta_x ;
            bottle_array[row][col].y = start_y + row * delta_y ;
        }
}

int main()
{
    bottle_array_type bottle_array = { {0,0} } ;
    initialize_bottle_array(bottle_array) ;

    for( int row = 0 ; row < NROWS ; ++row )
    {
        for( int col = 0 ; col < NCOLS ; ++col ) print( bottle_array[row][col] ) ;
        std::cout << '\n' ;
    }
}
closed account (N36fSL3A)
Whoops yea I did. But look at the time. That day I didn't sleep since 2 am and I was tired out of my mind.
Topic archived. No new replies allowed.