Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Qt re-arranging double array elements with for cycle
Qt 6.11 is out! See what's new in the release blog

Qt re-arranging double array elements with for cycle

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 4 Posters 5.0k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          [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
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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];
            @

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              [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".

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved