# Thread: Need help with algorithm for program

1. Member
Join Date
Sep 2016
Posts
15
Rep Power
0

## Need help with algorithm for program

I saw someone else post this problem sheet and I wanted to see if I could get it out. I managed to get it working for everything except the last part about the "Spiral Layout". Anyone any tips on how to make an algorithm for it?

I just could not find any pattern that I could manage to program it. For the square and pyramid ones I made a loop to find the row of the element. Then I used that to figure out its relative position from the center column but I can't see how to to this for the Spiral version.

Any help would be appreciated as this is distracting me from work I actually should be doing.(I get a little too focused on problems sometimes)

edit: As an overview I need some way of telling the horizontal and vertical distance between any two keys on a "Spiral Layout" Keyboard.(e.g. the distance between 25 and 48 is 1,the distance between 25 and 9 is 2.(Look at diagram))

Last edited by Galway; 11-18-2016 at 02:19 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need help with algorithm for program

I am not certain if you are talking about how to create the spiral or compute the distance.

The patterns are actually irrelevant and only there to describe the relationship between the key and its coordinate position.
So since the keys go from 0 to some value n, just create a class that has the row and col position. Store those
in a list from 0 to n. Then just use the key value to index the list and get the position of the key and perform the computation.

For any pattern, all you need is a single array or better, a list implementation.

Regards,
Jim
Last edited by jim829; 11-18-2016 at 04:58 PM.

3. Member
Join Date
Sep 2016
Posts
15
Rep Power
0

## Re: Need help with algorithm for program

I think I have managed to make a program now which creates a two dimensional int array which will fill in the right values for a spiral keyboard.

I am now stuck as I don't know a method that can return the row and column position of an element in the array?
I looked up on it and I saw people just using two for loops to loop through the columns and rows but I was hoping there was some in built function in the array class
something like <arrayname>.positionOf(4);
Also if there is a method like this what is its return type an array of ints?(e.g. {1,3};)

4. ## Re: Need help with algorithm for program

return the row and column position of an element
A Map<Position, RowCol> would work. Using built-in java classes: Map<Integer, Point>
Where RowCol is a simple class that holds the row and column.

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need help with algorithm for program

Ok, you have generated the 2D spiral square. That means that during that process you must know for any key
the row and column value. So you can do it two possible ways.

1. If you are creating the spiral class in key order from 0 to n, then simply create a List of type Point
and add each element in succession to the list with the the point being a Point(r,c) where r is the row and c
is the column of the key. Then to get say, the coordinate for key 10, use list.get(10). Note: The Point class
uses x and y so you could also create your own class if you find that confusing).

2. If you are creating the spiral class out of order, you still need to know the value, row, and column. So
create class to hold those values and add each class instance to a list. Then sort the list based on value so that
the list is sorted from 0 to n. Then reference the key's coordinates as before.

But as I said in previous posts, you don't need a 2D array for this. You just need to know the row/column
location of a given key to populate your list.

Regards,
Jim
Last edited by jim829; 11-18-2016 at 10:22 PM.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need help with algorithm for program

Yeah. I considered that too. When I wrote my version of the program a few days ago (yes, I confess) I used
the fact that there was a clear relationship between keys and took advantage of it. Using a map would provide
a more general solution independent of the nature of the keys.

Regards,
Jim

7. Member
Join Date
Sep 2016
Posts
15
Rep Power
0

## Re: Need help with algorithm for program

Thanks for the replies Norm and jim829, decided to try the List approach over Map as have never used a Map.
Was glad I had created the spiral class in key order from 0 to n as sorting it probably would have been messy.
Program essentially worked straight away(only spelling mistakes).

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Need help with algorithm for program

Originally Posted by Galway
decided to try the List approach over Map as have never used a Map
Maps are quite useful. They basically use objects as keys to retrieve values. And they are pretty easy to use.
I recommend you learn them asap.

Originally Posted by Galway
Was glad I had created the spiral class in key order from 0 to n as sorting it probably would have been messy.
Glad it worked! However, you did this, I presume, to test your programming abilities. So I would still recommend implementing
the Comparable interface and do the sort for practice. You could sort it in ascending order for the natural order.

You could also implement a separate sorting class that implements the Comparator interface. This would allow
one to specify any sorting criteria and passed to the Collections.sort method. For example, you could sort on row then
column or column then row.

To test it, you could mix up your list using Collections.shuffle(). It would be good practice in implementing two related interfaces.

Regards,
Jim
Last edited by jim829; 11-19-2016 at 12:15 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•