odd or even
odd or even
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...
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...
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.
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.
Okay to make something a bit more clear:
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even. ![Very Happy :D](./images/smilies/icon_biggrin.gif)
Code: Select all
IF value AND 1 = 1 THEN
' value is ODD
ELSE
'value is EVEN
END IF
![Very Happy :D](./images/smilies/icon_biggrin.gif)
okay i tried this but it turns my number toNekrophidius wrote:Okay to make something a bit more clear:
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even.Code: Select all
IF value AND 1 = 1 THEN ' value is ODD ELSE 'value is EVEN END IF
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
am i understanding VALUE wrong...did you mean VAL?
i know this was asnwered before but when i search for filename
there is no thread that has the asnwer in it...
shouldnt this open a file called boohoo.txt?
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
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".Anonymous wrote:okay i tried this but it turns my number toNekrophidius wrote:Okay to make something a bit more clear:
Basically...If the 0th bit of the value is on, the number is odd, If it's off, the number is even.Code: Select all
IF value AND 1 = 1 THEN ' value is ODD ELSE 'value is EVEN END IF
a negative...
this is for TRAVELLER RPG ships... BTW
example:
but this does not give me 10 like i want it gives me -1Code: 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
am i understanding VALUE wrong...did you mean VAL?
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.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
It would work if you built the string properly...Anonymous wrote:shouldnt this open a file called boohoo.txt?Code: Select all
input "enter your ships name"; file$ file$ = "boohoo" <<<<example open "c:\xxxx\file$ + ".txt" for append as #1
Code: Select all
open "c:\xxxx" + file$ + ".txt" for append as #1
1. Absolute paths are a no-no.
2. Static file handles are also a no-no.
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
moneo wrote:That can't be. Post your code so we can take a look.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
*****
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
We were all rookies once.Anonymous wrote:im defintly a rookie...![]()
![Very Happy :D](./images/smilies/icon_biggrin.gif)
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:1. how would i find the file if i dont have an exact path?
Yeah...the keyword is "freefile":Anonymous wrote:2. do you mean that next file # available thingy?
Code: Select all
DIM fh AS INTEGER
fh = FREEFILE
OPEN myfile$ FOR APPEND AS #fh
PRINT #fh, "Stuff!"
CLOSE fh
![Smile :)](./images/smilies/icon_smile.gif)
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.
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...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
Re: odd or even
The simplest method is to use the MOD function.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...
Code: Select all
IF berths MOD 2 > 0 THEN berths = berths +1
Divisibility by 4 ie. Leap Year
Here's a trick I use to check for divisibility by 4, for Leap Years and such:
The "-4" is for divisibility by 4. The same concept will work for any power of two.
Code: Select all
for i = 2000 to 2017
print i;
if i and not -4 then print "Not" else print "Is"
next i
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
--- Time flies like an arrow, but fruit flies like a banana ---
Well, the easy check for divisibility is:
But it only works on integers. (But there's not really a clear definition of divisibility for non-integers anyway.)
Code: Select all
if number MOD divisor = 0 then 'number is divisible
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.
Xerol's Music - Updated Regularly!
Xerol's Music - Updated Regularly!