Shifting a 2D array to the right

Discussion in 'Programmer's Corner' started by Dritech, Mar 26, 2016.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,
    I am trying to shift a 2D array (with variable width) to the right.
    My first attempt was the code below. This did not work since I was filling all elements with the values of the first column.

    Code (C):
    1. for(i = 0; i < array_width-1; i++)
    2. {
    3.      for(j = 0; j < 5; j++)      
    4.      {
    5.            ADC_process[j][i+1] = ADC_process[j][i];  // shift all columns to the right by one
    6.      }
    7. }

    Is the code below the correct way to implement this?
    The first column will not be changed, but that is ok for my application.

    Code (C):
    1. for(i = array_width-1; i == 0; i--)
    2. {
    3.     for(j = 0; j < 5; j++)
    4.     {
    5.          ADC_process[j][i] = ADC_process[j][i-1];
    6.     }
    7. }
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,755
    4,799
    Bench check (i.e., walk through it by hand with a pencil and paper) your original code and you will see why it behaves as it does. To make the bench check easy, start with an array_width of three or four.

    Then bench check your final code and you will see why it starts to behaves like you want, with two problems.

    The first problem is that your outer loop only stays in the loop if 'i' is exactly equal to zero. So unless the array_width is equal to 1, the outer loop will never execute at all.

    Try changing your test to 0 < i.

    Second (and it's hard to tell if your approach will have this problem since your test for the outer loop is incorrect), you (pretty much) never want to try to access an array element outside of the array bounds. C will be more than happy to let you do this -- but there be demons in those waters. So you need to make sure that your index values are never less than zero (or greater than or equal to the array length).
     
    Dritech likes this.
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    363
    Depending on how your array is define dand what you meant by "shifting it right".
     
  4. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    I changed the outer loop as you suggested and it worked, thanks.
    Sometimes these for-loops get me confused.
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,755
    4,799
    Do loops in general confuse you, or is it the for() loop in particular that gives you trouble?
     
  6. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Loops in general, but mostly nested for-loops.
     
  7. WBahn

    Moderator

    Mar 31, 2012
    17,755
    4,799
    If while() loops become non-confusing, it can help to remember that a for() loop is nothing more than syntactic sugar for a while loop in which the first statement is placed before the while() loop, the second statement is the while() loop test expression, and the last statement is placed at the end of the while() loop's body.

    For nested loops, just remember that the inner loops is just a loop. Nothing special about it. Like any loop, it has access to the value of those variables outside the loop that are in scope. Some of those just happen to be values that have been set by the present iteration of the outer loop.
     
    Dritech likes this.
Loading...