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
Wavefront loop
Page 1 of 14 Replies - 1286 Views - Last Post: 08 November 2012 - 05:31 AM
Replies To: Wavefront loop
#2
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
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
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|