odd or even

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

Guest

odd or even

Post by Guest »

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...
Guest

Post by Guest »

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.
Guest

Post by Guest »

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
Nemesis
Coder
Posts: 36
Joined: Mon Aug 16, 2004 12:54 pm
Location: Colorado Springs, Colorado

Post by Nemesis »

if berths and 1 then
berth = (berth + or - 1)
else
goto moveon
end if

laters,

Nemesis
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

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. :D
Guest

Post by Guest »

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. :D
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?
Guest

Post by Guest »

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?
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

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. :D
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.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

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.
Guest

Post by Guest »

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
Guest

Post by Guest »

Nekrophidius wrote:
1. Absolute paths are a no-no.
2. Static file handles are also a no-no.
im defintly a rookie... :oops:

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


2. do you mean that next file # available thingy?
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

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.
*****
Guest

Post by Guest »

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

Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Anonymous wrote:im defintly a rookie... :oops:
We were all rookies once. :D
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. :)
Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni »

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 
Guest

Post by Guest »

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...
Guest

Post by Guest »

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...
Guest

Re: odd or even

Post by Guest »

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.)
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Divisibility by 4 ie. Leap Year

Post by Zim »

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.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
User avatar
Xerol
Veteran
Posts: 81
Joined: Tue Jan 04, 2005 6:27 pm
Location: Timonium, MD
Contact:

Post by Xerol »

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.)
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.

Xerol's Music - Updated Regularly!
Post Reply