 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
prampuria Guest
|
Posted: Wed Sep 29, 2004 9:16 pm Post subject: complex for loops? |
|
|
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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Sep 29, 2004 9:26 pm Post subject: Re: complex for loops? |
|
|
prampuria wrote:
| Quote: | 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.
|
The number of rows in the output is 7 plus 4-1, right?
The number of columns is either 4 or fewer, depending on
where you start. The [i,j] pairs you output should be
relatively easy to calculate based on [k,m] of the output
loops. Have you made _any_ effort to figure this out?
BTW, what's your C++ language question? Homework we don't
do, pseudocode you asked for has nothing to do with C++,
AFAICT. So why did you post here?
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Wed Sep 29, 2004 11:28 pm Post subject: Re: complex for loops? |
|
|
"prampuria" <prampuria (AT) yahoo (DOT) com> wrote
| Quote: | 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.
|
Write down the indices of each of the items in your example
output. You should see a pattern which you can convert
to a formula. BTW this is not a question about C++, so
isn't really topical here.
-Mike
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Thu Sep 30, 2004 1:22 pm Post subject: Re: complex for loops? |
|
|
prampuria wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|