Im trying to get Base10 numbers to show as Base2
Author |
Message |
TokenHerbz
|
Posted: Mon Jul 31, 2006 12:49 pm Post subject: Im trying to get Base10 numbers to show as Base2 |
|
|
Here is my code:
Needing help with >>>fcn base10_base2<<<
code: |
%%trying to make it base2 -> base10 and base10 -> base2
var total : int
%%will change a base10 number into base2
fcn base10_base2 (base_number : int) : int
var base2 : flexible array 1 .. 0 of int
var number : int := base_number
var join_numbers : string := ""
loop
new base2, upper (base2) + 1 %%adds an array as the numbers are coming
if number rem 2 = 1 then %%if numbers odd it will add a one
base2 (upper (base2)) := 1
else
base2 (upper (base2)) := 0 %%if numbers even it will add a zero
end if
number div= 2 %%we divide the number by 2
exit when number / 2 <= 1 %%leaves this loop when base10 cant be devided anymore
end loop
%%debugging purposes
for i : 1 .. upper (base2)
put base2 (i)
end for
%%rewrites the numbers backwards
for decreasing i : upper (base2) .. 1
join_numbers += intstr (base2 (i))
end for
result strint (join_numbers)
end base10_base2
%%will make a base2 number a base10
fcn base2_base10 (base_number : string) : int
var number : int := 0
var base10 : array 1 .. length (base_number) of string
%%reverse the array of numbers to calculate it right to left
for i : 1 .. length (base_number)
base10 (i) := base_number ((length (base_number) + 1) - i)
end for
%%finds out the value of the base2 number
for i : 1 .. length (base_number)
if base10 (i) = intstr (0) then %%if numbers a 0
number += 1 %%we add 1 to the value
else
number += 2 ** i
end if
end for
result number
end base2_base10
total := base10_base2 (54)
put total
total := base2_base10 ("11010")
put total
%%11010 = 54
|
Note that the function base2_base10 seems to work fine, its the function base10_base2 i need help with.. I dont get where im going wrong with it. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Mon Jul 31, 2006 5:08 pm Post subject: (No subject) |
|
|
This is basically what you did, except cleaned up. I got rid of the use of a flexible array. If you were going to use an array, have it be an array of boolean (true for 1, false for 0). Your function should return a string or something other than an integer, because integers are always in base10.
code: |
module String
export reverse
function reverse (s : string) : string
var r := ""
for decreasing i : length (s) .. 1
r += s (i)
end for
result r
end reverse
end String
%%will change a base10 number into base2
fcn base10_base2 (base_number : int) : string
var base2 := ""
var n := base_number
loop
exit when n = 0
if n mod 2 = 0 then
base2 += "0"
else
base2 += "1"
end if
n div= 2
end loop
result String.reverse (base2)
end base10_base2
put base10_base2 (49)
|
|
|
|
|
|
|
Delos
|
Posted: Mon Jul 31, 2006 5:15 pm Post subject: (No subject) |
|
|
Just as a side note, there is a Turing fcn that accomplishes this. I won't mention it, since you're obviously learning more from recreating it. But, in case you were wondering.... |
|
|
|
|
|
TokenHerbz
|
Posted: Mon Jul 31, 2006 5:33 pm Post subject: (No subject) |
|
|
alright thanks. |
|
|
|
|
|
Clayton
|
Posted: Mon Jul 31, 2006 8:16 pm Post subject: (No subject) |
|
|
and what may I ask is this function? I'd like to know what it is to see what they did for it (if its not an external function ) |
|
|
|
|
|
Cervantes
|
Posted: Mon Jul 31, 2006 8:39 pm Post subject: (No subject) |
|
|
It's a function to convert a decimal (base 10) number to a binary (base 2) number, as the name of the function pretty clearly shows, the commenting confirms, and the coding asserts beyond doubt. |
|
|
|
|
|
[Gandalf]
|
Posted: Mon Jul 31, 2006 9:05 pm Post subject: (No subject) |
|
|
Cervantes, I'm quite sure he meant the Turing function Delos referred to, tut tut.
The function is indeed not coded in Turing. It's purpose is not specifically to convert numbers to and from different bases, though it can. |
|
|
|
|
|
Cervantes
|
Posted: Mon Jul 31, 2006 9:21 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Cervantes, I'm quite sure he meant the Turing function Delos referred to, tut tut.
Delos posted in this thread?
Sorry, Delos, you're posts just do that to me.
Sorry, SuperFreak82.
Sorry, [Gandalf].
Sorry, Cervantes. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|