|
Post by J on Apr 22, 2004 1:45:55 GMT
Write a menu-driven QBASIC program to dimension an array with the statement DIM state$(1 TO 50) and maintain a list of certain continental states in the United States. The list of the states should always be in alphabetical order (sort!) and occupy consecutive elements of the array. The option for this menu-driven program should be as follows:
1. Request the user to input a state, and insert it in the proper position in the array. (If the state is already in the array, so report.)
2. Request the user to input a state, and delete it from the array. (If the state is not in the array, so report.)
3. Display the states in the array.
4. Quit.
[Output varies depending on the content of array state$(1 TO 50)]
Test data: Delaware, Kentucky, Washington, Arizona, Massachusetts, Illinois, California, Utah, Colorado, Michigan, Florida, Nevada, Wisconsin, Nebraska, Ohio, Utah, Texas, Georgia, Oregon, Missouri¡K¡K..
i don't know how to get start can you give me some hint? thanx
|
|
|
Post by buffoasys on Apr 22, 2004 3:53:07 GMT
Print the list of options on the screen.
Input an option (let the user input an option).
go to the proper routine you write to do the option requested.
Also have an option to exit or Quit. and have it go to a Stop or End or system statement.
Use the Help. It should have some examples of some of the things.
If you don't understand arrays, they are basically tables. dim MyArray$(5) a string array for 5 elements MyArray$(1)="This is one" MyArray$(2)="This is two" print Myarray$(1) will print "This is one" on the screen
BTW, QuickBasic 4.5 includes a whole group (actuall to subdirectories) of example programs
|
|
|
Post by J on Apr 22, 2004 6:26:15 GMT
2. Request the user to input a state, and delete it from the array. (If the state is not in the array, so report.)
3. Display the states in the array. HOW TO WRITE THESE STEPS
|
|
|
Post by buffoasys on Apr 22, 2004 7:14:11 GMT
We don't do homework.
We'll help with code you write but will not write it for you.
Study sample program. there's lots around
|
|
|
Post by j on Apr 23, 2004 4:02:58 GMT
DIM state$(50) DIM state2$(50) DO CLS PRINT "Choose an option" PRINT "1) Add a state" PRINT "2) Remove a state" PRINT "3) Display States" PRINT "4) Quit" PRINT "" INPUT choice
IF choice = 1 THEN PRINT "Enter a state:" INPUT newstate$ pos = 1 in = 0 double = 0 FOR n = 1 TO 50 IF state$(n) > newstate and in = 0 THEN IF state$(n) = newstate THEN double = 1 state2$(pos) = newstate$ state2$(pos + 1) = state$(n) pos = pos + 2 in = 1 ELSE state2$(pos) = state$(n) pos = pos + 1 ENDIF NEXT n IF double <> 1 THEN FOR n = 1 TO 50 state$(n) = state$(n) NEXT n ELSE PRINT "State is already there!" ENDIF ENDIF
IF choice = 2 THEN PRINT "Enter a state:" INPUT newstate$ found = 0 pos = 1 FOR n = 1 TO 50 IF state$(n) <> newstate THEN state2$(pos) = state$(n) pos = pos + 1 found = 1 ENDIF NEXT n IF found = 0 THEN PRINT "State not found!" ENDIF
IF choice = 3 THEN FOR n = 1 TO 50 STEP 5 PRINT state$(n); state$(n+1); state$(n+2); state$(n+3); state$(n+4) NEXT n ENDIF
If choice = 4 THEN continue = 1
LOOP WHILE continue = 0 PRINT "" PRINT "Goodbye!"
I may have a few errors here and there and how to use sort for alphabetical order.... thanx
|
|
|
Post by buffoasys on Apr 23, 2004 22:39:33 GMT
You've got a good start.
Here's a sort that should work though not tested There are others. QuikSort is the fastest but for small sorts like this the time difference won't be significant.
SORTSTATES: Last% = 50 FOR k& = 1 TO 50 IF State$(k&) = "" THEN Last% = k& - 1 NEXT k& spread = Last% \ 2 'UBOUND(Array$) \ 2 DO WHILE spread > 0 FOR i = spread TO Last% - 1'UBOUND(Array$) - 1 FOR j = (i - spread + 1) TO 1 STEP -spread IF UCASE$(State$(j)) <= UCASE$(State$(j + spread)) THEN EXIT FOR ELSE ' swap if out of order SWAP State$(j), State$(j + spread) 'SWAP Array2%(j), Array2%(j + spread) 'SWAP Array3$(j), Array3$(j + spread) END IF NEXT j NEXT i spread = spread \ 2 LOOP RETURN
|
|
|
Post by buffoasys on Apr 23, 2004 22:40:40 GMT
This should show it better SORTSTATES: Last% = 50 FOR k& = 1 TO 50 IF State$(k&) = "" THEN Last% = k& - 1 NEXT k& spread = Last% \ 2 'UBOUND(Array$) \ 2 DO WHILE spread > 0 FOR i = spread TO Last% - 1'UBOUND(Array$) - 1 FOR j = (i - spread + 1) TO 1 STEP -spread IF UCASE$(State$(j)) <= UCASE$(State$(j + spread)) THEN EXIT FOR ELSE ' swap if out of order SWAP State$(j), State$(j + spread) 'SWAP Array2%(j), Array2%(j + spread) 'SWAP Array3$(j), Array3$(j + spread) END IF NEXT j NEXT i spread = spread \ 2 LOOP RETURN
|
|
|
Post by j on Apr 24, 2004 6:37:36 GMT
can you check my code? i still can't run the program right, some parts are wierd ,i can't figure it out , plz help me ,thanks
|
|
|
Post by buffoasys on Apr 24, 2004 8:56:28 GMT
For one thing, you are using some variable names that are reserved words.
POS was the first to get my attention. So you might use Position% instead
IN might cause some trouble as a variable name too
Also Double. Use doubled% instead
|
|
|
Post by j on Apr 25, 2004 1:38:13 GMT
hi,can you help me to rewrite my program? and i got out of range at "state2$(pos) = state$(n)" i am having difficult of this program ,plz help
|
|
|
Post by buffoasys on Apr 25, 2004 4:08:49 GMT
You'll still need to work on the display I left that to u but it works
DIM STATE$(50) DIM state2$(50) DO CLS PRINT "Choose an option" PRINT "1) Add a state" PRINT "2) Remove a state" PRINT "3) Display States" PRINT "4) Quit" PRINT "" INPUT choice
IF choice = 1 THEN PRINT "Enter a state:" INPUT NewState$ position% = 1 in = 0 doubled% = 0 found% = 0 FOR n = 1 TO 50 IF UCASE$(STATE$(n)) = UCASE$(NewState$) THEN ' > newstate$ AND in = 0 THEN found% = 1 LOCATE 20, 1: PRINT "State is already there!": SLEEP 2: LOCATE 20, 1: PRINT SPACE$(80); EXIT FOR END IF NEXT n IF found% THEN GOTO NextOption FOR n = 1 TO 50 IF STATE$(n) = "" THEN STATE$(n) = NewState$: n = 50 NEXT n 'GOTO NextOption ' IF State$(n) = newstate$ THEN doubled% = 1 ' state2$(position%) = newstate$ ' state2$(position% + 1) = State$(n) ' position% = position% + 2 ' in = 1 ' ELSE ' state2$(position%) = State$(n) ' position% = position% + 1 ' END IF ' NEXT n ' IF doubled% <> 1 THEN ' FOR n = 1 TO 50 ' State$(n) = State$(n) ' NEXT n ' ELSE ' PRINT "State is already there!" ' END IF END IF
IF choice = 2 THEN PRINT "Enter a state:" INPUT NewState$ found = 0 position% = 1 FOR n = 1 TO 50 IF UCASE$(STATE$(n)) = UCASE$(NewState$) THEN 'state2$(position%) = State$(n) 'position% = position% + 1 STATE$(n) = "": n = 50 found = 1 END IF NEXT n IF found = 1 THEN PRINT "State Deleted" 'already added! " 'not found!" SLEEP 1 END IF
IF choice = 3 THEN GOSUB SORTSTATES FOR n = 1 TO 50 STEP 5 PRINT STATE$(n); STATE$(n + 1); STATE$(n + 2); STATE$(n + 3); STATE$(n + 4) NEXT n WHILE INKEY$ = "": WEND'to keep screen from being cleared until a key is pressed END IF
IF choice = 4 THEN continue = 1 NextOption: LOOP WHILE continue = 0 PRINT "" PRINT "Goodbye!" GOTO ENDIT SORTSTATES: Last% = 50 FOR k& = 1 TO 50 IF STATE$(k&) = "" THEN Last% = k& - 1: k& = 50 NEXT k& spread = Last% \ 2 'UBOUND(Array$) \ 2 DO WHILE spread > 0 FOR i = spread TO Last% - 1'UBOUND(Array$) - 1 FOR j = (i - spread + 1) TO 1 STEP -spread IF UCASE$(STATE$(j)) <= UCASE$(STATE$(j + spread)) THEN EXIT FOR ELSE ' swap if out of order SWAP STATE$(j), STATE$(j + spread) 'SWAP Array2%(j), Array2%(j + spread) 'SWAP Array3$(j), Array3$(j + spread) END IF NEXT j NEXT i spread = spread \ 2 LOOP RETURN ENDIT: 'I may have a few errors here and there and how to use sort for alphabetical order.... thanx
|
|
|
Post by buffoasys on Apr 25, 2004 4:10:15 GMT
code tag apparently doesnt indent so will use pre tag DIM STATE$(50) DIM state2$(50) DO CLS PRINT "Choose an option" PRINT "1) Add a state" PRINT "2) Remove a state" PRINT "3) Display States" PRINT "4) Quit" PRINT "" INPUT choice
IF choice = 1 THEN PRINT "Enter a state:" INPUT NewState$ position% = 1 in = 0 doubled% = 0 found% = 0 FOR n = 1 TO 50 IF UCASE$(STATE$(n)) = UCASE$(NewState$) THEN ' > newstate$ AND in = 0 THEN found% = 1 LOCATE 20, 1: PRINT "State is already there!": SLEEP 2: LOCATE 20, 1: PRINT SPACE$(80); EXIT FOR END IF NEXT n IF found% THEN GOTO NextOption FOR n = 1 TO 50 IF STATE$(n) = "" THEN STATE$(n) = NewState$: n = 50 NEXT n 'GOTO NextOption ' IF State$(n) = newstate$ THEN doubled% = 1 ' state2$(position%) = newstate$ ' state2$(position% + 1) = State$(n) ' position% = position% + 2 ' in = 1 ' ELSE ' state2$(position%) = State$(n) ' position% = position% + 1 ' END IF ' NEXT n ' IF doubled% <> 1 THEN ' FOR n = 1 TO 50 ' State$(n) = State$(n) ' NEXT n ' ELSE ' PRINT "State is already there!" ' END IF END IF
IF choice = 2 THEN PRINT "Enter a state:" INPUT NewState$ found = 0 position% = 1 FOR n = 1 TO 50 IF UCASE$(STATE$(n)) = UCASE$(NewState$) THEN 'state2$(position%) = State$(n) 'position% = position% + 1 STATE$(n) = "": n = 50 found = 1 END IF NEXT n IF found = 1 THEN PRINT "State Deleted" 'already added! " 'not found!" SLEEP 1 END IF
IF choice = 3 THEN GOSUB SORTSTATES FOR n = 1 TO 50 STEP 5 PRINT STATE$(n); STATE$(n + 1); STATE$(n + 2); STATE$(n + 3); STATE$(n + 4) NEXT n WHILE INKEY$ = "": WEND'to keep screen from being cleared until a key is pressed END IF
IF choice = 4 THEN continue = 1 NextOption: LOOP WHILE continue = 0 PRINT "" PRINT "Goodbye!" GOTO ENDIT SORTSTATES: Last% = 50 FOR k& = 1 TO 50 IF STATE$(k&) = "" THEN Last% = k& - 1: k& = 50 NEXT k& spread = Last% \ 2 'UBOUND(Array$) \ 2 DO WHILE spread > 0 FOR i = spread TO Last% - 1'UBOUND(Array$) - 1 FOR j = (i - spread + 1) TO 1 STEP -spread IF UCASE$(STATE$(j)) <= UCASE$(STATE$(j + spread)) THEN EXIT FOR ELSE ' swap if out of order SWAP STATE$(j), STATE$(j + spread) 'SWAP Array2%(j), Array2%(j + spread) 'SWAP Array3$(j), Array3$(j + spread) END IF NEXT j NEXT i spread = spread \ 2 LOOP RETURN ENDIT: 'I may have a few errors here and there and how to use sort for alphabetical order.... thanx
|
|
|
Post by j on Apr 25, 2004 5:45:36 GMT
when i enter the choice why the output stuck and can't back to the program?
|
|
|
Post by j on Apr 25, 2004 5:57:26 GMT
hi, when i type the state in, it doesnt do anything??? what's wrong???
|
|
|
Post by buffoasys on Apr 26, 2004 0:20:07 GMT
I don't know. Works fine here
What's your email address. I'll email it to you
|
|
|
Post by j on Apr 26, 2004 0:28:16 GMT
my email is : oniguei@hotmail.com thanx
|
|
|
Post by h on Apr 26, 2004 15:15:40 GMT
hi, i havent got your email
|
|
|
Post by ihmgcf on Jun 29, 2019 13:07:42 GMT
comprare levitra originale on line levitranew.com <a href="http://levitranew.com/">generic levitra 20 mg</a>
|
|