Page 1 of 1
Searching help
Posted: Fri Oct 28, 2005 12:00 pm
by xkse
I have a problem that i can't seem to figure out. I'm a beginner, so don't harass me
i need to write a program with qbasic that generates and stores a list of 999 random integers between 1 and 999. It should ask the user what number they want to look for, and then print the number of times it appears in the list
if any one could give me some help on how to do it it would be greatly appreciated
Posted: Fri Oct 28, 2005 12:23 pm
by Patz QuickBASIC Creations
No need to worry about speed, works fast on a 400MHZ
Code: Select all
INPUT "Number to search for"; numtolookfor
LET search$ = "-":
WHILE numoftimes <> 999
LET num = INT(RND * 998) + 1
IF num = numtolookfor THEN timesfound = timesfound + 1
numoftimes = numoftimes + 1
'-----marker1
WEND
PRINT "The number"; numtolookfor; "was found"; timesfound; "times."
'-----marker2
If you want to store the variables, this is what you need to do:
Where marker 1 is, insert:
Code: Select all
LET SEARCH$ = SEARCH$ + LTRIM$(RTRIM$(STR$(NUM)))+"-"
Where marker 2 is, insert:
Code: Select all
INPUT "View list";yorn$
IF UCASE$(yorn$) = "Y" OR UCASE$(YORN$) = "YES" THEN PRINT SEARCH$
(the two marker spots are untested, but should work)
Posted: Fri Oct 28, 2005 1:23 pm
by xkse
thanks a lot man
Posted: Fri Oct 28, 2005 1:57 pm
by {Nathan}
you really shouldnt have done his homework for him... just provide psuedo code. EG a layout.
Posted: Fri Oct 28, 2005 8:00 pm
by moneo
PQBC wrote:
Code: Select all
INPUT "Number to search for"; numtolookfor
LET search$ = "-":
WHILE numoftimes <> 999
LET num = INT(RND * 998) + 1
IF num = numtolookfor THEN timesfound = timesfound + 1
numoftimes = numoftimes + 1
'-----marker1
WEND
PRINT "The number"; numtolookfor; "was found"; timesfound; "times."
[/quote]
OOPS, you forgot the RANDOMIZE TIMER.
Also, the formula for generating a random number is:
INT(RND * (Upper - Lower + 1)) + Lower
which for 1 to 999 becomes:
INT(RND * 999) + 1
*****