Qbasicnews.com

Full Version: Im stuck!! Sorting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How would i get this program to sort.....Sort if u dont enter a new name and sort if u do enter a name....Thanx

Code:
DECLARE SUB Information (Nme$, Address$, Id, gender%, Payrate)
DECLARE SUB Sort ()
DECLARE SUB File ()
DECLARE SUB AddPerson (last$, First$, Address$, gender%, Payrate)

DIM SHARED SortItems
DIM SHARED gender.type$(1 TO 2)
DIM SHARED Nme$(1 TO 12)
DIM SHARED Address$(1 TO 12)
DIM SHARED Id(1 TO 12) AS LONG
DIM SHARED gender%(1 TO 12)
DIM SHARED Payrate(1 TO 12)

TYPE ClassRecord
Nme AS STRING * 15
Address AS STRING * 42
Id AS INTEGER
gender AS STRING * 2
Payrate AS SINGLE
END TYPE

DIM Info(1 TO 12) AS ClassRecord

'---------------------------------Menu---------------------------------------'
CLS

INPUT "Do you want to add another person"; person$
IF person$ = "Y" OR person$ = "y" THEN
    PRINT "Please do not use commas!!"
    PRINT
    INPUT "Enter last name, first name:", last$
    INPUT "Enter your address:", Add$
    INPUT "Enter your id number:", idnum
    INPUT "Enter your gender (1 for M, 2 for F:) ", gend%
    INPUT "Enter your payrate:", rate

ELSE

CLS
PRINT "Choose how you want to sort the items!!"
PRINT "--------------------------------------"
PRINT
PRINT "By name          Press 1"
PRINT "By address       Press 2"
PRINT "By id            Press 3"
PRINT "By gender        Press 4"
PRINT "By payrate       Press 5"
INPUT "Which do you want to sort by"; SortItems

END IF
'------------------------Call Subs And Define Gender-------------------------'

gender.type$(1) = "Male"
gender.type$(2) = "Female"

CALL Information(Nme$, Address$, Id, gender%, Payrate)
CALL Sort
CALL File
'CALL AddPerson(last$, First$, Address$, gender%, Payrate)

CLS

'----------------------------------------------------------------------------'
'---------------------------------Print Info---------------------------------'
IF person$ = "N" OR person$ = "n" THEN
  FOR i = 1 TO 11
  PRINT "Name: "; Nme$(i);
  SLEEP 1
  PRINT " Address: "; Address$(i)
  PRINT "ID: "; Id(i)
  PRINT "Gender: "; gender.type$(gender%(i))
  PRINT "Hourly Salary: $"; Payrate(i)
  PRINT
  NEXT i

ELSE

  FOR i = 1 TO 11
  PRINT "Name: "; Nme$(i);
  SLEEP 1
  PRINT " Address: "; Address$(i)
  PRINT "ID: "; Id(i)
  PRINT "Gender: "; gender.type$(gender%(i))
  PRINT "Hourly Salary: $"; Payrate(i)
  PRINT
  NEXT i

  PRINT "Name: "; last$;
  PRINT " Address: "; Add$
  PRINT "ID: "; idnum
  PRINT "Gender: "; gender.type$(gend%)
  PRINT "Hourly Salary: $"; rate

END IF
'----------------------------------------------------------------------------'

SUB AddPerson (last$, First$, Address$, gender%, Payrate)

IF person$ = "Y" THEN

LET Nme$(12) = last$ + First$
LET Address$(12) = Address$
LET Id(12) = Id
LET gender%(12) = gender%
LET Payrate(12) = Payrate

OPEN "Info.DAT" FOR APPEND AS #1
  WRITE #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
CLOSE #1

ELSE

END IF

END SUB

SUB File

OPEN "Info.DAT" FOR INPUT AS #1
  FOR i = 1 TO 12
    INPUT #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)

    PRINT Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
  NEXT i

CLOSE #1
END SUB

SUB Information (Nme$, Address$, Id, gender%, Payrate)

Nme$(1) = "Preston, Drew"
Nme$(2) = "Peterman, Alex"
Nme$(3) = "Booth, Shane"
Nme$(4) = "Young, Chris"
Nme$(5) = "Carreer, Jim"
Nme$(6) = "Cardwell, James"
Nme$(7) = "Monroe, Judy"
Nme$(8) = "Belcher, Jacob"
Nme$(9) = "Eisenback, Josh"
Nme$(10) = "Allen, Rachel"
Nme$(11) = "Yancey, Crystal"
Nme$(12) = last$

Address$(1) = "9115 Benson Road, Bagdad, KY 40003"
Address$(2) = "0204 Adair Avenue, Shelbyville, KY 40065"
Address$(3) = "0805 Lakeview Drive, Shelbyville, KY 40065"
Address$(4) = "0555 Alter Avenue, Hollywood, CA 40219"
Address$(5) = "0710 Hallow Lane, Newport, KY 49281"
Address$(6) = "0177 Ivy Lane, Evansville, IN 33363"
Address$(7) = "0106 Ada Avenue, Shelbyville, KY 40065"
Address$(8) = "3228 Bluegrass Drive, Shelbyville, KY 40065"
Address$(9) = "0901 Heavens Way, Shelbyville, KY 40065"
Address$(10) = "0141 Fifth Street, Apolo Beach, FL 90210"
Address$(11) = "0740 Zero Lane, Shelbyville, KY 40065"
Address$(12) = Add$

Id(1) = 813176
Id(2) = 437751
Id(3) = 314207
Id(4) = 132694
Id(5) = 421738
Id(6) = 324963
Id(7) = 927691
Id(8) = 393722
Id(9) = 842101
Id(10) = 870192
Id(11) = 746669
Id(12) = idnum

gender%(1) = 1
gender%(2) = 1
gender%(3) = 1
gender%(4) = 1
gender%(5) = 1
gender%(6) = 1
gender%(7) = 1
gender%(8) = 1
gender%(9) = 1
gender%(10) = 2
gender%(11) = 2
gender%(12) = gend%

Payrate(1) = 6.3
Payrate(2) = 8.13
Payrate(3) = 8
Payrate(4) = 9.5
Payrate(5) = 7.35
Payrate(6) = 8.2
Payrate(7) = 6.95
Payrate(8) = 7.46
Payrate(9) = 6.25
Payrate(10) = 8.51
Payrate(11) = 6
Payrate(12) = rate
'-----------------------------File Opening-----------------------------------'

OPEN "Info.DAT" FOR OUTPUT AS #1
  FOR i = 1 TO 12
  
    WRITE #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
    
  NEXT i

CLOSE #1
END SUB

SUB Sort

SELECT CASE SortItems
CASE 1
    FOR i = 1 TO 12
    FOR j = 1 TO i
    IF Nme$(j) > Nme$(i) THEN
    SWAP Nme$(j), Nme$(i)
    SWAP Address$(j), Address$(i)
    SWAP Id(j), Id(i)
    SWAP gender%(j), gender%(i)
    SWAP Payrate(j), Payrate(i)
    END IF
    NEXT j, i

CASE 2
    FOR i = 1 TO 12
    FOR j = 1 TO i
    IF Address$(j) > Address$(i) THEN
    SWAP Nme$(j), Nme$(i)
    SWAP Address$(j), Address$(i)
    SWAP Id(j), Id(i)
    SWAP gender%(j), gender%(i)
    SWAP Payrate(j), Payrate(i)
    END IF
    NEXT j, i

CASE 3
    FOR i = 1 TO 12
    FOR j = 1 TO i
    IF Id(j) > Id(i) THEN
    SWAP Nme$(j), Nme$(i)
    SWAP Address$(j), Address$(i)
    SWAP Id(j), Id(i)
    SWAP gender%(j), gender%(i)
    SWAP Payrate(j), Payrate(i)
    END IF
    NEXT j, i

CASE 4
    FOR i = 1 TO 12
    FOR j = 1 TO i
    IF gender%(j) > gender%(i) THEN
    SWAP Nme$(j), Nme$(i)
    SWAP Address$(j), Address$(i)
    SWAP Id(j), Id(i)
    SWAP gender%(j), gender%(i)
    SWAP Payrate(j), Payrate(i)
    END IF
    NEXT j, i

CASE 5
    FOR i = 1 TO 12
    FOR j = 1 TO i
    IF Payrate(j) > Payrate(i) THEN
    SWAP Nme$(j), Nme$(i)
    SWAP Address$(j), Address$(i)
    SWAP Id(j), Id(i)
    SWAP gender%(j), gender%(i)
    SWAP Payrate(j), Payrate(i)
    END IF
    NEXT j, i
END SELECT
END SUB
again,

regular variable:

info.nme$(1)

TYPE variable:

info(1).nme$
Ignore the TYPE..I just need to sort it the way it is..Thanx
Ok, but why do you need a second print block? :|

You want to use REDIM. You need to copy your entire array to another temporary array, redim your array, then copy back. You can redim it by more than one space so that you don't have to redim all the time. (use UBOUND() to check for current array length)
I got it to print..Now i just need to sort it all..That is where im stuck
ok, you WILL have problems if you attempt to compare strings as numbers. you should use mid$

if asc(mid$(string(I),1,1)>asc(mid$(string(j),1,1) then ........

Those are my 2 cents

oh, mid$ is structured like "mid$(string, position to start at,number of chars to take)"

Anonymous

hey there hplilsnow. first of all let me say welcome. i hope you get all the help you need here.

alright, now to do just that Wink
i have made a couple of notes about ur proggie.. these should guide you

firstly, the sorting algo in ur prog is good. it works.

2nd DIE SLEEPS!!!!! Wink but if you HAVE to

3rd. the order in which events happen are a bit confuckled... perhaps you should sort before writing to the file? you are writing to the file, sorting, then reading from the (unsorted) file. it makes no sense dammit! lol

4th you should consider using a 0 or 1 system for gender... at least thisll keep it from crashing if gender = null : )

5th when you print the data at the end, you should check if Nme$() = "" so that null entries arent printed.

oh yeah, and you have to put a SHARED SortItems in the beginning of your sort bub. otherwise it thinks its 0 every time Wink

maybe ive helped you, maybe ive bored you. hey, its all relative.

peace
How would i do that tho..Im just stuck, confused whatever u wanna call it..

Anonymous

sounds to me like ur just lazy! Wink but, hey so am i, so... MERRY XMAS friend : )

Code:
DECLARE SUB Information (Nme$, Address$, Id, gender%, Payrate)
DECLARE SUB Sort ()
DECLARE SUB File ()
DECLARE SUB AddPerson (Last$, First$, Address$, gender%, Payrate)

DIM SHARED SortItems
DIM SHARED gender.type$(0 TO 1)
DIM SHARED Nme$(1 TO 12)
DIM SHARED Address$(1 TO 12)
DIM SHARED Id(1 TO 12) AS LONG
DIM SHARED gender%(1 TO 12)
DIM SHARED Payrate(1 TO 12)

TYPE ClassRecord

        Nme AS STRING * 15
        Address AS STRING * 42
        Id AS INTEGER
        gender AS STRING * 2
        Payrate AS SINGLE

END TYPE

DIM Info(1 TO 12) AS ClassRecord

'---------------------------------Menu---------------------------------------'

CLS

INPUT "Do you want to add another person"; person$

IF person$ = "Y" OR person$ = "y" THEN

        PRINT "Please do not use commas!!"
        PRINT
        PRINT "Example: Clinton William"
        INPUT "Enter last name, (space), first name:", Last$
        INPUT "Enter your address:", Add$
        INPUT "Enter your id number:", Idnum
        INPUT "Enter your gender (0 for M, 1 for F:) ", Gend%
        INPUT "Enter your payrate:", Rate

ELSE

END IF

        DO
      
                CLS
      
                PRINT "Choose how you want to sort the items!!"
                PRINT "--------------------------------------"
                PRINT
                PRINT "By name          Press 1"
                PRINT "By address       Press 2"
                PRINT "By id            Press 3"
                PRINT "By gender        Press 4"
                PRINT "By payrate       Press 5"
                INPUT "Which do you want to sort by"; SortItems

        LOOP UNTIL SortItems < 6 AND SortItems > 0

'----------------------------------------------------------------------------'

'------------------------Call Subs And Define Gender-------------------------'

gender.type$(0) = "Male"
gender.type$(1) = "Female"

CALL Information(Nme$, Address$, Id, gender%, Payrate)
CALL File
CALL AddPerson(Last$, First$, Address$, gender%, Payrate)

CLS

'----------------------------------------------------------------------------'

'---------------------------------Print Info---------------------------------'


        FOR i = 1 TO 11
              
                IF Nme$(i) <> "" THEN

                        PRINT "Name: "; Nme$(i);
                        SLEEP 1
                        PRINT " Address: "; Address$(i)
                        PRINT "ID: "; Id(i)
                        PRINT "Gender: "; gender.type$(gender%(i))
                        PRINT "Hourly Salary: $"; Payrate(i)
                        PRINT
      
                END IF
        NEXT i

IF UCASE$(person$) = "Y" THEN
      
        IF Last$ <> "" THEN
      
                PRINT "Name: "; Last$;
                PRINT " Address: "; Add$
                PRINT "ID: "; Idnum
                PRINT "Gender: "; gender.type$(Gend%)
                PRINT "Hourly Salary: $"; Rate

        END IF

END IF

'----------------------------------------------------------------------------'

SUB AddPerson (Last$, First$, Address$, gender%, Payrate)


IF person$ = "Y" THEN

        Nme$(12) = Last$
        Address$(12) = Address$
        Id(12) = Id
        gender%(12) = gender%
        Payrate(12) = Payrate
      
        OPEN "Info.DAT" FOR APPEND AS #1
              
                WRITE #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
      
        CLOSE #1

END IF


END SUB

SUB File


OPEN "Info.DAT" FOR INPUT AS #1

        FOR i = 1 TO 12
              
                IF Nme$(i) <> "" THEN
              
                        INPUT #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
                      
                END IF
      
        NEXT i

CLOSE #1


END SUB

SUB Information (Last$, Add$, Idnum, Gend%, Rate)


Nme$(1) = "Preston, Drew"
Nme$(2) = "Peterman, Alex"
Nme$(3) = "Booth, Shane"
Nme$(4) = "Young, Chris"
Nme$(5) = "Carreer, Jim"
Nme$(6) = "Cardwell, James"
Nme$(7) = "Monroe, Judy"
Nme$(8) = "Belcher, Jacob"
Nme$(9) = "Eisenback, Josh"
Nme$(10) = "Allen, Rachel"
Nme$(11) = "Yancey, Crystal"
Nme$(12) = Last$

Address$(1) = "9115 Benson Road, Bagdad, KY 40003"
Address$(2) = "0204 Adair Avenue, Shelbyville, KY 40065"
Address$(3) = "0805 Lakeview Drive, Shelbyville, KY 40065"
Address$(4) = "0555 Alter Avenue, Hollywood, CA 40219"
Address$(5) = "0710 Hallow Lane, Newport, KY 49281"
Address$(6) = "0177 Ivy Lane, Evansville, IN 33363"
Address$(7) = "0106 Ada Avenue, Shelbyville, KY 40065"
Address$(8) = "3228 Bluegrass Drive, Shelbyville, KY 40065"
Address$(9) = "0901 Heavens Way, Shelbyville, KY 40065"
Address$(10) = "0141 Fifth Street, Apolo Beach, FL 90210"
Address$(11) = "0740 Zero Lane, Shelbyville, KY 40065"
Address$(12) = Add$

Id(1) = 813176
Id(2) = 437751
Id(3) = 314207
Id(4) = 132694
Id(5) = 421738
Id(6) = 324963
Id(7) = 927691
Id(8) = 393722
Id(9) = 842101
Id(10) = 870192
Id(11) = 746669
Id(12) = Idnum

gender%(1) = 0
gender%(2) = 0
gender%(3) = 0
gender%(4) = 0
gender%(5) = 0
gender%(6) = 0
gender%(7) = 1
gender%(8) = 0
gender%(9) = 0
gender%(10) = 1
gender%(11) = 1
gender%(12) = Gend%

Payrate(1) = 6.3
Payrate(2) = 8.13
Payrate(3) = 8
Payrate(4) = 9.5
Payrate(5) = 7.35
Payrate(6) = 8.2
Payrate(7) = 6.95
Payrate(8) = 7.46
Payrate(9) = 6.25
Payrate(10) = 8.51
Payrate(11) = 6
Payrate(12) = Rate


'-----------------------------Info Sorting-----------------------------------'

Sort

'-----------------------------File Opening-----------------------------------'
OPEN "Info.DAT" FOR OUTPUT AS #1

  FOR i = 1 TO 12

    
    IF Nme$(i) <> "" THEN
  
        WRITE #1, Nme$(i), Address$(i), Id(i), gender%(i), Payrate(i)
  
    END IF
        

  NEXT i

CLOSE #1


END SUB

SUB Sort

SHARED SortItems


SELECT CASE SortItems

CASE 1

        FOR i = 1 TO 12
        FOR j = 1 TO i

                IF Nme$(j) > Nme$(i) THEN
                      
                        SWAP Nme$(j), Nme$(i)
                        SWAP Address$(j), Address$(i)
                        SWAP Id(j), Id(i)
                        SWAP gender%(j), gender%(i)
                        SWAP Payrate(j), Payrate(i)
              
                END IF

        NEXT j, i


CASE 2

        FOR i = 1 TO 12
        FOR j = 1 TO i

                IF Address$(j) > Address$(i) THEN

                        SWAP Nme$(j), Nme$(i)
                        SWAP Address$(j), Address$(i)
                        SWAP Id(j), Id(i)
                        SWAP gender%(j), gender%(i)
                        SWAP Payrate(j), Payrate(i)

                END IF

        NEXT j, i


CASE 3

        FOR i = 1 TO 12
        FOR j = 1 TO i

                IF Id(j) > Id(i) THEN

                        SWAP Nme$(j), Nme$(i)
                        SWAP Address$(j), Address$(i)
                        SWAP Id(j), Id(i)
                        SWAP gender%(j), gender%(i)
                        SWAP Payrate(j), Payrate(i)

                END IF

        NEXT j, i


CASE 4

        FOR i = 1 TO 12
        FOR j = 1 TO i

                IF gender%(j) > gender%(i) THEN

                        SWAP Nme$(j), Nme$(i)
                        SWAP Address$(j), Address$(i)
                        SWAP Id(j), Id(i)
                        SWAP gender%(j), gender%(i)
                        SWAP Payrate(j), Payrate(i)

                END IF

        NEXT j, i


CASE 5

        FOR i = 1 TO 12
        FOR j = 1 TO i

                IF Payrate(j) > Payrate(i) THEN

                        SWAP Nme$(j), Nme$(i)
                        SWAP Address$(j), Address$(i)
                        SWAP Id(j), Id(i)
                        SWAP gender%(j), gender%(i)
                        SWAP Payrate(j), Payrate(i)

                END IF

        NEXT j, i

END SELECT


END SUB

P.S. You BETTER go thru and figure out what changes I made and pm em to me,or else ima never gonna help you again Tongue [/no malice]

i just want you to learn