converting int to arrays, more info

This is a discussion on converting int to arrays, more info 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

    converting int to arrays, more info

    hey all,

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

    For example:

    #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.


    The actual 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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > int u;
    Contains garbage

    > int U;
    Contains garbage

    > y=grid[u][U];
    Overwrites the value of y you've just input with some wild array reference, caused by indexing it with two garbage values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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