converting ints to arrarys

This is a discussion on converting ints to arrarys within the C++ Programming forums, part of the General Programming Boards category; hey all, how do one convert an integer that a user has entered into a location is an existing 2-d ...

  1. #1
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31

    Post converting ints to arrarys

    hey all,

    how do one convert an integer that a user has entered into a location is an existing 2-d array?



    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>


    main(){

    int y;
    int grid[4][4]={{1,5,9,13},{2,6,10,14},{3,7,11,15},{4,8,12,16}};

    cin>>y;
    int u;
    int U;
    y=grid[u][U];
    if (y==grid[3][3]){
    cout<<"good";
    }
    else{
    cout<<"bad";
    }
    }
    When I run this, I get a windows error, even tho I get no compiler errors.

    thanks

    777

  2. #2
    Unregistered
    Guest
    y is an int
    grid[u][U] is an int

    so if you want to see if y = grid[3][3] (which is 16 in your grid)
    drop the following lines in your code and you should be golden

    int u;
    int U;
    y=grid[u][U];

    if you want to see if the value of y exists at any spot in grid, then you need a nested loop to generate each element in the grid to compare with y.

  3. #3
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31

    unknown values in arrays

    OK, got that.
    But, it gets a little more complicated.
    The array represents basically a game board. The comptuer "moves" 1 up, or 1 down, or 1 diagonally, etc. Obviously, the movement is based on its current position, as denoted by the coordinates (i.e. array[x][y]).
    The problem arises when the computer makes an illegal move, such a 2 spaces across. To prevent that, I use an if/then statement, referring to the intended coordinates (array [x][y]), and subtracting from them the coordinated of the present position, which, of course, is itself based on previous movements, etc., etc., starting all the way back with array[0][0]--the computer starts at the top left corner.

    The problem is that the coordinates, or [x][y], change, depending on the position on the "board". So I would need to refer to array[x][y] as a variable, as opposed to array[3][3], which is always 16.

    I hope that's kinda clear (!)

    Thanks

    777

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Element 16 is cordinate 3,3

    we can convert that by dividing for the row and modula (%) for the column:
    (16-1)/4 = 3
    (16-1)%4 = 3

    or (Element - 1)/n = rows, where n equals number if columns.
    (Element - 1)%n = columns, where n equal number of columns.

  5. #5
    Unregistered
    Guest
    Here's how I did it in a game I did once.

    General case: a given square is surrounded by 8 other squares, each of which is a valid move. Given board 3 row and 3 col and numbering sequence 1 - 9, this would mean square 5

    123
    456
    789

    and converting square number to x, y coordinates:
    # x y
    1 0 0
    2 0 1
    3 0 2
    4 1 0
    5 1 1
    6 1 2
    7 2 0
    8 2 1
    9 2 2

    In my game I used a struct to hold coordinates and a value at the coordinate.

    To write a function to check if it is a valid move I used boolean return type and passed two structs-- current struct and new struct;
    //if x coordinate the same then y can be + or - 1 from current
    //this checks for squares 4 and 6
    if (current.x == new.x && (current.y == new.y + 1 || current.x == new.y - 1))
    {return true;}
    //to check for squares 2 and 8
    else if(current.y == new.y && (current.x == new.x + 1 || current.x == new.x - 1))
    {return true;}
    //to check for square 1 or 3
    else if(current.x == new.x + 1 && (current.y == new.y + 1 || current.y == new.y - 1))
    {return true}
    //to check for square 7 or 9
    else if(current.x == new.x - 1 && (current .y == new.y + 1 || current.y == new.y - 1)
    {return true}
    else
    return false;
    end function

    This will allow you to check for valid moves from any position on the board except if the current position is on the border of the board. If the current position is on the border you need to set up a system to do border checks, that is, if current coordinate is located in any outside row or column then there are only 5 valid moves rather than 8 AND if current coordinate is in an outside row and an outside column (that is, it is one of the four corners) then there are only three valid moves to check).

    All in all this got kind of messy, but it worked. There are undoubtedly more sophisticated ways to do this, but I don't know them. You might try checking at a bulletin board specializing in games at this and other sites.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >When I run this, I get a windows error, even tho I get no compiler errors.

    y=grid[u][U];

    u and U haven't been initialized at this point, so there is no guarantee they even point within the array. Either do this:
    int u = 2;
    int U = 2;

    or assign values after they are declared like this:
    u = 2;
    U = 2;

  7. #7
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Unregistered, thanks for the info.

    777

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 04:52 PM
  2. atoi not converting chars to ints?
    By C++Child in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2004, 04:59 PM
  3. Replies: 2
    Last Post: 08-05-2002, 03:19 PM
  4. Converting strings to ints
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2002, 03:48 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21