|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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. URL So, if anyone could tell me were I'm going wrong, that would be great! Thanks, |
|
#2
|
||||
|
||||
|
Quote:
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 re-write 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 pseudo-code): 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 Windows-based editing environment to work in.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov Last edited by Schol-R-LEA : September 30th, 2006 at 09:12 AM. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
__________________
~James [Not currently seeking freelance work] Like philosophy or interested in spirituality? Philosophorum. Game Dev Experts Forums Foresight Linux - Because your desktop should be cool! Linux FAQ FedoraFAQ UbuntuGuide |
|
#5
|
||||
|
||||
|
Quote:
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. ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Pascal Help! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|