# Wavefront loop

Page 1 of 1

## 4 Replies - 6009 Views - Last Post: 08 November 2012 - 05:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298456&amp;s=2dccfd329e2b7cb0067f883e1b75f891&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lw2

Reputation: 1
• 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

• Duke of Err

Reputation: 1852
• Posts: 6,661
• 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;
}

```

### #3 lw2

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

## Re: Wavefront loop

Posted 07 November 2012 - 10:29 AM

#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

### #4 #define

• Duke of Err

Reputation: 1852
• Posts: 6,661
• 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);
}

```

### #5 lw2

Reputation: 1
• 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

```