Help with 2d array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moody
    New Member
    • Sep 2007
    • 4

    Help with 2d array

    Language is C++

    Hey everyone I am having trouble doing the following:
    Test if (x,y) is already on the path. This can be done by comparing the kth element of p with the columns before it.

    p[0][k] = x (x values, which are the columns.)
    p[1][k] = y (y values, which are the rows.)


    for ( int i = k; i<= k ; i--){
    if (p[0][i] && p[1][i] == p[0][k] && p[1][k]){
    k = k-1;
    return;
    }

    else{
    // do something;
    }


    Not sure if I have the right idea here...Any help would be much appreciated. Thank you.
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    First of all you have an infinite loop, 'i' will always be equal too or less than 'k'.
    you can fix that by i>= 0;

    Now i don't fully understand the question.
    Are you just trying to see if the value already exists in a 2 dimensional array and looking at your logic and the question it appears the array only has 2 rows? if so you need to loop through and check the value of X if true check the value of Y.

    you could also do that by using two loops
    One loop will handle the progression through the rows
    the other loop will handle the progression through the columns.

    Comment

    • moody
      New Member
      • Sep 2007
      • 4

      #3
      Yes, I need to check to see if (x,y) already exist in the array for example.


      Say I send in the values
      x= 2;
      and y =2;

      and the array consists of:

      (2,3),(2,4),(2, 2),(3,5),(3,6). ..

      Since 2,2 is already in the array return

      If it does not exist already in the array do the else statement...

      Hope that cleared things up.

      Thanks for the reply.

      Comment

      • moody
        New Member
        • Sep 2007
        • 4

        #4
        for ( int i = 0; i< k ; i++){
        if (((p[0][i] == x) && (p[1][i] == y)) == ((p[0][k] == x) && (p[1][k] == y))) {


        k = k-1;
        return;
        }
        else{

        I tried this and for some reason it still does not work

        Comment

        • Studlyami
          Recognized Expert Contributor
          • Sep 2007
          • 464

          #5
          okay, i edited my reply up above before i saw your new post, so i just paste part of it.

          If there is only going to be 2 rows in the array all you need to loop through and check the value of X if true check the value [x][y] if y is true the item already exists.

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            your for loop should loop through all columns. You can do this by the following

            for(int i =0; i<sizeof(array )/sizeof(array[0][0]); i++)

            the sizeof(array) determines the total size of the array (memory wise)
            then you divide that number by the size of one element. This will give you the total number of elements.

            Also i'm not quite sure why you have so many comparisons you only need 2

            Does NewX = old X
            if true
            Does NewY = old Y
            if true item exists
            if false
            continue loop.

            Comment

            • moody
              New Member
              • Sep 2007
              • 4

              #7
              Still not getting this to work...If there is a double I need it removed from the path...and have something over write it...


              p[0][k],p[1][k] = (x,y)

              those are the same thing and I have to see if (x,y) is at position p[0][k],p[1][k]

              k = 2 lets say for the purpose of this example...

              I appreciate everyones input and time for me to get some rest been working on this problem forever...New to C++ and really want to be good...

              Thanks


              EDIT:::::Figure d it out, thanks alot again

              if ((p[0][i],p[1][i]) == (p[0][k],p[1][k])) {

              Comment

              Working...