Thread: Brain Fart on 2D arrays...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    111

    Brain Fart on 2D arrays...

    I'm having a little brain fart on 2D arrays.

    If I am going to declare a 2D array as

    Code:
    #define gridSize 20
    
    int array[gridSize][gridSize];
    And if I am going to access, (5,7), in (X,Y) cords. Then the correct format would be

    Code:
    array[7,5]
    If that is the case, what would be a good suggestion, in not to confuse the X and Y cords? Would the following work?

    Code:
    #define gridSize 20
    
    int x = gridSize;
    int y = gridSize;
    
    int array[x][y];
    
    //now if I want (5,7), in (X,Y) cords
    array[5][7]
    Thank You in advance

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Remembering that x comes before y works good for me.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Why not name your variable with "xy" in the name? Then every time you use it, you can't miss it.

    Example:
    Code:
    int xyArray[gridSize][gridSize];
    xyArray[5][7] = 3;

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just remember this: if you want to go left to right you increment the second index. If you want to go top to bottom, you increment the first index.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    13
    They don't actually represent real 2D structures, that's just an abstraction to help new programmers try to understand them. It really doesn't matter which you use for your x and why as long as you consistently use the same ones.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Cool, thanks for the clarification, the up and down and left and right are great

    I didn't have a C compiler installed on my laptop (and dam school doesn't allow downloads of X amount or greater), and I was working it out on pen and paper.

    Quote Originally Posted by XenoReseller View Post
    They don't actually represent real 2D structures, that's just an abstraction to help new programmers try to understand them. It really doesn't matter which you use for your x and why as long as you consistently use the same ones.
    Actually for this it does sorta matter. This is actually part of a larger program.

    I'm doing an AI for a Tron type game (light cycles), and as a twist there are two moving walls in the middle of the "grid."

    We are given the tail positions in (x,y) cords, length, the NW corner (x,y cords), the length of the side the walls travels on, and speed. At the start of the game then it is our responsibility to keep track of the moving wall from that point on.

    Up until now, I just kept the "square" path that the wall travels on a "no go zone," but I'm looking to change that, it's just a rather simple struct persay to feed details into my masking grid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional IF Brain Fart
    By paulogrady in forum C Programming
    Replies: 4
    Last Post: 04-12-2009, 11:07 AM
  2. brain fart
    By Gardul in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2005, 08:49 AM
  3. Brain Fart Problem
    By golfinguy4 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2002, 11:56 PM
  4. Brain fart
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-30-2001, 12:40 PM