Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

help with bubble sort

help with bubble sort

help with bubble sort

Please help me with this problem. I am having a problem with my bubble sort. Here is the problem and what I have so far:

The Klingons have captured Spock, science officer of the U.S.S. Enterprise. Captain Kirk has broken into their prison colony to free him. He has reached the computer that possesses information concering all of the prisoners, including their cell numbers. Write a program to propmt the user to enter the following data into the program. The data should be stored in three parallel arrays:
Mudd.........Pleasure Dome..222
Khan.........Botany Bay.....009
Rogers.......Galaxy 2.......727
The output should be alphabetized by name using a bubble sort.

This is what I now have:


DIM prisoner$(1 TO 10), ship$(1 TO 10), cell$(1 TO 10)

PRINT TAB(10); "This program will collect data on prisoner locations."

'*** Collecting data ***

FOR count = 1 TO 10
INPUT "Enter the prisoner's name: ", prisoner$(count)
INPUT "Enter the ship that the prisoner is on: ", ship$(count)
INPUT "Enter the cell that the prisoner is being held in: ", cell$(count)
CALL dividingline
NEXT count

' *** Sorting prisoner names in alphabetical order ***

final = 10
flag = 1
DO WHILE flag = 1
flag = 0
FOR count = 1 TO final
IF prisoner$(count) > prisoner$(count + 1) THEN
SWAP prisoner$(count), prisoner$(count + 1)
SWAP ship$(count), ship$(count + 1)
SWAP cell$(count), cell$(count + 1)
flag = 1
NEXT count
final = final - 1

' *** Display Output ***

PRINT TAB(15); "Prisoner"; TAB(30); "Ship"; TAB(45); "Cell #"
CALL dividingline

FOR count = 1 TO 10
PRINT TAB(15); prisoner$(count); TAB(30); ship$(count); TAB(45); cell$(count)
NEXT count

SUB dividingline
PRINT "----------------------------------------------------------"

After I enter all of the prisoners, the ship they are on, and the cell #'s, I get a "Subscript Out Of Range" error, and the "IF prisoner$(count) > prisoner$(count + 1) THEN" turns white. Any help would be greatly appreciated. Thank you.

RE: help with bubble sort

If you wrote
IF prisoner$(count) > prisoner$(count + 1) THEN
, then on last item (count + 1) would be greater then (final).
So you should use
FOR count = 1 TO final-1
instead of
FOR count = 1 TO final.

BTW Then your programs breaks you can swich to "Immediate" window (with mouse or F6 key)  and check out variables. For example type
? count
in Immediate, then press Enter - and you will see on which FOR iteration your program stopped.

BTW2 there is a special forum for Qbasic

RE: help with bubble sort

beat me too it, i knew that one, i knew that one

RE: help with bubble sort

Your DIM statemnts are DIM prisoner$(1 TO 10), ship$(1 TO 10), cell$(1 TO 10)and you are entering 11 lines of data. Change the folowing statements to DIM prisoner$(1 TO 11), ship$(1 TO 11), cell$(1 TO 11), FOR count = 1 TO 11, final = 10, and FOR count = 1 TO 11.  Any reference to 10 change it to 11.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close