|
Post by writer45 on Mar 28, 2008 3:51:26 GMT
I Have started to write a qbasic program which will generate 6 different random numbers. I also want the program to printout subsequent 6 number selections and give a message indicating how many of the six numbers match my selection of 6. I cannot however, get the code right to make program check to ensure no numbers are the same in each set of 6. Please help. I threw my Basic text away 2 years ago Here's the code I have so far to generate the six random numbers: Screen 12 CLS for c = 1 to 6 n = 1 +INT(RND*49) print n next c (As you can see by output I only get one set of 6 numbers and in my output, the 5th and 6th numbers are the same (15)) I would appreciate all suggestions for code for my program. Thanks
|
|
|
Post by Mikrondel on Mar 28, 2008 7:51:12 GMT
I shall very briefly explain arrays. If you need more, search the internet for tutorials.
Arrays basically let you distinguish between a group of variables by using a number, not a name. (Importantly, this lets you use another variable to specify which variable you want to refer to.)
DIM ArrayName(1 TO 49)
will create an array called "ArrayName" with elements numbered from 1 to 50. Replace ArrayName, 1 and 49 with whatever you choose.
To access the elements of the array, you would use ArrayName(number)
E.g. PRINT ArrayName(3) will display the contents of the 3rd element of the array.
To put a random number in each element, you can do this:
FOR I = 1 TO 49 ArrayName(I) = INT(RND * 49) + 1 NEXT
Then to display them all you can use
FOR I = 1 TO 49 PRINT ArrayName(I) NEXT
This can be modified a little so that repeated numbers are avoided.
Arrays will also be helpful with the other things you want to do.
See how you go; if you need any more help then let me know.
|
|
|
Post by writer45 on Mar 29, 2008 14:16:05 GMT
WELL I TRIED BUT CANNOT GET IT TO DETERMINE THAT IF NUMBERS ARE IDENTICAL THEN PICK ANOTHER AND ENSURE THAT ALL 6 NUMBERS ARE OF DIFFERENT VALUE.
HERE IS MY CODE (WHICH WILL PROBABLY TURN YOUR STOMACH OR MAKE YOU LAUGH OR BOTH!) BUT I REALLY WANT TO GET BETTER AT BASIC PROGRAMMING AND WOULD APPRECIATE ANY HELP YOU OFFER..(OTHER THAN TAKING ANOTHER COMP PROGRAMMING COURSE...WHICH I TOOK ABOUT 12 YEARS AGO)
HERE IS MY CODE:
SCREEN 12 CLS DIM A(49) AS INTEGER FOR C = 1 TO 6 FOR B = 1 TO 6 N = 1 + INT(RND*49) IF N = NUM1 THEN 20 ELSE 10 NEXT B FOR G = 2 TO 5 IF NUM1 = NUM2 THEN 20 ELSE 10 NEXT G FOR C = 3 TO 6 IF NUM2 = NUM3 THEN 20 ELSE 10 NEXT C FOR D = 4 TO 6 IF NUM3 = NUM4 THEN 20 ELSE 10 NEXT D FOR E = 5 TO 6 IF NUM4 = NUM5 THEN 20 ELSE 10 NEXT E 10 PRINT N; 20 NEXT C ???
|
|
buff1
New Member
Posts: 3
|
Post by buff1 on Mar 29, 2008 19:37:52 GMT
There are some serious problems with your code. 1 you dim an array A() but never seem to use it. 2 you check num1 num2 and num3 but never set these variables to anything that i can see so they will always be zero.
3 use randomize timer at the beginning of your program to create a pseudo random number using rnd
|
|
|
Post by writer45 on Mar 30, 2008 14:02:32 GMT
Thanks a lot!
I took your suggestions and after an hour or so, got my code to actually work!!! Right now, it prints out 6 different numbers (I tested it 25 times and each time, the 6 numbers in each group came up different). This may seem like an insignificant thing to you but to me, it's opened the door for me re-learning computer programming. I still have a way to go.
THE RANDOMIZE TIMER STATEMENT MADE ALL THE DIFFERENCE IN THE WORLD AS WELL AS BEING MORE PRECISE WITH USING THE VARIABLES.
Here is the code that works and once again thank you for your help.
SCREEN 12 CLS RANDOMIZE TIMER FOR C = 1 TO 6 FOR A = 1 TO 49 N= 1+INT(RND*49) 10 PRINT N;
FOR G = 2 TO 6 IF NUM1(G) = NUM2(G) THEN 20 ELSE 10 NEXT G
FOR H = 3 TO 6 IF NUM2(H)=NUM3(H) THEN 20 ELSE 10 NEXT H
FOR I = 4 TO 6 IF NUM3(I) = NUM4(I) THEN 20 ELSE 10 NEXT I
FOR J = 5 TO 6 IF NUM4(J) = NUM5(J) THEN 20 ELSE 10 NEXT J
NEXT A NEXT C
|
|
|
Post by writer45 on Mar 30, 2008 14:38:41 GMT
I give up! (Almost)
I tested the above code futher and the output was 6 different numbers each time...for about 85 % of the time!! Then I began to get identical numbers in the set of six. For example, 30 repeated itself once, 15 repeated itself once, as did a few other numbers. Now you're probably fed up with a newbee so if you don't reply further I'll understand.
I am getting so close to understanding arrays and conditional statements....I plan on expanding this program so that it sorts the 6 numbers in ascending order, then picks 100 sets of 6 different numbers and compares them to 6 numbers pre-selected...and records how many times each pre-selected number was chosen. AND NO, I'M NOT TRYING TO WIN A LOTTERY LOL, I JUST LOVE A CHALLENGE.
I HAVE BEGUN STUDYING SOME TUTORIALS IN QBASIC BUT IT SEEMS SUGGESTIONS FROM EXPERTS LIKE YOU GUYS HAS HELPED MUCH MORE.
THANKS FOR ALL YOU'VE DONE SO FAR, I APPRECIATE IT
AL
|
|
|
Post by Mikrondel on Mar 31, 2008 6:54:07 GMT
I want to let you solve this problem yourself, so I won't give you the solution. But I'll give you solutions to some related problems to help you understand how to go from problem to solution.
Firstly, you have to be clear in your head about procedure. In mathematics, if you say x = y, you are stating a relation. x always equals y - they are related.
In programming, things are different. X = Y is an assignment. The value that Y currently holds is assigned to X. X will keep this value until you assign something else to it.
For instance, try this program - see if you can predict what will happen before running it.
X = 3 X = 4 Y = X X = 1 PRINT X, Y
Realise that this is a procedure, and things are done in a certain order, and that order is very important. Also, you are not telling the computer what you want; you're telling it what to do.
Let's briefly consider your program. Look at NUM1(G).
Do you ever assign anything to any element of NUM1? No. This means NUM1(anything) will be zero. Similarly for all the other times you use arrays.
Add two little PRINTs into your program, like this:
FOR G = 2 TO 6 PRINT "Debug: NUM1(G) ="; NUM1(G); "and NUM2(G) ="; NUM2(G) IF NUM1(G) = NUM2(G) THEN 20 ELSE 10 PRINT "Debug: You won't see this" NEXT G
The first will tell you what NUM1(G) and NUM2(G) are... now you can work out exactly what will happen on the next line.
The second PRINT will never be seen, because the program never gets there. If the condition to the IF is true, execution will go to line 20. Otherwise execution will go to line 10. There is no third option - the second PRINT will never be reached.
Using PRINT in such a manner is a great programming aid. Apply it liberally.
Now, there's not much of value in your program as it is, so you'd be best to start over. Though I'd suggest you get a bit more practice with arrays and FOR loops first.
OK, now let's start with the question of getting the user to enter 5 numbers, then displaying the highest number.
'Declare the array - don't use any array without DIMensioning it DIM Nums(1 TO 5)
FOR I = 1 TO 5 INPUT "Enter a number: ", Nums(I) NEXT
'Start with the first number Highest = Nums(1) FOR I = 2 TO 5 'If the current number is higher, make it our new highest number IF Nums(I) > Highest THEN Highest = Nums(I) NEXT
PRINT "The highest number was: "; Highest
That'll do for now. Pick it apart; follow everything, line by line, as the computer would.
Put extra PRINTs in the program to see what it's doing, fiddle with things. Tell me how you fare. I'll post another program for you later.
Good luck!
|
|
|
Post by writer45 on Apr 5, 2008 16:54:49 GMT
Very frustrating...I used your print "Debug" statements and to my delight, the PRINT statement "You won't see that" did not turn up. However. the numbers are still repeating themselves in a random manner. I will get output of 6 different numbers 8 times in a row, then the next 2 sets have a repeated and then 5 with no duplicate numbers, and so on...obviously something is not being checked but my limited knowledge of conditional statements and where in a loop to put them is limited /weak.
I will type out the code and let you see what is happening now...if you have the time to look at it.
Al
|
|
|
Post by Mikrondel on Apr 6, 2008 7:39:35 GMT
I would advise you to put your ambition aside for a little while. For now, get comfortable with arrays and loops.
Here's a bigger program for you to look at. The array use is fairly simple, but four arrays are used in parallel to store information about a set of planets. The X and Y arrays store the position of each planet while the XV and YV arrays store their velocities in the X and Y directions.
For instance, planet 3's position is given by X(3) and Y(3), while its velocity is given by XV(3) and YV(3).
Try to work out exactly why it is written as it is. Carefully analyse each loop; where there is one loop within another make sure you understand how they come together.
SCREEN 12
NumPlanets = 20
DIM X(1 TO NumPlanets) DIM XV(1 TO NumPlanets) DIM Y(1 TO NumPlanets) DIM YV(1 TO NumPlanets)
RANDOMIZE TIMER
'Generate initial planet states FOR I = 1 TO NumPlanets X(I) = INT(RND * 640) Y(I) = INT(RND * 640) XV(I) = RND * 2 - 1 YV(I) = RND * 2 - 1 NEXT
'Star LINE (319, 239)-STEP(1, 1), 14, BF
DO Et! = TIMER + .05
FOR I = 1 TO NumPlanets
'Grey tail PSET (X(I), Y(I)), 8
'Add velocity to position X(I) = X(I) + XV(I) Y(I) = Y(I) + YV(I)
'Adjust velocities (according to Newton's Law of Gravitation) 'Don't worry about the details of the mathematics DX = X(I) - 319.5 DY = Y(I) - 239.5 D = (DX ^ 2 + DY ^ 2) ^ 1.5 XV(I) = XV(I) - 100 * DX / D YV(I) = YV(I) - 100 * DY / D
'Draw planet PSET (X(I), Y(I)), 15
NEXT
'Delay to keep things at reasonable speed DO: LOOP WHILE TIMER < Et!
'Break out if user presses key. LOOP UNTIL INKEY$ <> ""
Try modifying the program so that the planets do not leave any trails. (Hint: colour 0 is black.)
Also, modify the program so that when the user presses a key, the planet that is nearest the star is highlighted.
If you need help, let me know.
|
|
|
Post by Daye on Mar 15, 2017 7:25:33 GMT
Write a qbasic program to generate 30 random integer numbers distributed between 20 and 50.
|
|
|
Post by drey on Oct 4, 2017 12:46:36 GMT
PLEASE HELP; Write a QBASIC program to generate thirty random integer numbers distributed between 20 and 50. Your program should ensure that no number is repeated.
|
|
|
Post by Kelserm on Sept 30, 2019 7:08:30 GMT
Keflex And Holy Basil <a href=http://cthosts.net>buy generic cialis online</a> Greenline Pharmacy Order Progesterone Menopause Medicine
|
|
|
Post by Dennisflara on Nov 26, 2019 17:18:42 GMT
|
|
|
Post by Dennisflara on Nov 27, 2019 13:55:36 GMT
|
|
|
Post by Kelserm on Jan 14, 2020 10:33:14 GMT
Prix Du Propecia Au Canada <a href=http://cialibuy.com>cialis</a> Prix Cialis Generique Pharmacie Get Cialis From Canada Hydrochlorothiazide Mastercard Accepted By Money Order
|
|
|
Post by Kelserm on May 26, 2020 7:07:05 GMT
Propecia Sperm Analysis purchase cialis online Viagra Italia <a href=https://abcialisnews.com/#>safe place to buy cialis online</a> Discount Cipro
|
|
|
Post by Katieweele on Aug 7, 2020 11:48:33 GMT
We are presently taking on new product reviewers at qbasic.proboards.com to review our new sexy lingerie <a href=https://peachesandscreams.co.uk/>Wedding Stockings pieces such as Nude and White Floral Applique High Neck Chemise</a>. If you are interested, <a href=https://www.thefashionspot.com/buzz-news/latest-news/179579-vancouver-has-it-all-fashion-week-lineup-and-christian-dior-exclusive/>get in touch</a>
|
|
|
Post by Katieweele on Aug 11, 2020 13:08:04 GMT
We are presently searching for product reviewers at qbasic.proboards.com to review our latest sexy hosiery <a href=https://peachesandscreams.co.uk/>Fishnet Tights and Suspender Tights pieces such as Open Cup Purple Mesh and Lace Babydoll Set</a>:-) If this sounds like you, <a href=http://brooklynsextherapy.com>get in touch</a>
|
|