September 29th, 2006, 01:31 PM

Pascal Help!
Hi guys,
Been doing Pascal at college an have been told to change a number into binary. I think I've got the idea of how to do it, it's just that the code seems to be full of errors which I just can find/understand.
I was using Freepascal, I couldn't copy it, so I took a screenshot.
So, if anyone could tell me were I'm going wrong, that would be great!
Thanks,
September 30th, 2006, 03:56 AM

Originally Posted by mit111
Hi guys,
Been doing Pascal at college an have been told to change a number into binary. I think I've got the idea of how to do it, it's just that the code seems to be full of errors which I just can find/understand.
The main problem I see is that you aren't actually saving the value you are reading into the variable input1; you dropped the trailing '1'. The reason you may not be getting a compiler error is because input is the name of the standard console, equivalent to stdin in C and Perl or cin in C++. Since you are giving ReadLn() an input stream but no variables, it doesn't actually read anything. To make things a bit less ambiguous, you might rename input1 to something like value or just simply n.
As a stylistic note, any time you have a series of variables with names like foo1, foo2, foo3, etc., you generally should simply replace them with an array:
Code:
answer: array [1..7] of Integer;
answer[1] := input1 mod 2;
input1 := input1 div 2; { I'll explain this in a moment }
answer[2] := input1 mod 2;
{ ... }
Also, as shown here, the successive values need to be from the division of the current version of the number, not the modulo; the modulo of any number by 2 is always either 0 or 1, after all, with 0 values being even and 1 values being odd (this is the basis of the algorithm).
There's another problem with this, in that you are printing the bits in the wrong order. In the algorithm you are using, the first bit returned is the least significant, i.e., the rightmost bit. The second bit it returns is the second twos, the third bit the fours, and so on. You can fix this by reversing either the order you produce them in, or, more usefully, the order in which you print them.
Alternately, you can dispense with all but one of the temporary values entirely, and instead rewrite the function so that it goes through the number recursively, getting the current bit, then calling itself, then printing the bit. This is the classic form of the algorithm, in fact (in pseudocode):
Code:
procedure printBinary(x) is
if x is less than zero
print a negative sign
printBinary(abs(x))
else
if x is equal to zero
return from the function
else
bit := x mod 2
printBinary(x div 2)
write(bit)
end printBinary
Done this way, you can print binary values for values up to 2,147,483,648 accurately. (BTW, the maximum value a 7 bit number can hold is 127, not 134).
(Converting this algorithm into Pascal is left as an exercise. As a hint, I will tell you that you'll need to write it as a procedure  or perhaps a function, one which returns, say, a string  and that you'll want to read the value in first before calling it.)
Finally, regarding the difficulty of cutting and pasting code from FreePascal, you should recall that a Pascal program is just a text file; you could, after having closed it in FreePascal, opened the file in a text editor such as Notepad, and cut and paste it from there. Alternately, you can use DevPascal as your development system, which uses the Free Pascal compiler but gives a Windowsbased editing environment to work in.
Last edited by ScholRLEA; September 30th, 2006 at 10:12 AM.
September 30th, 2006, 04:37 AM

Wow! Thanks for helping me understand it a little better.
Edit: I've made the file now and it compiles without any bugs, the only problem is, once I input my number the window closes, is there any way of stopping this so I can see if my code works?
September 30th, 2006, 08:38 AM

[hl=pascal]procedure pause;
begin
writeln('Press any key to continue...');
readkey;
end;[/hl]
September 30th, 2006, 09:13 AM

Originally Posted by LinuxPenguin
[hl=pascal]procedure pause;
begin
writeln('Press any key to continue...');
readkey;
end;[/hl]
Jepp, or use the 'readln();' function, I belive the only difference is that readkey waits untill you enter any key and readln() ignores all keys except the ENTER key.
EDIT: Also, instead of taking screenshots of you compiler, open the source code in wordpad and copy it from there.