Samuel Williams wrote:
Dear Friends,

Hello,

It would also be great if someone could rewrite the Perl source code
example so that it is as close as possible to the C implementation:

        http://programming.dojo.net.nz/languages/c/index


/* Include the standard input / output functions */
#include <stdio.h>

int main()
{
    /* Initialize the array of doors to 0 (closed) */
    char is_open[100] = {0};

Why are you creating an array of 101 elements when you only use 100? Your comment says you initialize the array but you only initialize the first element.

    int pass, door;

    /* Process the doors */
    for (pass = 0; pass < 100; ++pass)
        for (door = pass; door < 100; door += pass+1)
            is_open[door] = !is_open[door];

    /* Print out the results */
    for (door = 0; door < 100; ++door)
        printf("Door #%d is %s.\n", door+1, (is_open[door] ? "open." : 
"closed."));

    return 0;
}


sub main()
{
    # Initialize the array of doors to 0 (closed)
    $is_open[100] = 0;

    # Process the doors
    for ($pass = 0; $pass < 100; ++$pass){
        for ($door = $pass; $door < 100; $door += $pass+1){
            $is_open[$door] = !$is_open[$door];}}

    # Print out the results
    for ($door = 0; $door < 100; ++$door){
printf("Door #%d is %s.\n", $door+1, ($is_open[$door] ? "open." : "closed."));}

    return 0;
}




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to