Author |
Message |
Voltage128
|
Posted: Wed Nov 29, 2006 9:30 pm Post subject: Decimal to Binary. First turing project ever. |
|
|
Something I threw together in about 5 min today for my comp teacher, he wanted us to use "-1" to end the program instead of just typing "stop" or "end" because he says "-1" is easier to do. Then he pinged me marks later for it. What do you guys think?
code: | var number, temp, binarySize : int
loop
put "Enter a positive integer or -1 to end the program: " ..
get number
exit when number = -1
temp := number
binarySize := 0
loop
exit when temp < 2
temp := temp div 2
binarySize := binarySize + 1
end loop
binarySize := binarySize + 1
var binary : array 1 .. binarySize of int
for i : 1 .. binarySize
binary (i) := number mod 2
number := number div 2
end for
put "The binary representation of your number is: " ..
for decreasing j : binarySize .. 1
put binary (j) ..
end for
put ""
end loop
put ""
put "End of the program!" |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Wed Nov 29, 2006 9:57 pm Post subject: (No subject) |
|
|
good job, now make that a function that error traps for improper input.
as for inputting -1 as an exit condition, thats pretty foolish, using a simple "y/n" is probably the better idea. |
|
|
|
|
|
Voltage128
|
Posted: Wed Nov 29, 2006 10:28 pm Post subject: (No subject) |
|
|
alright, i think that is better for the exiting the program. If i need to correct it again please let me kno, im learning. Also how would i go about making a function that error traps for improper input?
code: | var number, temp, binarySize : int
var YesNo : string
loop
put "Enter a positive integer: " ..
get number
temp := number
binarySize := 0
loop
exit when temp < 2
temp := temp div 2
binarySize := binarySize + 1
end loop
binarySize := binarySize + 1
var binary : array 1 .. binarySize of int
for i : 1 .. binarySize
binary (i) := number mod 2
number := number div 2
end for
put "The binary representation of your number is: " ..
for decreasing j : binarySize .. 1
put binary (j) ..
end for
put ""
put "Would you like to enter another integer? (y/n)"
View.Update
get YesNo
exit when YesNo = "n"
put ""
cls
end loop
put ""
put "End of the program!" |
|
|
|
|
|
|
frank26080115
|
Posted: Fri Dec 01, 2006 4:54 pm Post subject: (No subject) |
|
|
thats pretty kickass, i had to make one for class too
code: |
var baseten : int
var basetenstr : string
var maxplace : int := 25
var displayplace : int := 1
var bin1 : array 1 .. maxplace of int
loop
displayplace := 1
loop
put "input base ten number"
get basetenstr
if strintok (basetenstr, 10) then % checking
exit
end if
end loop
baseten := strint (basetenstr, 10) % string to interger converting
put "decimal ---> binary:"
put baseten, " ---> " ..
for i : 1 .. maxplace % math
if baseten - (2 ** (maxplace - i)) >= 0 then
bin1 ((1 + maxplace - i)) := 1
baseten -= (2 ** (maxplace - i))
else
bin1 ((1 + maxplace - i)) := 0
end if
end for
for i : 1 .. maxplace % eliminate leading 0
if bin1 ((1 + maxplace - i)) = 1 then
exit
end if
displayplace += 1
end for
for i : displayplace .. maxplace % display binary
put bin1 ((1 + maxplace - i)) ..
end for
put ""
end loop
|
and i had to do a binary to decimal
code: |
var inputedstr : string
var inputedlength : int
var baseten : int := 0
loop
%input
put "input binary number:"
get inputedstr
% < check for errors
loop
if strintok (inputedstr, 2) = true then
inputedlength := length (inputedstr)
exit
else
put "input binary number again:"
get inputedstr
end if
end loop
% check for errors >
% < math
for i : 1 .. inputedlength
if inputedstr (i) = "1" then
baseten += 2 ** (inputedlength - i)
end if
end for
% math >
% answer
put "answer:"
put baseten
% reset
baseten := 0
end loop
|
|
|
|
|
|
|
Voltage128
|
Posted: Fri Dec 01, 2006 5:08 pm Post subject: (No subject) |
|
|
yea, my teacher actually takes marks off mine if I did the error check thing. Since in the book it doesnt ask for a error proof he says its un-needed and he takes marks off for not following the instructions. Which is stupid because all i learn are the basics of the program and i want to learn more. So explain to me how you did that error check please? |
|
|
|
|
|
uberwalla
|
Posted: Fri Dec 01, 2006 6:43 pm Post subject: (No subject) |
|
|
ok a little off the wack but how did u guys know the formula for binary to decimal and back etc.? is that the ACTUAL formula or made up? cuz i see u both got same one |
|
|
|
|
|
frank26080115
|
Posted: Fri Dec 01, 2006 7:27 pm Post subject: (No subject) |
|
|
strintok means if this string is able to be converted into a number, then its ok, if the string contains letters then it is not ok and it will ask for the input again
oh and i dont know anything about "mod" so i calculated manually
binary 0001 is 2 ^ 1-1 = 1
binary 0010 is 2 ^ 2-1 = 2
binary 0011 is 2 ^ 2-1 + 2 ^ 1-1 = 3
binary 0100 is 2 ^ 3-1 = 4
so if the decimal number is 6
6 - (2 ^ 3 - 1) = 2
2 - (2 ^ 2 - 1) = 0
0110
reverse the process to get binary to decimal, which is a lot easier |
|
|
|
|
|
Voltage128
|
Posted: Fri Dec 01, 2006 8:02 pm Post subject: (No subject) |
|
|
mod just gets the remainder of a number when u divide it. so when u are doing a decimal to binary conversion lets say 5 you would go
5/2 = which gives you 2 and a remainder of 1
then u take the 2 on top divide that by 2 and u get 1 and a remainder of 0
therefore 5=101
you take the last 1 and the 2 remainders to get the binary number. Its just one way of figuring out the conversion. Mod just calculates the remainder. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jamonathin
|
Posted: Sat Dec 02, 2006 8:56 pm Post subject: (No subject) |
|
|
Freakman wrote: as for inputting -1 as an exit condition, thats pretty foolish, using a simple "y/n" is probably the better idea.
How so Freakman? -1 in this case is better than y/n. Y/N implies that he would either have to make a completely different get statement OR make his input-variable string, then convert the string after the input -> causing more work.
So it's either, annoyance to the user, or more work for the programmer.
As an end result, -1 FTW.
(ps. my profs use -1) |
|
|
|
|
|
Voltage128
|
Posted: Sat Dec 02, 2006 9:16 pm Post subject: (No subject) |
|
|
-1 was a lot easier to do I agree. But it isnt that much work to put a yes/no as an exit condition. For this case however, i do like the -1 as an exit condition better.
I did realize that it is really annoying to have they yes/no in the program. Everytime it asks if you would like to continue. |
|
|
|
|
|
Silent Avenger
|
Posted: Sat Dec 02, 2006 9:24 pm Post subject: (No subject) |
|
|
-1 is easier but you also have to think about the user. To an average user it would be a lot better for them to have a Y/N option. Although you program is most likely not going to be used by the average Joe on the computer anyways. |
|
|
|
|
|
Voltage128
|
Posted: Sat Dec 02, 2006 9:56 pm Post subject: (No subject) |
|
|
Yes it decimal to binary isnt a common thing most people use everyday. So then you are agreeing that the -1 exit condistion is better suited for this program? |
|
|
|
|
|
jamonathin
|
Posted: Sat Dec 02, 2006 10:05 pm Post subject: (No subject) |
|
|
for a game, or a full scale program (with many subprogs), yes, y/n is lovely, however, for simple progs that deal with people going "ok whats this, now whats this, and this" wanting fast answers, they wont want a convo with the program. But anyways, i think we're getting a little off topic. Gj btw. |
|
|
|
|
|
Silent Avenger
|
Posted: Sat Dec 02, 2006 10:35 pm Post subject: (No subject) |
|
|
Voltage128 wrote: Yes it decimal to binary isnt a common thing most people use everyday. So then you are agreeing that the -1 exit condistion is better suited for this program? Yes I am agreeing that the -1 exit condition is better suited for this program in your situation than the Y/N. |
|
|
|
|
|
|