Qt re-arranging double array elements with for cycle



  • 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]



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



  • 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]



  • [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 :)



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.