10 RANDOMIZE TIMER
20 FOR X=1 TO 5 {SELECTS RANDOM NUMBERS}
30 L(X)=INT(50*RND(1)+1)
40 NEXT X
50 REM
60 REM
70 I=I-1
80 S=O
90 FOR X=1 TO I-1
100 IF L(X)<=(X+1)THEN 150
110 S1=L(X)
120 L(X)=L(X+1) {TO ENSURE NO NUMBERS ARE REPEATED}
130 L(X+1)=S1
140 S=1
150 NEXT X
160 IF S=1 THEN 70
170 FOR Y=1 TO 50
180 REM {TIME DELAY}
190 NEXT Y
200 REM
210 S=O
220 FOR I=1 TO 4
230 IF L(I)<=L(I+1) THEN 290
240 K=L(I)
250 L(I)=L(I+1)
260 L(I+1)=K
270 S=1 {PUTS NUMBERS IN ORDER}
280 REM
290 NEXT I
300 REM
310 IF S=1 THEN 210
320 REM
330 REM
340 FOR X=1 TO 5
350 PRINT L(X); {PRINTS FIVE NUMBERS}
360 NEXT X
I NOW WANT A SUBROUTINE TO ADD TOGETHER THE FIVE NUMBERS
THAT IT PRINTS.
Can anyone help me finish my program
-
- Newbie
- Posts: 2
- Joined: Mon Feb 25, 2008 6:06 pm
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
That code actually works in QB?
I see quite a few coding errors including your code comments. Look at GOTO and perhaps get in the habit of dimensioning your arrays. QB arrays can only be undimensioned to 10. Comments need ' or REM.
But to answer your question, use the FOR loop where you PRINT to add the numbers together using a sum variable.
Sum = Sum + L(X)
If you are getting errors or other code problems, please list them when posting next time. Also Line numbers or names are not required in QB unless you are using GOTO or GOSUB.
Ted
But to answer your question, use the FOR loop where you PRINT to add the numbers together using a sum variable.
Sum = Sum + L(X)
If you are getting errors or other code problems, please list them when posting next time. Also Line numbers or names are not required in QB unless you are using GOTO or GOSUB.
Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
And to show some debugging techniques
Some debugging techniques
Run the program below.
It is designed to create 3 numbers rather than 5
It is designed for a max of 4
Why? Because the likelyhood of getting a duplicate
is so low for 5 numbers in the range 0-49 that you
can't test that case. This means someday your program
will go down a path that was not tested. BAD. With the
new values, it is surely tested.
Run it like this:
Either compile and run the EXE, or from the DOS prompt
START QBASIC / RUN Z
(where you have Z.BAS in NOTEPAD (ASCII) format.
Keep pressing ENTER until you are satisfied. Voil?,
your program is debugged. You can play around with
other values to see what happens for bigger values.
Change the CONSTs for 20 numbers in the range < 22.
To stop the program, don't just press Enter, but
instead enter something such as "q".
Anyway, to meet your original specification you just
have to change the CONSTs to
CONST maxCNT = 5
CONST maxN = 50
and be confident that if 32 were generated twice by
RND, no problem - your program will handle it.
Mac
Run the program below.
It is designed to create 3 numbers rather than 5
It is designed for a max of 4
Why? Because the likelyhood of getting a duplicate
is so low for 5 numbers in the range 0-49 that you
can't test that case. This means someday your program
will go down a path that was not tested. BAD. With the
new values, it is surely tested.
Run it like this:
Either compile and run the EXE, or from the DOS prompt
START QBASIC / RUN Z
(where you have Z.BAS in NOTEPAD (ASCII) format.
Keep pressing ENTER until you are satisfied. Voil?,
your program is debugged. You can play around with
other values to see what happens for bigger values.
Change the CONSTs for 20 numbers in the range < 22.
To stop the program, don't just press Enter, but
instead enter something such as "q".
Anyway, to meet your original specification you just
have to change the CONSTs to
CONST maxCNT = 5
CONST maxN = 50
and be confident that if 32 were generated twice by
RND, no problem - your program will handle it.
Mac
Code: Select all
CLS
RANDOMIZE TIMER
CONST maxCNT = 3: ' Don't hard code 5's
CONST maxN = 4: ' Don't hard code 50
DIM N AS INTEGER ' A candidate integer
DIM l(maxCNT) AS INTEGER ' Accepted candidates
DIM cnt AS INTEGER ' Count of accepted candidates
DIM dup AS INTEGER ' 0=No -1=Yes
DIM i AS INTEGER, j AS INTEGER ' Work variables
' ### Generate the numbers
DO
dup = 0
N = INT(RND * maxN)
FOR i = 1 TO cnt
IF N = l(i) THEN dup = -1: EXIT FOR
NEXT i
IF NOT dup THEN cnt = cnt + 1: l(i) = N
LOOP WHILE cnt < maxCNT
' ### Sort them
FOR i = 1 TO maxCNT - 1
FOR j = i TO maxCNT
IF l(i) > l(j) THEN SWAP l(i), l(j)
NEXT j
NEXT i
' ### Compute total while printing entries
DIM Total AS INTEGER
FOR i = 1 TO maxCNT
PRINT l(i);
Total = Total + l(i)
NEXT i
PRINT : PRINT : PRINT : PRINT "Total is"; Total
' Exit the program
LOCATE 25, 5
LINE INPUT "Enter nothing to run again, something like 'q' to exit: "; e$
IF e$ = "" THEN RUN
CLS
SYSTEM