Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Qt re-arranging double array elements with for cycle

    C++ Gurus
    4
    6
    2726
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      Nagamo last edited by

      Hello!

      I've been playing around with arrays recently and I did a short check on weather I could re-arrange double arrays.

      What I'm trying to do in a simply cycle is:

      @
      array[10]=array[9];
      array[9]=array[8];
      array[8]=array[7];
      @

      and so on.

      My attempt to do so :
      @
      for (int i=10; i>=1; i--)
      {
      array[i]=array[i--];
      }
      @

      The program runs, though I get zero result (a timer re-arranges and writes out the values in a string).

      What am I doing wrong?

      Cheers,
      Gabor Nagy

      [EDIT: code formatting, please wrap ALL snippets in @-tags, Volker]
      [EDIT: moved to C++ Gurus forum, as it's a c++ topic, Gerolf]

      1 Reply Last reply Reply Quote 0
      • M
        mkoskim last edited by

        Well, without any deeper analysis, you are at least decreasing the index twice in your loop. Something like this could work:

        @for(int i=10; i > 0;i--) array[i] = array[i-1];@

        Notice, that in this case your array size is 11.

        http://mkoskim.wordpress.com
        http://mkoskim.drivehq.com
        http://mkoskim.deviantart.com

        1 Reply Last reply Reply Quote 0
        • L
          lgeyer last edited by

          Well, there are two things that catch my eyes immediately:

          • array[ 10 ] ranges from array[ 0 ] to array[ 9 ]. So array[ i ] is out of bounds for the first iteration.
          • i-- is not the same as i - 1. i-- decrements i, so your are iterating 10, 8, 6, 4, 2.

          [EDIT: fixed square brackets for superscript, Volker]

          1 Reply Last reply Reply Quote 0
          • N
            Nagamo last edited by

            [quote author="mkoskim" date="1320762018"]Well, without any deeper analysis, you are at least decreasing the index twice in your loop. Something like this could work:

            @for(int i=10; i > 0;i--) array[i] = array[i-1];@

            Notice, that in this case your array size is 11.
            [/quote]

            Doh! Looking back at it, it's quite a dumb mistake.
            Thank you very much for pointing it out!

            The program works great now :)

            1 Reply Last reply Reply Quote 0
            • N
              nokiaadict last edited by

              by using your examples you could use
              @
              int arrayLength = sizeof(array) / sizeof(int); //if you array is comprised of int values
              for(int i=arrayLength; i > 0;i--) array[i] = array[i-1];
              @

              Always in contact...   <a href="http://www.jocurile.us/cat-jocuri-cu-masini.htm" class="gsc-tabsAreaInvisible">jocuri cu masini</a>

              1 Reply Last reply Reply Quote 0
              • M
                mkoskim last edited by

                [quote author="nokiaadict" date="1320878701"]by using your examples you could use
                @
                int arrayLength = sizeof(array) / sizeof(int); //if you array is comprised of int values
                for(int i=arrayLength; i > 0;i--) array[i] = array[i-1];
                @
                [/quote]

                Be very careful when making loop to go backwars with arrays. In this example, the first access is probably going to overflow the array. There are two basic ways to go through the array:

                Forward:

                [code]for(int i=0; i < length; i++) { ...}[/code]

                Backward:

                [code]if(length > 0) for(int i=length; i--;) { ... }[/code]

                Doing other than those needs always deeper analysis of the loop. If the direction does not matter, always go forward, it makes it much easier to understand to any other programmer.

                EDIT: In the example, the trick is (very common) "scrolling" (moving) the array, in which case you don't go through the whole array, but array length -1. There are common solutions for both forward (as in this case) and backwards "scrolling".

                http://mkoskim.wordpress.com
                http://mkoskim.drivehq.com
                http://mkoskim.deviantart.com

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post