Thread: 3 x 3 loop

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    3 x 3 loop

    Code:
     int img4[IMG_SIZE][IMG_SIZE] = 
    { 16, 16, 220,  16,   1,  16,
      16,  87,  16,  16, 215,  16,
       16,  16,  16,   5,  16,  16,
        128, 128,  56, 128, 128, 128, 
       128, 198, 128, 128, 128,  32,
      78, 128, 128,  21, 128, 128};
    i have that 2d array, and i want to access it in this sequence using 2 for loop. any idea?
    thx for the helpz

    [0][0] [0][1] [0][2]

    [1][0] [1][1] [1][2]

    [2][0] [2][1] [2][2]
    -----------------------
    [3][0] [3][1] [3][2]

    [4][0] [4][1] [4][2]

    [5][0] [5][1] [5][2]
    -----------------------
    [0][3] [0][4] [0][5]

    [1][3] [1][4] [1][5]

    [2][3] [2][4] [2][5]
    -----------------------
    [3][3] [3][4] [3][5]

    [4][3] [4][4] [4][5]

    [5][3] [5][4] [5][5]

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    The biggest concern here seems to be writing a single loop to produce the sequence: 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5 (i.e. how to "leap" from 5 to 0). Seems to me that you'll want to use the modulo (&#37 operator. Here's a hint:

    0 % 6 = 0
    1 % 6 = 1
    2 % 6 = 2
    3 % 6 = 3
    4 % 6 = 4
    5 % 6 = 5
    6 % 6 = 0

    With this hint, try and write a single loop that produces that sequence 2 times - only ONE loop.

    EDIT: Oops, I just realized I might not have been 100% clear when I said "that sequence 2 times". I meant the following sequence: 0, 1, 2, 3, 4, 5.
    Last edited by Mr_Miguel; 09-28-2008 at 06:08 AM.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM