Unfamiliar: Stepping through array

First the code im using this far

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
int process()
{
    
}

int output()
{
    
}

struct circle_t {
         int x,y,r;         /* x, y, radius */
         int col;           /* colour */
         int xs,ys;         /* x speed, y speed */
         int dx,dy;         /* drawn x, drawn y */
     };



void circle_draw (struct circle_t *circle, BITMAP *bmp);
void circle_erase (struct circle_t *circle, BITMAP *bmp);
void circle_update (struct circle_t *circle);
struct circle_t *circle_init (int new_x, int new_y, int new_radius, int new_col, int new_xs, int new_ys);
void circle_destroy (struct circle_t *circle);






int main(int argc, char *argv[])
 {
             allegro_init();
             install_keyboard();
             set_gfx_mode (GFX_AUTODETECT, 800, 600, 0, 0);
             
             
             
             
             
             
             allegro_exit();
             system("pause");
             return 0;
          
}


Instructions:

We'll have an array of pointers to `circle_t' structs, and our
main `init' function will initialise them all by calling
`circle_init' with various parameters.

Having initialised them, we'll draw them all, before we return
from the `init' function.

In `process', we will calculate the new positions of all the
circles. To do this, we need to set up a for loop stepping
through the array of circles, and call `circle_update' for each.


The bold is what im having trouble with. In the function process, i need a loop to step through the array of circles. I dont know how to do this, nor have i set up a loop to go through this before. Any help?
I would just use a for loop for something like this.

1
2
3
4
for(i = 0; i < array_max; ++i) {
    // access something using i, e.g.
    array[i] = 20;
}
So just one last question, how would i initiate circle_update every time for that? I wanna say another loop, but that doesnt seem right. Other than that i have no idea, any suggestions there?
When you're working with arrays, and want to do something for all members of said array, then a loop will always be what you want to use.

With your array of circle pointers you would have

1
2
3
4
for (int i = 0; i < array_max; i++)
{
	circle_update(*circles[i])
}


where "circles" is the name of your array.

Is this what you're after?
Last edited on
Mainly a better understanding so i dont have to go after anything lol But yes and thank you! Honestly, i beleive im over thinking it. The example shown is what came to mind first but i thought it would of been a bit more complicated.
Last edited on
Now im stuck again, im really confused on how to put this all together. Now the array of pointers i have to declare is supposed to be circles and needs to be initialized, drawn, updated, and destroyed using Allegro. My question revolves now around the functions that came with the assignment.

1
2
3
4
5
void circle_draw (struct circle_t *circle, BITMAP *bmp);
void circle_erase (struct circle_t *circle, BITMAP *bmp);
void circle_update (struct circle_t *circle);
struct circle_t *circle_init (int new_x, int new_y, int new_radius, int new_col, int new_xs, int new_ys);
void circle_destroy (struct circle_t *circle);


specifically

struct circle_t *circle_init (int new_x, int new_y, int new_radius, int new_col, int new_xs, int new_ys);

In my own theory i beleive i should write it something like this but i dont think this is appropriate

struct circle_t *circle_init[5](parameters and so on)

The question is, is this the appropriate way to do something like this? If not how should i go about doing this?

No, you can't, you have to use a loop, and premake your array.

1
2
3
4
circle* circles[5];
for(int i = 0; i < something; ++i) {
    circles[i] = makeCircle(/*stuff*/);
}
Topic archived. No new replies allowed.