4 Replies - 2905 Views - Last Post: 08 November 2012 - 05:31 AM Rate Topic: -----

#1 lw2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 03-November 12

Wavefront loop

Posted 03 November 2012 - 05:45 AM

As part of my research I need to implement wavefront approach to Successive Over-Relaxation. I am struggling to design a loop that would traverse the 2d array in a wavefront manner.

Just to clarify - wavefront accesses the top left element in the array, then the two neighbors, then the third diagonal line etc.

So if we had a 4x4 array, the elements would be accesses in the following order
(1, 1) (1, 2) (2, 1) (1, 3), (2, 2), (3, 1) (1, 4), (2, 3), (3, 2), (4, 1) (4, 2), (3, 3), (4, 3), (2, 4), (3, 4), (4, 4)

Can any please help in designing a loop that would do traversal in that order? Pseudo code would be useful

Is This A Good Question/Topic? 0
  • +

Replies To: Wavefront loop

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Wavefront loop

Posted 05 November 2012 - 10:08 PM

This code is a bit C-ish.

for(diagonal = 1; diagonal < 5; diagonal++)
{
  col = diagonal;  
  for(row = 1; row <= diagonal; row++, col--)
    print row, col;
}


Was This Post Helpful? 1
  • +
  • -

#3 lw2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 03-November 12

Re: Wavefront loop

Posted 07 November 2012 - 10:29 AM

View Post#define, on 05 November 2012 - 10:08 PM, said:

This code is a bit C-ish.

for(diagonal = 1; diagonal < 5; diagonal++)
{
  col = diagonal;  
  for(row = 1; row <= diagonal; row++, col--)
    print row, col;
}



Ok, it half works. The only thing is it stops at the largest diagonal (exactly half-way), i.e. (4,1) (3,2), (3,2), (1,4) and does't go further (all the way to (4,4)).

Thanks anyway
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: Wavefront loop

Posted 07 November 2012 - 09:08 PM

Well begun is half done.


#include <iostream>
using namespace std;

#define ARRAYSIZE 21


void initialize(char ocean[][ARRAYSIZE], char ch)
{
  int row, col;
  for(row = 0; row < ARRAYSIZE; row++) {
    for(col = 0; col < ARRAYSIZE; col++) {
      ocean[row][col] = ch;
    }
  }
}


void display(char ocean[][ARRAYSIZE])
{
  int row, col;
  for(row = 0; row < ARRAYSIZE; row++) {
    for(col = 0; col < ARRAYSIZE; col++) {
      printf(" %c ", ocean[row][col]);
    }
    printf("\n");
  }
  printf("\n");
  getchar();
}


void wavefront(char ocean[][ARRAYSIZE], char ch)
{
  int diagonal, row, col;
  for(diagonal = 0; diagonal < ARRAYSIZE; diagonal++)
  {
    col = diagonal;
    for(row = 0; row <= diagonal; row++, col--) {
      ocean[row][col] = ch;
    }
    display(ocean);
  }

  for(diagonal = 1; diagonal < ARRAYSIZE; diagonal++)
  {
    col = diagonal;
    for(row = ARRAYSIZE-1; row >= diagonal; row--, col++) {
      ocean[row][col] = ch;
    }
    display(ocean);
  }

}


/* Press enter after each wave */

int main()
{
  char ocean[ARRAYSIZE][ARRAYSIZE];

  initialize(ocean, ' ');
  wavefront(ocean, '*');

  cout << "Press enter to continue. " << endl;
  cin.get();
  return(0);
}


Was This Post Helpful? 1
  • +
  • -

#5 lw2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 03-November 12

Re: Wavefront loop

Posted 08 November 2012 - 05:31 AM

Works perfect, thanks. Just for reference, here is the Fortran version:


DO diag=1,M
        col = diag
        DO row=1,diag
            PRINT *, "i= " , col , " j= ", row
            col=col-1
        END DO
END DO


DO diag=2,M
        col = diag
        DO row=M,diag,-1
            PRINT *, "i= " , col , " j= ", row
            col=col+1
        END DO
END DO



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1