Searching help

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
xkse

Searching help

Post by xkse »

I have a problem that i can't seem to figure out. I'm a beginner, so don't harass me :oops:

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
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post 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)
xkse

Post by xkse »

thanks a lot man
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

you really shouldnt have done his homework for him... just provide psuedo code. EG a layout.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post 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
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Post Reply