Page 1 of 2

### odd or even

Posted: Thu Dec 01, 2005 10:55 pm
i'm not seeing a command in my qbasic book
for determining ood or even numbers and the
math eludes me...

what i want to do is generate a number
then check if its odd or even if its even
i leave it alone if its odd i add/sub 1 to make it even

berths = int(rnd * X) +A

if berths = even then goto moveon
if berths = odd then berth = (berth + or - 1)

i do need the +A as there needs to be a minimum # of berths
i have made that an even number to be safe...

Posted: Fri Dec 02, 2005 2:53 am
What you need to do is find whether the number is evenly divisible by 2.

If it is, then the number is even (as all even numbers are divisible by 2).

I don't know whether it will work in QBasic, but in Visual Basic, there is a MOD command that generates the remainder when a number is divided by another.

example: 4 MOD 2 = 0 - as there is no remainder when 4 is divided by 2.

If this works, you can then say...
IF (whatever number) MOD 2 = 0 then
That number is even
ELSE
That number is odd.

I hope this works for you.

Good Luck.

Posted: Fri Dec 02, 2005 2:58 am
I'm sorry, i've just been playing around with it and have found that MOD doesn't actually work in QBasic.

I'm sure that there is an alternative statement, and i'm sure someone will be able to help you further.

Sorry

Posted: Fri Dec 02, 2005 1:01 pm
if berths and 1 then
berth = (berth + or - 1)
else
goto moveon
end if

laters,

Nemesis

Posted: Fri Dec 02, 2005 3:30 pm
Okay to make something a bit more clear:

Code: Select all

``````IF value AND 1 = 1 THEN
' value is ODD
ELSE
'value is EVEN
END IF``````
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even.

Posted: Fri Dec 02, 2005 6:22 pm
Nekrophidius wrote:Okay to make something a bit more clear:

Code: Select all

``````IF value AND 1 = 1 THEN
' value is ODD
ELSE
'value is EVEN
END IF``````
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even.
okay i tried this but it turns my number to
a negative...

this is for TRAVELLER RPG ships... BTW

example:

Code: Select all

``````berth = int(rnd * 16) + 5  <<<minium 5 berths max 20 berths

berth = 11  <<<<example...

if 11 AND 1 = 1 then
let 11 = (11 - 1)
else
PRINT "its okay its even"
end if
``````
but this does not give me 10 like i want it gives me -1
am i understanding VALUE wrong...did you mean VAL?

Posted: Fri Dec 02, 2005 6:28 pm
i know this was asnwered before but when i search for filename
there is no thread that has the asnwer in it...

Code: Select all

``````
input "enter your ships name"; file\$

file\$ = "boohoo"   <<<<example

open "c:\xxxx\file\$ + ".txt" for append as #1

``````
shouldnt this open a file called boohoo.txt?

Posted: Fri Dec 02, 2005 7:09 pm
Anonymous wrote:
Nekrophidius wrote:Okay to make something a bit more clear:

Code: Select all

``````IF value AND 1 = 1 THEN
' value is ODD
ELSE
'value is EVEN
END IF``````
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even.
okay i tried this but it turns my number to
a negative...

this is for TRAVELLER RPG ships... BTW

example:

Code: Select all

``````berth = int(rnd * 16) + 5  <<<minium 5 berths max 20 berths

berth = 11  <<<<example...

if 11 AND 1 = 1 then
let 11 = (11 - 1)
else
PRINT "its okay its even"
end if
``````
but this does not give me 10 like i want it gives me -1
am i understanding VALUE wrong...did you mean VAL?
What Nek means as "value" is the name of a variable containing your value to be tested for odd/even. In your case "value" is "berth".

I tried his code and it works fine. If berth is 11, it's odd, and as a result berth is set to 11-1 or 10. Can't see why it didn't work for you.
*****

Posted: Fri Dec 02, 2005 7:42 pm
Anonymous wrote:

Code: Select all

``````
input "enter your ships name"; file\$

file\$ = "boohoo"   <<<<example

open "c:\xxxx\file\$ + ".txt" for append as #1

``````
shouldnt this open a file called boohoo.txt?
It would work if you built the string properly...

Code: Select all

``open "c:\xxxx" + file\$ + ".txt" for append as #1``
On a related item...you're making two rookie mistakes on this line alone...

1. Absolute paths are a no-no.
2. Static file handles are also a no-no.

Posted: Fri Dec 02, 2005 8:11 pm
moneo wrote:
What Nek means as "value" is the name of a variable containing your value to be tested for odd/even. In your case "value" is "berth".

I tried his code and it works fine. If berth is 11, it's odd, and as a result berth is set to 11-1 or 10. Can't see why it didn't work for you.
*****

correct i was just plugin' in 11 as the "value" to show whats happening...
i used berth as my variable..

but it turned all my "berths" to -1..

i had a number of berths like 15, 11, 29...

it set them all to -1

Posted: Fri Dec 02, 2005 8:15 pm
Nekrophidius wrote:
1. Absolute paths are a no-no.
2. Static file handles are also a no-no.
im defintly a rookie...

1. how would i find the file if i dont have an exact path?

2. do you mean that next file # available thingy?

Posted: Fri Dec 02, 2005 8:42 pm
Anonymous wrote:.....

correct i was just plugin' in 11 as the "value" to show whats happening...
i used berth as my variable..

but it turned all my "berths" to -1..

i had a number of berths like 15, 11, 29...

it set them all to -1
That can't be. Post your code so we can take a look.
*****

Posted: Sat Dec 03, 2005 12:52 am
moneo wrote:
Anonymous wrote:.....

correct i was just plugin' in 11 as the "value" to show whats happening...
i used berth as my variable..

but it turned all my "berths" to -1..

i had a number of berths like 15, 11, 29...

it set them all to -1
That can't be. Post your code so we can take a look.
*****

Code: Select all

``````
im confused....:(

berth = int(rnd * 16) + 5

if berth AND 1 = 1 THEN 'its ODD
let berth = (berth - 1)
print berth
else
print "its okay berths even" 'its EVEN
end if

``````

Posted: Sat Dec 03, 2005 12:55 am
Anonymous wrote:im defintly a rookie...
We were all rookies once.
Anonymous wrote:1. how would i find the file if i dont have an exact path?
Chances are your file is going to be in the same directory as your executable. All you'd need is the filename in that case. If it's in a subdirectory, then you need the subdir name and the filename (say you have a subdir called "stuff" and a file called "myfile.txt", you'd write this as "stuff\myfile.txt"). Absolute paths are extremely unstable when it comes to another person's computer and in QB, this leads to program crashes.
Anonymous wrote:2. do you mean that next file # available thingy?
Yeah...the keyword is "freefile":

Code: Select all

``````DIM fh AS INTEGER
fh = FREEFILE
OPEN myfile\$ FOR APPEND AS #fh
PRINT #fh, "Stuff!"
CLOSE fh``````
It's a lot cleaner and safer.

Posted: Sat Dec 03, 2005 10:45 am
Guest: Nek's code works... if every variable is integer.
You are probably using the QB defaults that make all variables floating point, so rounding errors may prevent the code from work. The easiest workaround is to put a pair of parentheses in the IF condition.

Code: Select all

``````berth = int(rnd * 16) + 5

if (berth AND 1) = 1 THEN 'its ODD
let berth = (berth - 1)
print berth
else
print "its okay berths even" 'its EVEN
end if
``````

Posted: Sat Dec 03, 2005 10:15 pm
Antoni wrote:Guest: Nek's code works... if every variable is integer.
You are probably using the QB defaults that make all variables floating point, so rounding errors may prevent the code from work. The easiest workaround is to put a pair of parentheses in the IF condition.

Code: Select all

``````berth = int(rnd * 16) + 5

if (berth AND 1) = 1 THEN 'its ODD
let berth = (berth - 1)
print berth
else
print "its okay berths even" 'its EVEN
end if
``````
yep thanks that did it...

Posted: Sat Dec 03, 2005 10:17 pm
would you guys like to be included in the credits
of the program for helping with the math?

its just a program that makes data for a starship
for the RPG traveller...basiclly just a tool...not a game
in its self...

### Re: odd or even

Posted: Wed Dec 07, 2005 12:48 pm
Anonymous wrote:i'm not seeing a command in my qbasic book
for determining ood or even numbers and the
math eludes me...

what i want to do is generate a number
then check if its odd or even if its even
i leave it alone if its odd i add/sub 1 to make it even

berths = int(rnd * X) +A

if berths = even then goto moveon
if berths = odd then berth = (berth + or - 1)

i do need the +A as there needs to be a minimum # of berths
i have made that an even number to be safe...
The simplest method is to use the MOD function.

Code: Select all

``IF berths MOD 2 > 0 THEN berths = berths +1``
(That should do it.)

### Divisibility by 4 ie. Leap Year

Posted: Fri Dec 09, 2005 12:30 pm
Here's a trick I use to check for divisibility by 4, for Leap Years and such:

Code: Select all

``````for i = 2000 to 2017
print i;
if i and not -4 then print "Not" else print "Is"
next i``````
The "-4" is for divisibility by 4. The same concept will work for any power of two.

Posted: Fri Dec 09, 2005 4:58 pm
Well, the easy check for divisibility is:

Code: Select all

``if number MOD divisor = 0 then 'number is divisible``
But it only works on integers. (But there's not really a clear definition of divisibility for non-integers anyway.)