Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a question for my programming class. Question Below. Cant figure out how to do it. My teacher said to use an array but I don't know how.


Write a program which picks 20 random numbers between 10 and 99 inclusive. The numbers are then printed out with all the evens on one line and all odds on the other line. The output should look like:

ODD: 49 25 21 33 63 77 29 77 27 43
EVEN: 66 56 18 24 28 70 56 62 48 76

So this is what my program currently looks like.

RANDOMIZE TIMER

FOR x = 1 TO 20
z = INT(RND * 89) + 10
IF z MOD 2 = 0 GOTO 7 ELSE GOTO 12
7 IF x = 1 THEN
PRINT "EVEN: "; z;
ELSE
PRINT z;
END IF
12 IF x = 1 THEN
PRINT "ODD: "; z;
ELSE
PRINT z;
END IF
NEXT x
END

Not sure why it isnt working. Im not really that familiar with GOTO so I'm probobly doing something wrong there.

The Output has to look exactly the same as the way the question had it. So the cpu has to print ODD on first line and EVEN on second line with their numbers to the right.

If possible I would like an array definition and solution as well as my current non array finished solution.

What I have tried:

I have attempted arrays and goto but I'm just to basic for this question.
Posted
Updated 7-Oct-16 15:31pm

I'd chuck out the GOTO's and rethink the approach .. what about

1) generating an array of 20 random numbers

VB
RANDOMIZE TIMER
' Declare an array to hold all the random numbers
Dim AllNumbers As INTEGER
' Generate 20 Random numbers in the correct range
FOR x = 1 TO 20
    z = INT(RND * 89) + 10
    AllNumbers(x) = z
NEXT


2) There are two ways you can go from here - either two subroutines, one for Odd, one for Even, or, two loops, one to print the odds, one to print the evens .. here's an example

VB
' Print the ODD's 
PRINT "ODD:";
' Loop Over the Array
FOR x = 1 to 20
    IF AllNumbers(x)  MOD 2 != 0 THEN PRINT AllNumbers(x);
NEXT
PRINT


'Even' is similar ... (I cant remember how to 'jump to a new line' in a print statement, you'll have to look that one up, I think its semi-colons or not) ... and you'll likely need spaces between the numbers, again, something you can look up
 
Share this answer
 
v2
I have not used QBasic for last 20 years or more.

Just an advice: use the debugger and you will see your code running.

Your code do not match the statement and need a complete rewrite. See how to use arrays.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900