473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.
Jul 22 '05 #1
3 1457
prampuria wrote:
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?
Jul 22 '05 #2

"prampuria" <pr*******@yahoo.com> wrote in message
news:36**************************@posting.google.c om...
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
Jul 22 '05 #3
prampuria wrote:
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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: spar | last post by:
I'm converting a Perl script to Python and have run into something I'm not sure how to do in Python. In Perl, I am running through a couple loops and inserting values directly into a complex...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
6
by: Mahesh Hardikar | last post by:
Hi , Oracle 8.1.7.0.0 on HP-UX 11.0 I am a newbie as far as PL-SQL is concerned . I have Sybase/MSSQL T-SQL background. We have a report which uses a select statement . This select...
3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
1
by: Tim Smith | last post by:
Hi, I have a table ORDER_DETAIL with 22 million rows which has an index of (person_id, code_id, created_dtt) I have another ORDER table with 5 million rows which has an index of (order_dtt,...
7
by: seia0106 | last post by:
Hello, Writing a program in c++ that should use complex numbers I have two choices before me. 1- define a struct for complex data i.e struct {float real,imag; }ComplexNum; 2-use an array...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
11
by: db2admin | last post by:
hello, is it possible to write compound sql without stored procedure or trigger. can i just run in command center of db2. regards, jagdip singh
11
by: Fabri | last post by:
I searched and tried to develop (with no luck) a function to do the following: I have a string that may be: "Le'ts go to <a href="my.htm">my car</a>. Tomorrow I'll have to buy a new car. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.