Thread: Pascal Help!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep Power
    0

    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,
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,804
    Rep Power
    1569
    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 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.
    Last edited by Schol-R-LEA; September 30th, 2006 at 09:12 AM.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep Power
    0
    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?
  6. #4
  7. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1050
    [hl=pascal]procedure pause;
    begin
    writeln('Press any key to continue...');
    readkey;
    end;[/hl]
  8. #5
  9. C++arl!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Stockholm
    Posts
    165
    Rep Power
    18
    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.

IMN logo majestic logo threadwatch logo seochat tools logo