Results 1 to 8 of 8
  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default 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))

    Need help with algorithm for program-spiral_layout.jpg
    Attached Files Attached Files
    Last edited by Galway; 11-18-2016 at 03:19 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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 05:58 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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 11:22 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default 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. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with algorithm for program

    Quote Originally Posted by Galway View Post
    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.

    Quote Originally Posted by Galway View Post
    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 01:15 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 8
    Last Post: 10-19-2016, 09:06 PM
  2. Replies: 5
    Last Post: 01-29-2016, 10:11 PM
  3. Mph to Kph Algorithm
    By jbing109 in forum New To Java
    Replies: 4
    Last Post: 01-21-2013, 09:26 AM
  4. all i need is algorithm
    By naved in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 03:34 PM
  5. Program where the user can choose the algorithm
    By le_albina@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 05:45 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
  •