prampuria said:
My 2-dimensional array is like this.
int array[7][4];
1 3 6 10 14 18 22
2 5 9 13 17 21 25
4 8 12 16 20 24 27
7 11 15 19 23 26 28
I need to run through it "diagonally" like this and print the data:
1
2 3
4 5 6
7 8 9 10
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25
26 27
28
any psuedo code would be helpful.
Thanks in advance.
Hmmmm, let us look at this one step at a time.
The first number is a row #0 and column #0.
For the second value, we go to row 1, column zero.
Note that we incremented the row, not the column.
The third value is at row #0 and column #1.
Hmmm, for this position we subtracted 1 from the
row and added one to the column.
The fourth value is at row #2, column #0.
The fifth value is at row #1, column #1.
Notice that we subtracted 1 from the row and
added one to the column.
So now we make a table of positions we have
to visit (or traverse):
Visitation
Order Row Column
---------- ---- ------
1 0 0
2 1 0
3 0 1
4 2 0
5 1 1
6 0 2
7 3 0 + 0
8 2 1 + 0
9 1 2 + 0
10 0 3 + 0
11 3 0 + 1
12 2 1 + 1
13 1 2 + 1
14 0 3 + 1
15 3 0 + 2
16 2 1 + 2
17 1 2 + 2
18 0 3 + 2
Now look at the above data and figure out if
there are any patterns. If you can't find
any relations, try to figure out how to
calculate the next location.
From the above table, we notice that the row
is always decremented by one and the column
is always incremented by one, EXCEPT for certain
conditions.
List those conditions and you will be on your way.
Step through your algorithm for each visitation
order and verify that your exceptions are correct.
Hint: lookup modulo arithmetic, especially the
'%' operator.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book