tags:

views:

66

answers:

2

Very simple problem, but can't seem to solve it. I'm probably just not thinking right. So I'm using C and I need to be able to sort some images I download into two rows. Like this: http://i.imgur.com/lWkEO.png So my program will, say, download 30 images and it has to sort it into these two rows just like you see in the picture. So I know I have to loop through them but can't seem to get it. Can someone please help me out? Like I said I know it's simple but just can't seem to get it to work! Thanks so much! :D

+2  A: 

Add 2 in your iteration, instead of one, then iterate it again starting from 1 instead of 0.

EDIT:

(sigh)

void *images;
int NUM_IMAGES = 10;
int NUM_ROWS = 2;

int inneri;
int outeri;

for (outeri = 0; outeri < NUM_ROWS; outeri++)
  for (inneri = outeri; inneri < NUM_IMAGES; inneri += NUM_ROWS)
    do_something_with(images[inneri]);

And then if you need an even bigger loop around this for some reason, you could use a variable called thirdi.

Ignacio Vazquez-Abrams
Wait I dont think I'm understanding what you mean? Could you explain a bit more?
Alexander
+2  A: 

OK, your loop could either be how Ignacio has done it, or you could do something like this:

int i, imageCount, row, col, imagesPerCol;

//...
imagesPerCol = 2;

for(i = 0; i < imageCount; i++) {
  row = i % imagesPerCol;
  col = i / imagesPerCol;

  //... do image placement here using row and col
}
Vincent McNabb
Thanks so much!!
Alexander