Traversing a 2D array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wallacej

    Traversing a 2D array

    Hi

    I have a 2D array : imageTileArray and I would like to traverse it i.e.
    visit every entry in the array sequentially. I've had a go at doing it
    myself as follows but i get address violations:


    for(i=0; i < sizeof(imageTil eArray[i]); i++) {
    for(j=0; j < sizeof(imageTil eArray[i][j]); j++) {
    //do something
    }
    }

    Is this the correct way to do so or is there better/correct way.

    thank You

    wallace

  • Kai-Uwe Bux

    #2
    Re: Traversing a 2D array

    wallacej wrote:
    [color=blue]
    > Hi
    >
    > I have a 2D array : imageTileArray and I would like to traverse it i.e.
    > visit every entry in the array sequentially. I've had a go at doing it
    > myself as follows but i get address violations:
    >
    >
    > for(i=0; i < sizeof(imageTil eArray[i]); i++) {
    > for(j=0; j < sizeof(imageTil eArray[i][j]); j++) {
    > //do something
    > }
    > }
    >
    > Is this the correct way to do so or is there better/correct way.[/color]

    No, it is not correct.

    First, from your post, it is not clear whether the arrays are statically or
    dynamically allocated. The biggest problem with the latter is that they
    simply do not keep track of their length. The former do, but to get at that
    information in a generic fashion one needs templates.

    As a solution for your problem, I would suggest to use std::vector instead.

    typedef std::vector< imageTile > imageTileRow;
    typedef std::vector< imageTyleRow > imageTileMatrix ;

    for ( imageTyleMatrix ::const_iterato r row_iter = matrix.begin();
    row_iter != matrix.end(); ++row_iter ) {
    for ( imageTypeRow::c onst_iterator col_iter = row_iter->begin();
    col_iter != row_iter->end(); ++ col_iter ) {
    // do something to *col_iter
    }
    }

    If you use static allocation, you may want to replace std::vector by
    boost::array.


    Best

    Kai-Uwe Bux

    Comment

    • Stephan Brönnimann

      #3
      Re: Traversing a 2D array

      wallacej wrote:[color=blue]
      > Hi
      >
      > I have a 2D array : imageTileArray and I would like to traverse it i.e.
      > visit every entry in the array sequentially. I've had a go at doing it
      > myself as follows but i get address violations:
      >
      >
      > for(i=0; i < sizeof(imageTil eArray[i]); i++) {
      > for(j=0; j < sizeof(imageTil eArray[i][j]); j++) {
      > //do something
      > }
      > }
      >
      > Is this the correct way to do so or is there better/correct way.
      >
      > thank You
      >
      > wallace[/color]

      Add the following lines to your code snipet and make it compile and
      run, you'll learn a lot:

      int a[5][7];
      std::cout << sizeof(int) << "\n"; // 4
      std::cout << sizeof(a[0]) << "\n"; // you'd like 5, I got 28!
      std::cout << sizeof(a[0][0]) << "\n"; // you'd like 7, I got 4!

      Regards, Stephan

      Comment

      • Gernot Frisch

        #4
        Re: Traversing a 2D array

        [color=blue]
        > I have a 2D array : imageTileArray and I would like to traverse it
        > i.e.
        > visit every entry in the array sequentially. I've had a go at doing
        > it
        > myself as follows but i get address violations:[/color]

        if it's an array on the stack:
        [color=blue]
        > for(i=0; i < sizeof(imageTil eArray[i])/sizeof(imageTil eArray[0]);
        > i++) {
        > for(j=0; j <
        > sizeof(imageTil eArray[i][j])/sizeof(imageTil eArray[0][0]); j++) {
        > //do something
        > }
        > }[/color]

        if it's a heap array (using new/malloc) - uh-oh!

        This is C++, so _do_ use std::vector as told by Kai-Uwe.


        Comment

        • wallacej

          #5
          Re: Traversing a 2D array

          thanks for all the help guys, the arrays are dynamic so i'll give
          vectors a try.

          wallace

          Comment

          • roberts.noah@gmail.com

            #6
            Re: Traversing a 2D array


            wallacej wrote:[color=blue]
            > thanks for all the help guys, the arrays are dynamic so i'll give
            > vectors a try.
            >
            > wallace[/color]

            One disadvantage of using vector<vector<> > is that your items won't be
            contiguous. Your rows will be but not the rest. You can't do
            something like this:

            for (int i = 0; i < ROWSIZE * COLSIZE; ++i)
            visit(array + i);

            This can, depending on your needs, better fit the algorithms you want
            to perform.

            You can still access it like a two dim array of sorts:

            visit(array + a * ROWSIZE + b);

            You can of course hide this type of access in an operator, if you
            encapsulate your storage in a class, like so:

            ....
            T & operator()(int a, int b) { return array + a * ROWSIZE + b; }

            visit(darray(a, b)); // note that this visit accepts a different type
            than the other two.

            or

            T & elementAt(int a, int b)....

            You can do operator[] to return a pointer to your row but that is a
            hack best avoided if not absolutely necessary. Even if such syntax is
            necessary returning some sort of adapter and/or proxy instead of a
            pointer would be much better but more complex to develop.

            Comment

            • Howard

              #7
              Re: Traversing a 2D array


              "wallacej" <jamie_r_wallac e@yahoo.co.uk> wrote in message
              news:1138806382 .217392.29230@g 14g2000cwa.goog legroups.com...[color=blue]
              > thanks for all the help guys, the arrays are dynamic so i'll give
              > vectors a try.
              >
              > wallace
              >[/color]

              If the arrays are allocated dynamically, then you should already know the
              number of elements (i.e., length and width). You could just keep those two
              values around and use them. (Or, if each sub-array is a different size, you
              can keep their individual lengths in a separate array.)

              -Howard


              Comment

              • wallacej

                #8
                Re: Traversing a 2D array

                thanks for the help there guys

                i took howards advice and it has worked. as usual a simple solution
                was staring me in the face and i only had to change a minor part of my
                code.

                Cheers

                Comment

                Working...