Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with bubble sort 1

Status
Not open for further replies.

qbasicstudent123

Technical User
Apr 8, 2003
1
US
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:
Prisoner.....Ship...........Cell#
Kanobi.......Falcon.........328
Spock........Enterprise.....562
Yoda.........None...........122
Mudd.........Pleasure Dome..222
Khan.........Botany Bay.....009
Jetson.......Astrofly.......468
Rogers.......Galaxy 2.......727
Koenig.......Alpher.........999
Adama........Galactic.......987
Who..........Tardis.........585
The output should be alphabetized by name using a bubble sort.

This is what I now have:

CLS

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

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

'*** Collecting data ***

FOR count = 1 TO 10
INPUT "Enter the prisoner's name: ", prisoner$(count)
PRINT
INPUT "Enter the ship that the prisoner is on: ", ship$(count)
PRINT
INPUT "Enter the cell that the prisoner is being held in: ", cell$(count)
PRINT
CALL dividingline
PRINT
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
END IF
NEXT count
final = final - 1
LOOP

' *** 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 "----------------------------------------------------------"
END SUB

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.
 
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
forum314
 
beat me too it, i knew that one, i knew that one
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top