QBasic.. hard...

Binary search, bubble sort...

Page 1 of 1

2 Replies - 3173 Views - Last Post: 30 May 2008 - 07:20 PM Rate Topic: -----

#1 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

QBasic.. hard...

Post icon  Posted 25 April 2008 - 06:18 AM

Chapter 9: Arrays and Searching and Sorting

Level 2, Problem 3
Due Date: Tuesday, April 29, 2008

Level 2, Problem 3 (Klingon Prisoners)

TASK

Create a flowchart for a program that allows the user to enter the name, ship, and prison cell number for prisoners in the Klingon prison colony. The data should be stored in three parallel arrays, and the arrays should be alphabetized by prisoner name. The user should be able to display information about a particular prisoner when his or her name is entered.

Use QBasic to write a program that allows the user to enter the name, ship, and prison cell number for prisoners in the Klingon prison colony. The data should be stored in three parallel arrays, and the arrays should be alphabetized by prisoner name. The user should be able to display information about a particular prisoner when his or her name is entered.
Your program should include the following:

·Correct use of three parallel arrays
·Use of binary search to locate prisoners by name
·Correct, formatted output


I am not kidding this is what it really says, lol

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 concerning all of the prisoners, including their cell numbers. Write a program to prompt the user to enter the following data into the program. The data should be stored in all 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
Koeing Alpher 999
Adama Galactic 987
Who Tardis 585

Alphabetize the data by name, and display information regarding a prisoner when his or her name is entered. Use a binary search routine in your program to speed Captain Kirk's search for Spock.

Here is what I have so far in regards to the program I have to create...

I am wondering if when Name$ or the name of the prisoner is sorted alphabetically if it will switch the names around leaving the
cell numbers and ship names behind making them be with another prisoners name.

I am also wondering on how to make a freakin binary seach for this program, lol

'*** This program displays information reguarding ***
'*** A prisoner  when his or her name is entered.  ***


'*** This procedure prompts the user to  enter the name ***
'*** of the prisoner, the ane of the prisoner's ship	***
'*** and the prisoners cell number.					 ***

CLS

DIM Nam$(1 TO 10), Ship$(1 TO 10), Cell(1 TO  10)

FOR Count = 1 TO 10
   INPUT " Enter the name of the prisoner:",  Nam$(Count)
  PRINT
   INPUT " Enter the name of the prisoner's ship:",  Ship$(Count)
  PRINT
   INPUT " Enter the cell number where the prisoner  is held:", Cell(Count)
  PRINT
NEXT  Count

CLS


'*** This procedure uses the  bubble sort to alphabetically sort the array Nam$ ***

Final = 9
Flag =  1

'*** Execute loop until no exchanges are made ***

DO WHILE Flag  = 1
Flag = 0
FOR Count = 1 TO Final
   '*** If names are out of order,  switch them ***
  IF Nam$(Count) > Nam$(Count+1) THEN
   SWAP  Nam$(Count), Nam$(Count+1)
   Flag = 1	  '*** Reset Flag, indicating a  switch
  END IF
 NEXT Count
Final = Final -  1
LOOP


'*** This procedure prompts the user to  enter the name of a prisoner ***
'*** and displays information about that  prisoner					***

PRINT
PRINT
INPUT  "Enter the name of the prisoner you wish to view information  about:",
PRINT
PRINT



Thanks for any help anyone may be able to provide.

Is This A Good Question/Topic? 0
  • +

Replies To: QBasic.. hard...

#2 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: QBasic.. hard...

Posted 27 May 2008 - 08:27 AM

View PostStealthTools, on 25 Apr, 2008 - 08:18 AM, said:

I am wondering if when Name$ or the name of the prisoner is sorted alphabetically if it will switch the names around leaving the
cell numbers and ship names behind making them be with another prisoners name.



The way your code is written, yes. The order of the names will change but the order of the ship-names and cells will remain where they are.

You will need to SWAP their values at the same time you SWAP the values of the Name fields.

This post has been edited by djkitt: 27 May 2008 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,908
  • Joined: 13-October 07

Re: QBasic.. hard...

Posted 30 May 2008 - 07:20 PM

Yes, like djkitt said, you will have to swap the other arrays as well:
DO WHILE Flag  = 1
Flag = 0
FOR Count = 1 TO Final
   '*** If names are out of order,  switch them ***
  IF Nam$(Count) > Nam$(Count+1) THEN
   SWAP  Nam$(Count), Nam$(Count+1)
   SWAP  Ship$(Count), Ship$(Count+1)
   SWAP  Cell(Count), Cell(Count+1)
   Flag = 1	  '*** Reset Flag, indicating a  switch
  END IF
NEXT Count
Final = Final -  1
LOOP


as for a binary lookup?
you would have to ask your teacher about how exactly she wants to do this "binary" lookup.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1