develooper Front page | perl.beginners | Postings from February 2002

Re: Help can't figure this one out

Thread Previous
From:
John W. Krahn
Date:
February 14, 2002 01:11
Subject:
Re: Help can't figure this one out
Message ID:
3C6B7F7C.4E22D8E6@acm.org
Bruce Ambraal wrote:
> 
> I have written following coding to produce a triangle pattern(see below);
> I now want to  produce following paterns I can't figer this out
> 
>         (a)            (b)                 (c)                 (d) diamond
>            *            * * * * *           * * * * *          *
>          * *           * * * *                * * * *        * * *
>        * * *           * * *                    * * *       * * * *
>      * * * *           * *                        * *      * * * * *
>    * * * * *          *                             *        * * * *
>                                                                 * * *
>                                                                   *
> ______________________________________________
> #!/usr/local/bin/perl -w
> my $num_rows;
> my $i;
> my $r;
> 
> $num_rows = <STDIN>;
> 
> for ($r = 1; $r <= $num_rows; $r++)
>     {
>     # indent by printing num_rows - r spaces
>     for ($i = 1; $i <= $num_rows - $r; $i++) {print ("  \n");}
>     # print r asterisks
>        for ($i=1; $i<= $r; $i++) { print ("     *");}
>     # drop cursor to a new line
>       print "\n";
>      }
> # end for


#!/usr/local/bin/perl -w
use strict;

my $num_rows = shift || die "usage: $0 rows\n";

print "a)\n";
print '  ' x (($num_rows+1) - $_), ' *' x $_, "\n" for 1 .. $num_rows;
print "\n";

print "b)\n";
print '  ', ' *' x $_, "\n" for reverse 1 .. $num_rows;
print "\n";

print "c)\n";
print '  ' x (($num_rows+1) - $_), ' *' x $_, "\n" for reverse 1 ..
$num_rows;
print "\n";

print "d)\n";
print '  ' x (($num_rows+1) - $_), ' *' x (2 * $_ - 1), "\n"
    for 1 .. ($num_rows/2+1), reverse 1 .. ($num_rows/2);
print "\n";

__END__



John
-- 
use Perl;
program
fulfillment

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About