Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Priority Queuing
#1
This is a fun little program that can be done in about 20 lines of code.

PURPOSE:
Given a queue of 4 entries, let the user select one of the entries to be moved to the top of the queue, rearranging all the other entries accordingly.

STARTING QUEUE...: 1 2 3 4
User selects #3.
UPDATED QUEUE....: 3 1 2 4
User selects #1.
UPDATED QUEUE....: 1 3 2 4
User selects #1.
UPDATED QUEUE....: 1 3 2 4 (the same, #1 was already at top)

You'll love it!
*****
Reply
#2
20 lines of code? I must not be understanding the question correctly.

What do you mean by "entry"?

Meg.
Reply
#3
I don't understand what the number of lines of code has to do with understanding the problem.

An entry in a queue is like an entry or element or slot in a table. If I have a table for converting ASCII to EBCDIC, I will have 256 entries or elements in the table.
*****
Reply
#4
3 Lines!

Code:
' Priority Queuing
' Oracle
' webmaster@qbnz.com

DIM entries(1 to 4) AS INTEGER ' Does this line count?

' These definately don't, do they. They just
' initialise the values in the array
FOR cnt% = 1 to 4
  entries(cnt%) = cnt%
NEXT
DO  ' This doesn't count either
CLS  ' Or this

' Not even these count!

FOR inc% = 1 TO 4
  PRINT "Entry"; inc%; "="; entries(inc%)
NEXT
INPUT "Which entry do you want moved to the top?"; entrynum%

' Here's the meat. 3 lines!!

1 FOR count% = entrynum% TO 2 STEP -1
2   SWAP entries(count%), entries(count% - 1)
3 NEXT

LOOP ' Beat that!

There's no way that can be beaten.
Reply
#5
Soory, Oracle, but it only works sometimes.

If the first time the user hits 3, it works.

If the second time he hits 1 or 2, it doesn't work. This second time only works if he hits 4.

PS: On the PRINT line, it should be inc%, not inc.
*****
Reply
#6
No Moneo, it does work perfectly. You are choosing the array number to sort to the top, not the actual number. If you'd like the actual number, I'll alter it, but I think the algorithm is the important thing. It does work.
Reply
#7
After fixing the inc% on the PRINT statement and running it again, I realized that you "enhanced" the specifications by displaying the entry numbers as well as their content. This allowed you to only use one array, instread of two needed to implement the original specifications of only displaying the contents, and having the user refer to the content value that he wants to move to the top of the queue.

In any event, your solution works, shows talent, and deserves credit.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)