Thread: store and sort help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    store and sort help

    hi guys, for my HNC project i'm making an elevator programme that decides which floor to visit depending on the direction is has just traveled and i'm having trouble finding code that i could use to sort the input from the user and then sort this.

    E.G if the elevator traveled from floor 1 to 5 and then floors 4 and 6 are selected at the same time. i want the elevator to go to floor 6 first as it travel up to get to floor 5, then go to floor 4. if you get what i mean, any help would be great as i'm well and truly stumped

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If you just looking for some ready code, you can take a look at my uploads: (one is in cpp but I used as less C++ as possible, in order C people can use it too).



    Also welcome to the forum.

    However, I have to inform you, that if you plan to build your own sorting functions, then you should try yourself at first and then post your code in code tags, like this [code]/*your program*/[/code] and then you will receive much help, since here, there are lot of people able and willing to help
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    thanks a lot ill have a look through them later and hopefully upload some code you can nitpick, i was thinking of using an array as i will only be using 10 floors, using an if statement to decided on which way to sort them depending on the way the lift traveled (accending for lift up and deciding for lift down) and then storing then other inputs until the elevator reaches the lowest input floor?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by boulton1991 View Post
    thanks a lot ill have a look through them later and hopefully upload some code you can nitpick, i was thinking of using an array as i will only be using 10 floors, using an if statement to decided on which way to sort them depending on the way the lift traveled (accending for lift up and deciding for lift down) and then storing then other inputs until the elevator reaches the lowest input floor?
    I'm not sure how elevators are programmed, but I'm thinking that no sorting is needed, and the elevator headed up, is not going to ever backtrack back downward, to pick up passengers.

    Because it's an up elevator, and it will go to all the upward requests, before changing to a down elevator, at which time it will handle all the down floor requests only. It's neither a FIFO queue, nor a LIFO stack. Requests are honored IN THE DIRECTION OF TRAVEL by the elevator, at that time, in floor # order, up or down.

    No one wants to be going up, only to find the elevator going back down to pick up more passengers, before heading back up again. Indeed, if that continued, the up passengers would never reach their very top floor, in a busy building.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Yeah that's what I was trying to explain but you did a better job haha, how would I go about doing that? It's making my head spin

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would call it a queue, but it's really just a list of floors: so floor[11]. I'd use an array with 11 elements: Basement is 0, floors are numbers 1-10 (there is no "floor 0", of course).

    That is the floor making the request. Whether they are requesting an elevator to get into one, or requesting a destination floor or direction of travel, (an up or down elevator), doesn't matter - they're all requests, so they're all destinations, to the elevator. Elevator must stop and open doors, in either case.

    The elevator has a variable - like a flag or boolean variable that is either 1 or 0. 1 is up, and down is 0. After the last upward request, direction=!direction, and the int or boolean variable "direction" changes, and the up elevator becomes the down elevator, or vice-versa.

    When you get a request for an elevator, floor[yourFloor] becomes 1. After that floor has been visited, that floor[number] becomes 0. The next floor the elevator visits is the next floor in the direction of travel of the elevator: up elevator on floor 3 will take any request for floor[4] next, then floor[5], then floor[6], etc. Because it's now an up elevator. Going down is just the opposite - requests for higher floor are put into the floor[number]=1, array, but they won't be honored until the elevator finishes it's down requests, and becomes an up elevator again.

    The best way to understand it better, is to take a few pages of paper, and a paperweight. The pages represent the floors, from left to right on a tabletop, and the paperweight is the elevator. Ignore the people for the time being. (They always complain anyway - lol). Work the "elevator" up and down and make requests and you'll see the patterns emerge for the logic your program will need, after bit.

    Don't prioritize all the requests, because that's a lot of bookkeeping, and you don't need it, anyway. It's like quicksand - avoid it. An efficient elevator is never going to be a perfectly FAIR elevator, to every request. Some requests will wait longer than others. That's not a shortcoming of the elevator service.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  3. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  4. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM