Qbasicnews.com
August 26, 2019, 12:11:54 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Challenge: Priority Queuing  (Read 3415 times)
Moneo
Na_th_an
*****
Posts: 1971


« on: July 04, 2003, 03:52:21 PM »

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!
*****
Logged
Meg
Ancient QBer
****
Posts: 483


um
« Reply #1 on: July 04, 2003, 04:38:42 PM »

20 lines of code?  I must not be understanding the question correctly.

What do you mean by "entry"?

Meg.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: July 04, 2003, 06:11:33 PM »

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.
*****
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #3 on: July 04, 2003, 09:11:51 PM »

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.
Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #4 on: July 04, 2003, 09:28:57 PM »

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.
*****
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #5 on: July 04, 2003, 09:36:41 PM »

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.
Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: July 04, 2003, 09:57:44 PM »

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.
*****
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!