Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Parsing M*N matrix on Periphery

Amarbir Singh
Greenhorn
Posts: 20
Hi All,

I have this query of parsing an M*N matrix on periphery...
e.g we are given 4*4 matrix as below...

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

We need to parse it to display output in the sequence as shown....

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10.

Please guide me to write the algorithm generic for the same....!

Thank you very much.
cheers
Amar

Peter Chase
Ranch Hand
Posts: 1970
Presumably, this is a homework assignment. I hope no-one's going to do your homework for you. But, if you show how you've tried, and where you're stuck, we're happy to help.

I don't know if there's a "clever" solution to this problem. I'd do it the "dumb" way, with 4 loops. Does that help you to get started?

(Please don't anyone post full code for their "clever" or "dumb" solution!)

Paul Clapham
Sheriff
Posts: 21416
33
The recursive solution might be considered "clever".

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
How would you tell somebody who doesn't know the rules how to move a coin around a chess board in this pattern? "go east, go east, go south ..."

How would you describe the rules? Seriously, see if you can write it out in plain English first. Show us what you get!

Amarbir Singh
Greenhorn
Posts: 20
Hi Frnz..Thank you very much....for your guidance..
what i was looking for ... Is there any mathematical algorithm for handling M*N matrix ... to parse it on the periphery...!

or do we need to go through 4-different loops.....?

cheers

" Art of people is the true mirror of their minds...! "

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Um, the mathematical algorithm for solving this will probably involve four inner loops inside one outer loop. Does that answer your question?

Amarbir Singh
Greenhorn
Posts: 20
I wrote following two functions to increment/decrement based on flag...
now the issue is how to call these symmetrically in loop so that parameter values are correctly passed.

Is there any symmetry that i can follow in loop to call these functions for desired output.

[ added code tags - Jim ]
[ October 05, 2007: Message edited by: Jim Yingst ]

Amarbir Singh
Greenhorn
Posts: 20
I'm using two variables to control the number of iterations in the while loop that gets changed when we do i--/j--/j++ etc....

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I did something in Pascal once with "delta pairs" that tell how to adjust X & Y to go each direction. With 0,0 in the top left corner ...

EAST: +1, 0
SOUTH: 0, +1
WEST: -1, 0
NORTH: 0, -1

Now we can do it in one loop, I think:

That's totally untested. Surely one could clean it up to eliminate the double "next cell" thingy. Do you think it would work?
[ October 06, 2007: Message edited by: Stan James ]

Amarbir Singh
Greenhorn
Posts: 20
Thanks James,

Your algorithm will be of great help. Marking the nodes as Visited/Unvisited should help make it work perfectly fine.