converting Niklaus Wirth's examples from pascal to C

Discussion in 'C Programming' started by FSX, May 21, 2009.

  1. FSX

    FSX Guest

    Hello,

    How difficult is it to convert "Algorithms + Data structures = Programs
    by N. Wirth"'s examples to C?

    Cheers,

    FSX

    Posted using www.webuse.net
    FSX, May 21, 2009
    #1
    1. Advertising

  2. FSX

    osmium Guest

    "FSX" wrote:

    > How difficult is it to convert "Algorithms + Data structures = Programs
    > by N. Wirth"'s examples to C?


    I have done that a few times and don't remember any particular difficulty.
    osmium, May 21, 2009
    #2
    1. Advertising

  3. FSX

    FSX Guest

    osmium <> wrote:

    > "FSX" wrote:
    >
    > > How difficult is it to convert "Algorithms + Data structures = Programs
    > > by N. Wirth"'s examples to C?

    >
    > I have done that a few times and don't remember any particular difficulty.
    >



    Great! Can you post or send them? It would be great since I know almost
    nothing about pascal and I have that book lying on a shelf...

    Posted using www.webuse.net
    FSX, May 21, 2009
    #3
  4. FSX

    osmium Guest

    "FSX" wrote:

    > osmium <> wrote:
    >
    >> "FSX" wrote:
    >>
    >> > How difficult is it to convert "Algorithms + Data structures = Programs
    >> > by N. Wirth"'s examples to C?

    >>
    >> I have done that a few times and don't remember any particular
    >> difficulty.
    >>

    >
    >
    > Great! Can you post or send them? It would be great since I know almost
    > nothing about pascal and I have that book lying on a shelf...


    Sorry. They were embedded in bigger works and I have no idea where they are
    now. It was several years ago.
    osmium, May 21, 2009
    #4
  5. FSX

    FSX Guest

    osmium <> wrote:

    > "FSX" wrote:
    >
    > > osmium <> wrote:
    > >
    > >> "FSX" wrote:
    > >>
    > >> > How difficult is it to convert "Algorithms + Data structures = Programs
    > >> > by N. Wirth"'s examples to C?
    > >>
    > >> I have done that a few times and don't remember any particular
    > >> difficulty.
    > >>

    > >
    > >
    > > Great! Can you post or send them? It would be great since I know almost
    > > nothing about pascal and I have that book lying on a shelf...

    >
    > Sorry. They were embedded in bigger works and I have no idea where they are
    > now. It was several years ago.
    >


    So expect some questions at least... =) Do you think it's a good idea to post
    questions about pascal conversion to C here? Thanks in advance...

    Posted using www.webuse.net
    FSX, May 21, 2009
    #5
  6. FSX

    jameskuyper Guest

    FSX wrote:
    > osmium <> wrote:
    >
    > > "FSX" wrote:
    > >
    > > > osmium <> wrote:
    > > >
    > > >> "FSX" wrote:
    > > >>
    > > >> > How difficult is it to convert "Algorithms + Data structures = Programs
    > > >> > by N. Wirth"'s examples to C?
    > > >>
    > > >> I have done that a few times and don't remember any particular
    > > >> difficulty.
    > > >>
    > > >
    > > >
    > > > Great! Can you post or send them? It would be great since I know almost
    > > > nothing about pascal and I have that book lying on a shelf...

    > >
    > > Sorry. They were embedded in bigger works and I have no idea where they are
    > > now. It was several years ago.
    > >

    >
    > So expect some questions at least... =) Do you think it's a good idea to post
    > questions about pascal conversion to C here? Thanks in advance...


    Questions about conversion of code from another language to C, or vice
    versa are equally appropriate here or in a group devoted to the other
    language, such as comp.lang.pascal. However, for best results I'd
    recommend cross-posting the question to both groups.
    jameskuyper, May 21, 2009
    #6
  7. FSX

    jameskuyper Guest

    jameskuyper wrote:
    ....
    > Questions about conversion of code from another language to C, or vice
    > versa are equally appropriate here or in a group devoted to the other
    > language, such as comp.lang.pascal. However, for best results I'd
    > recommend cross-posting the question to both groups.


    Sorry - I only checked for the existence of comp.lang.pascal; I forgot
    to check whether it's still active. According to Google Groups, it's
    available only as an archive; there hasn't been a new message since
    2004.

    However, there's several comp.lang.pascal.* newsgroups that are still
    active: ansi-iso, borland, misc, delphi.*, mac.
    comp.lang.pascal.borland is the busiest, with 15 messages a month.
    Many of them are spam, but it's not quite dead.
    jameskuyper, May 21, 2009
    #7
  8. FSX

    FSX Guest

    Richard Heathfield <> wrote:

    > FSX said:
    >
    > > Hello,
    > >
    > > How difficult is it to convert "Algorithms + Data structures =
    > > Programs by N. Wirth"'s examples to C?


    (snip)

    > But generally it isn't that hard, honest!


    Thanks so much! I have the hardcopy of the italian version, and you are so
    helpful!

    =)

    Best regards
    FSX

    Posted using www.webuse.net
    FSX, May 22, 2009
    #8
  9. FSX writes:
    > How difficult is it to convert "Algorithms + Data structures = Programs
    > by N. Wirth"'s examples to C?


    Difficulty is relative. To answer the question for yourself, try
    it and see. Duh.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 29, 2009
    #9
  10. On May 21, 10:18 am, Richard Heathfield <> wrote:
    > FSX said:
    >
    > > Hello,

    >
    > > How difficult is it to convert "Algorithms + Data structures =
    > > Programs by N. Wirth"'s examples to C?

    >
    > Not too bad.
    >
    > Let's take a frinstance. In my copy it's on p15 (copy-typed, so
    > please forgive any unwitting unPascalisms - and I may have changed
    > the spacing a bit):
    >
    > program power(output);
    > { decimal representation of negative powers of 2}
    > const n = 10;
    > type digit = 0..9;
    > var i, k, r: integer;
    >   d: array[1..n] of digit;
    > begin  for k := 1 to n do
    >   begin write('.'); r := 0;
    >     for i := 1 to k - 1 do
    >       begin r := 10 * r + d; d := r div 2;
    >         r := r - 2 * d; write(chr(d+ord('0')))
    >       end;
    >     d[k] := 5; writeln('5');
    >   end
    > end .
    >
    > Here's the C version, which is as close a translation as I can
    > reasonably manage without taking ages over it:
    >
    > /* program: power
    >  *
    >  * decimal representation of negative powers of 2
    >  *
    >  * translated into C from [Wirth 1976] - the translation
    >  * is as precise as I can make it (in a reasonably short
    >  * time), so it will reflect Wirth's style rather than mine.
    >  * I have, however, translated 1-based indexing into
    >  * 0-based indexing.
    >  *
    >  */
    >
    > #include <stdio.h>
    >
    > #define n 10 /* why not const? because no VLAs in C90! */
    >
    > int main(void)
    > {
    >   int i, k, r;
    >   int d[n];
    >   for(k = 0; k < n; k++)
    >   {
    >     putchar('.');
    >     r = 0;
    >     for(i = 0; i < k; i++)
    >     {
    >       r = 10 * r + d;
    >       d = r / 2;
    >       r = r - 2 * d;
    >       putchar(d + '0');
    >     }
    >     d[k] = 5;
    >     puts("5");
    >   }
    >   return 0;
    >
    > }
    >
    > And here's the output:
    >
    > .5
    > .25
    > .125
    > .0625
    > .03125
    > .015625
    > .0078125
    > .00390625
    > .001953125
    > .0009765625
    >
    > which is identical to Wirth's output.
    >
    > I think the main gotchas in translating this book's code examples
    > into C are:
    >
    > 1) sets (for which C has no direct equivalent);
    > 2) indexing (1-based makes for some strange contortions - the C
    > version will generally be simpler);
    > 3) pointers - the p^ syntax is a little strange until you get used
    > to it.
    >
    > But generally it isn't that hard, honest!
    >


    Don't know what Pascal's initialisation semantics are, but isn't this
    line of code -

    > r = 10 * r + d;


    - using uninitialised elements of the array 'd'?

    - Anand
    Anand Hariharan, May 29, 2009
    #10
  11. On May 29, 9:36 am, Jensen Somers <> wrote:
    > Anand Hariharan wrote:
    > > On May 21, 10:18 am, Richard Heathfield <> wrote:

    >
    > >> /* program: power
    > >>  *
    > >>  * decimal representation of negative powers of 2
    > >>  *
    > >>  * translated into C from [Wirth 1976] - the translation
    > >>  * is as precise as I can make it (in a reasonably short
    > >>  * time), so it will reflect Wirth's style rather than mine.
    > >>  * I have, however, translated 1-based indexing into
    > >>  * 0-based indexing.
    > >>  *
    > >>  */

    >
    > >> #include <stdio.h>

    >
    > >> #define n 10 /* why not const? because no VLAs in C90! */

    >
    > >> int main(void)
    > >> {
    > >>   int i, k, r;
    > >>   int d[n];
    > >>   for(k = 0; k < n; k++)
    > >>   {
    > >>     putchar('.');
    > >>     r = 0;
    > >>     for(i = 0; i < k; i++)
    > >>     {
    > >>       r = 10 * r + d;
    > >>       d = r / 2;
    > >>       r = r - 2 * d;
    > >>       putchar(d + '0');
    > >>     }
    > >>     d[k] = 5;
    > >>     puts("5");
    > >>   }
    > >>   return 0;

    >
    > >> }

    >

    (...)
    >
    > > Don't know what Pascal's initialisation semantics are, but isn't this
    > > line of code -

    >
    > >>       r = 10 * r + d;

    >
    > > - using uninitialised elements of the array 'd'?

    >
    > > - Anand

    >
    > As far as I can see no, because when they enter that loop the 1st time
    > at least 1 value will be filled in. During the first run of the first
    > for loop the inner for loop is not executed.
    >


    Mea culpa. Missed this line the first time:

    > >> d[k] = 5;


    That and the inner loop that ensures i < k makes it all tick.

    thank you,
    - Anand
    Anand Hariharan, May 29, 2009
    #11
  12. "FSX" <> wrote in news::

    > Hello,
    >
    > How difficult is it to convert "Algorithms + Data structures = Programs
    > by N. Wirth"'s examples to C?
    >
    > Cheers,
    >
    > FSX
    >
    > Posted using www.webuse.net


    google: pascal to c translator

    In Debian:

    $ aptitude search p2c
    p p2c - Pascal to C translator
    Donkey Hottie, Jun 2, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam

    Pascal-style "types"

    Adam, Feb 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    343
  2. Ivan Vecerina
    Replies:
    1
    Views:
    1,246
    Jerry Coffin
    Oct 21, 2003
  3. Jeff Schwab

    Translating from C to Pascal

    Jeff Schwab, Dec 31, 2003, in forum: C++
    Replies:
    10
    Views:
    688
    Jeff Schwab
    Jan 1, 2004
  4. robert
    Replies:
    0
    Views:
    327
    robert
    Oct 26, 2006
  5. David Buckley

    req converting video to flash video examples

    David Buckley, Apr 22, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Peter Bromberg [C# MVP]
    Apr 22, 2008
Loading...

Share This Page