Thread: array to list: (x,y) to int and int to (x,y)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    array to list: (x,y) to int and int to (x,y)

    hi,

    need help on this:

    ned to convert from an array to a list and back.

    I wrote this function:

    Code:
    
    /*
    * inputs:
    * coordX: column;
    * coordY: line;
    * nc: number of colums
    *
    * returns: int
    *
    */
     
    int XYtoInt(int coordX, int coordY, int nc){
     
         return (coordY * nc + coordX -1);
    }
    it works fine but the other way arround is giving me sleeping problems.

    I was thinking something about this:

    Code:
    /*
    * inputs: integer
    * returns: X coordinate
    */
    int inttoX(int n, int nc){
        int coordX;
    
        coordX = ...............;
    
        return coordX;
    }
    
    /*
    * inputs: integer
    * returns: Y coordinate
    */
    int inttoY(int n){
        int coordX, coordY;
    
        coordY = ...............................;
        return coordY;
    }
    I don't need the code, just na idea to continue... i'm a little blocked.

    tanks.
    regards

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK well you reduced the xy-coordinate with the formula:

    reduced = coordY * nc + coordX - 1

    The formula needs to be symbolically manipulated to give you the xy-coordinate parts.

    coordY = (reduced - coordX + 1) / nc
    coordX = reduced - (coordY * nc) + 1

    There seems to be no other way. One coordinate requires knowing the other.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Assuming x ranges from 0 to nc-1 and the subtraction of one in the XYtoInt function is a mistake, you could simply:
    Code:
    y = n / nc;
    x = n % nc;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic List Char Array & Double Linked List
    By Roadrunner2015 in forum C Programming
    Replies: 18
    Last Post: 10-20-2013, 01:31 PM
  2. Replies: 1
    Last Post: 02-10-2012, 05:42 AM
  3. Array-based ADT list
    By campermama in forum C++ Programming
    Replies: 0
    Last Post: 06-14-2004, 01:32 PM
  4. Circular array list
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2004, 02:28 PM
  5. List and array
    By planet_abhi in forum C Programming
    Replies: 2
    Last Post: 02-01-2003, 03:36 PM