Qbasicnews.com

Full Version: help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
pls tell me how to do this program

Q. write a QBASIC program to read 2 one dimensional arrays A and B from the keyboard. The program should mearge them in to single sorted array C that contains every item from A and B, in sorted assending order.

note:The sorting of arrays should be written as a separate SUB Procedure Called"Sort". The exchange of the individual item should be written as a separate SUB Procedure called "Swap".
Hmmm....

[syntax="QBasic"]'- Something for spidy
'- 03/09/2006
'- Joe King

DECLARE SUB Sort (s AS STRING)
DECLARE SUB SwapStrings (a AS STRING, b AS STRING)

DIM a AS STRING
DIM b AS STRING
DIM c AS STRING

'- Get Input from Keyboard
INPUT "Enter A: ", a
INPUT "Enter B: ", b

'- Merge the strings
c = a + b

'- Call the Sort sub to sort the elements in the string
Sort c

'- Finally, print out the resulting string
PRINT c

END

SUB Sort (s AS STRING)

DIM ElementA AS STRING
DIM ElementB AS STRING

'- Sorting Algorithm (Insertion Sort)
FOR i = 1 TO LEN(s)

FOR n = i TO LEN(s)

ElementA = MID$(s, i, 1)
ElementB = MID$(s, n, 1)

IF ElementA > ElementB THEN CALL SwapStrings(ElementA, ElementB)

MID$(s, i) = ElementA
MID$(s, n) = ElementB

NEXT n

NEXT i

END SUB

SUB SwapStrings (a AS STRING, b AS STRING)

'- Swapping 2 Strings

DIM c AS STRING

c = a
a = b
b = c

END SUB
[/syntax]

Quote:note:The sorting of arrays should be written as a separate SUB Procedure Called"Sort". The exchange of the individual item should be written as a separate SUB Procedure called "Swap".
Swap is a keyword that is already defined in Qbasic; therefore, you cannot write your own sub named "Swap". That's why I changed the name to "SwapStrings" in my code.