## Qbasic program(need 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

Drazen
Newbie
Posts: 5
Joined: Mon Sep 20, 2010 1:44 pm

### Qbasic program(need help)

Hi all!

I need to make this program and I don't know how,can you do it and explain please,I know for someone will seem siple but I'm begginer...

- Generate 20 random numbers(from 10-99)
- show odd and even numbers
- recommit odd and recommit even numbers
- show sum of odd and even number
- show then in file

burger2227
Veteran
Posts: 2418
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
Sounds like homework to me!

If you need help, post the code you have tried so far. We don't help people who don't help themselves.

Ted
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

Drazen
Newbie
Posts: 5
Joined: Mon Sep 20, 2010 1:44 pm
CLS
FOR i = 1 TO 10
RANDOMIZE TIMER
a(i) = 10 + INT(RND * 89)
PRINT a(i)
Next i
FOR i = 1 TO 10
IF a(i) MOD 2=0 THEN
PRINT a(i); "broj je paran"
p(i) = a(i)
ELSE
PRINT a(i); "je neparan"
n(i) = a(i)
END IF
NEXT i
OPEN "vjezba.dat" FOR OUTPUT AS #1
FOR i = 1 TO 10
WRITE #1, p(i), n(i)
NEXT i
CLOSE #1

PRINT
PRINT "brojevi"
PRINT "parni:", "neparni:"
OPEN "vjezba.dat" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, p, n
PRINT p, n
LOOP
CLOSE #1
END

I can't figure out 2 problems :

1. Can't figure how to set to generate 20 number,always says 'out of range'

2. I don't wanna (0) to show up when I start program...

Btw, I'Croatian, so 'Paran'=EVEN and 'Neparan'=Odd

commandvom
Coder
Posts: 10
Joined: Mon Sep 06, 2010 8:41 am

### prg

dim odd(20) as integer, even(20) as integer, i as integer, aux as integer
dim totalodd as integer, totaleven as integer, k as integer, j as integer

randomize timer
j=1
k=1

for i=1 to 20
aux=10 + INT(RND * 89)
if (aux mod 2)=0 then
even(j)=aux
totaleven=totaleven+aux
j=j+1
else
odd(k)=aux
totalodd=totalodd+aux
k=k+1
end if
next i

print "odd even"
for i=1 to 20
print odd(i), even(i)
next i
print
print totalodd, totaleven

I am from argentina, speak spanish, some english. I no test this but i think is ok!
Last edited by commandvom on Tue Sep 21, 2010 7:54 am, edited 1 time in total.

burger2227
Veteran
Posts: 2418
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
You have to dimension arrays using DIM Array(10).

You can get away with not doing that with smaller arrays, but I DIM all arrays.

Ted
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

Drazen
Newbie
Posts: 5
Joined: Mon Sep 20, 2010 1:44 pm

### Re: prg

commandvom wrote:dim odd(20) as integer, even(20) as integer, i as integer, aux as integer
dim totalodd as integer, totaleven as integer, k as integer, j as integer

randomize timer
j=1
k=1

for i=1 to 20
aux=10 + INT(RND * 89)
if (aux mod 2)=0 then
odd(k)=aux
totalodd=totalodd+aux
k=k+1
else
even(j)=aux
totaleven=totaleven+aux
j=j+1
end if
next i

print "odd even"
for i=1 to 20
print odd(i), even(i)
next i
print
print totalodd, totaleven

I am from argentina, speak spanish, some english. I no test this but i think is ok!
'Subscript out of range' ??

Didn't understand you Ted well,as I said I'm from Croatia so...

commandvom
Coder
Posts: 10
Joined: Mon Sep 06, 2010 8:41 am

### sorry

sorry, i confuse means of odd-even!

Drazen
Newbie
Posts: 5
Joined: Mon Sep 20, 2010 1:44 pm

### Re: sorry

commandvom wrote:sorry, i confuse means of odd-even!
I google translated and it said odd & even, but I will try to help you understand :

ODD are numbers like : 1,3,5,7,9,11...

EVEN are numbers like : 2,4,6,8,10,12...

burger2227
Veteran
Posts: 2418
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
NEED 20 numbers in array if ALL ODD or ALL EVEN. Could happen.

number = INT(RND * 90) + 10 'INT never returns 90, only 89 most = 90 * .9999999

'other code OK

Works OK, what else you need? NOT 0?

FOR i = 1 TO 20
IF odd(i) <> 0 THEN WRITE #1, odd(i)
IF even(i) <> 0 THEN WRITE #1, even(i)
NEXT
CLOSE #1

What else? Hello Croatia!
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

Drazen
Newbie
Posts: 5
Joined: Mon Sep 20, 2010 1:44 pm
burger2227 wrote:NEED 20 numbers in array if ALL ODD or ALL EVEN. Could happen.

number = INT(RND * 90) + 10 'INT never returns 90, only 89 most = 90 * .9999999

'other code OK

Works OK, what else you need? NOT 0?

FOR i = 1 TO 20
IF odd(i) <> 0 THEN WRITE #1, odd(i)
IF even(i) <> 0 THEN WRITE #1, even(i)
NEXT
CLOSE #1

What else? Hello Croatia!
What else?
else is that now I have all zeros(0) in my result, here's the code :

CLS
DIM a(20)
FOR i = 1 TO 20
RANDOMIZE TIMER
a(i) = INT(RND * 90)+10
PRINT a(i)
Next i

OPEN "vjezba.dat" FOR OUTPUT AS #1
FOR i= 1 to 20
IF p <> 0 THEN WRITE #1, p
IF n <> 0 THEN WRITE #1, n
NEXT i
CLOSE #1

FOR i = 1 TO 20
IF a(i) MOD 2=0 THEN
PRINT a(i); "broj je paran"
ELSE
PRINT a(i); "je neparan"
END IF
NEXT i
OPEN "vjezba.dat" FOR OUTPUT AS #1
FOR i = 1 TO 20
WRITE #1, p, n
NEXT i
CLOSE #1

PRINT
PRINT "brojevi"
PRINT "parni:", "neparni:"
OPEN "vjezba.dat" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, p, n
PRINT p, n
LOOP
CLOSE #1
END

burger2227
Veteran
Posts: 2418
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
USE Array a(i) values. That is where you put the numbers!
p and n = 0 always until you read file

Code: Select all

``````CLS
DIM a(20)
FOR i = 1 TO 20
RANDOMIZE TIMER
a(i) = INT(RND * 90)+10
PRINT a(i)
Next i

OPEN "vjezba.dat" FOR OUTPUT AS #1
FOR i= 1 to 20
WRITE #1, a(i)
NEXT i
CLOSE #1

FOR i = 1 TO 20
IF a(i) MOD 2=0 THEN
PRINT a(i); "broj je paran"
ELSE
PRINT a(i); "je neparan"
END IF
NEXT i

``````