
-----------------------------------
ZeroPaladn
Thu Sep 29, 2005 1:07 pm

shortening code pops up prob...
-----------------------------------
Hey, my decimal/binary/hex program is almost complete (damn math), and im try to shorten some code. this is the code that i made to convert decimal to binary, and you have to know how to do math between binary and decimal to see how this code works. regardless, its buggy, here it is.


var numin : int
var binout : array 1..8 of int
var num2binmath : int := 128

get numin

for i : 1..8
if numin < num2binmath then
binout (i) := 0
else
numin := numin - num2binmath
binout (i) := 1
num2binmath := num2binmath div 2
end if
end for

for o : 1..8
put binout (o)..
end for


thanks guys

-----------------------------------
codemage
Thu Sep 29, 2005 2:02 pm


-----------------------------------
Your algorithm is fundamentally flawed.
Try again using this pseudo-code:

1.  Let D= the number we wish to convert from decimal to binary.
2.  Repeat until D=0:
    a) If D is odd, put "1" in the leftmost open column, and subtract 1 from D.
    b) else (D is even), put "0" in the leftmost open column.
    c) Divide D by 2.
    End Repeat

This is a slim algorithm, I did it in 15 lines - if you've got significantly more than that then you can still cut back.

-----------------------------------
ZeroPaladn
Thu Sep 29, 2005 8:52 pm


-----------------------------------
thanks alot, the old code took up about 80 lines what youve just decribed in 15. Thats what i hate about my prgramming, i can do it, but its alot of code.

-----------------------------------
wtd
Thu Sep 29, 2005 8:55 pm


-----------------------------------
Look for patterns.  Places where you're doing repetitive work.  Then let the computer do the repitition.

-----------------------------------
ZeroPaladn
Fri Sep 30, 2005 8:52 am


-----------------------------------
HURRAY! fixed it! the area where it was doing the dividing, it wonly worked when it went thru that part of the for loop, so it didnt use that command when then number was smaller than was asked, so a bunch of 0's popped up. just move the calculation (num2binmath := num2binmath div 2), like so...


var numin : int
var binout : array 1 .. 8 of int
var num2binmath : int := 128

get numin

for i : 1 .. 8
    if numin < num2binmath then
        binout (i) := 0
    else
        numin := numin - num2binmath
        binout (i) := 1
    end if
    num2binmath := num2binmath div 2
end for

for o : 1 .. 8
    put binout (o) ..
end for


and it works. thanks wtd.

-----------------------------------
codemage
Fri Sep 30, 2005 12:03 pm


-----------------------------------
Here's the algorithm I described, now that you've solved it.  (For reference's sake).

var dec : int
var bin : array 1 .. 8 of int
get dec

for decreasing counter : 8 .. 1
    if dec mod 2 = 1 then
        bin (counter) := 1
        dec := dec - 1
    else
        bin (counter) := 0
    end if
    dec := dec div 2
end for

for counter : 1 .. 8
    put bin (counter) ..
end for

-----------------------------------
[Gandalf]
Fri Sep 30, 2005 7:40 pm


-----------------------------------
Ah, I posted this in the other thread by mistake...  Here's what I got:

var inputNum : int
var binaryOut : array 1 .. 8 of int := init (0, 0, 0, 0, 0, 0, 0, 0)

get inputNum

for count : 1 .. 8
    if inputNum mod 2 = 1 then
        binaryOut (count) := 1
    else
        binaryOut (count) := 0
    end if
    inputNum := inputNum div 2
end for

for decreasing count : 8 .. 1
    put binaryOut (count) ..
end for

-----------------------------------
ZeroPaladn
Mon Oct 03, 2005 9:02 am


-----------------------------------
aha... ive studied both of your subbimmisons and somethings bugging me... what the hell does mod do?

-----------------------------------
Tony
Mon Oct 03, 2005 10:23 am


-----------------------------------
mod gives you a remainder of the division.

-----------------------------------
codemage
Mon Oct 03, 2005 11:57 am


-----------------------------------
6 mod 2 = 0
7 mod 2 = 1

20 mod 5 = 0
23 mod 5 = 3

etc.

A very useful operator/function (depending on the language) in certain spots.

-----------------------------------
wtd
Mon Oct 03, 2005 12:46 pm


-----------------------------------
Who needs loops?

function dec2bin (number : int) : string
   if number = 0 then
      result "0"
   else
      result (dec2bin (number div 2) + strint (number mod 2))
   end if
end dec2bin

-----------------------------------
ZeroPaladn
Mon Oct 03, 2005 12:56 pm


-----------------------------------
so tahts what it does, ive read the turing help all it says is "modulo"... whatever that is.
