converting Niklaus Wirth's examples from pascal to C

O

osmium

FSX said:
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.
 
F

FSX

osmium said:
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
 
O

osmium

FSX said:
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.
 
F

FSX

osmium said:
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
 
J

jameskuyper

FSX said:
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.
 
J

jameskuyper

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.
 
K

Keith Thompson

FSX said:
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.
 
A

Anand Hariharan

FSX said:
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
 
A

Anand Hariharan

Anand said:
/* 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'?

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:

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

thank you,
- Anand
 
D

Donkey Hottie

FSX said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top