Current location: Hot Scripts Forums » Programming Languages » Perl » Perl class deadbeat teacher need help


Perl class deadbeat teacher need help

Reply
  #1 (permalink)  
Old 11-30-03, 04:26 PM
Teppic Teppic is offline
New Member
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Perl class deadbeat teacher need help

I need some help debugging my perl scripts. my teacher has been MIA on my online class and over 75% of my fellow students have dropped the course. I need to get alot of work done between now and december 17nth.

Heres the only question im having trouble with right now. if u need my perl work on it so far let me know.

write a program that prompts the user for an integer representing width, and integer representing height and a character

a.The program then prints out a solid rectangle using the character with the entered width and height as the dimensions. Trust the user to enter integers but dont trust that they will be non-negitive or small enough to fit the screen.

b.Change the program so it prints the outline of a rectangle rather than a solid one.
Reply With Quote
  #2 (permalink)  
Old 11-30-03, 10:00 PM
YUPAPA's Avatar
YUPAPA YUPAPA is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Antarctica
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Quote:
Originally Posted by Teppic
I need some help debugging my perl scripts. my teacher has been MIA on my online class and over 75% of my fellow students have dropped the course. I need to get alot of work done between now and december 17nth.

Heres the only question im having trouble with right now. if u need my perl work on it so far let me know.

write a program that prompts the user for an integer representing width, and integer representing height and a character

a.The program then prints out a solid rectangle using the character with the entered width and height as the dimensions. Trust the user to enter integers but dont trust that they will be non-negitive or small enough to fit the screen.

b.Change the program so it prints the outline of a rectangle rather than a solid one.
is this a console program you are creating?
__________________
YuPaPa.CoM is my home ~
Reply With Quote
  #3 (permalink)  
Old 12-01-03, 12:36 AM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
someone that knows sprintf can trim this down a bit but I am sure this would work (for console):

Code:
#!perl -w

&start;

sub start {
   print "Enter a character:";
   $c = <STDIN>;
   chomp($c);
   if ($c =~ m/[\w]/) {&first_sub;}
   else {&start;}
} 

sub first_sub {
   print "Enter a number between 4 and 70:";
   $i = <STDIN>;
   &first_sub unless $i =~ m/\d/;
   if ($i > 3 && $i < 71) {&second_sub;}
   else {&first_sub;}
}

sub second_sub {
   print "Enter a number between 4 and 20:";
   $n = <STDIN>;
   &second_sub unless $n =~ m/\d/;
   if ($n > 3 && $n < 31) {&third_sub;}
   else {&second_sub;}
}

sub third_sub {
   print "\n\n";
   print $c for (1..$i);
   print "\n";
   for (3..$n){
      print $c;
      #print " " for (3..$i);#print outline
      print $c for (3..$i);#print solid
      print "$c\n";
   }
   print $c for (1..$i);
   print "\n\n";
   print "Want to try again? (y/n):";
   $c = <STDIN>;
   chomp($c);
   if ($c =~ m/^y/i) {&start;}
   else {print "Goodbye!";exit(0);}
}
I hope you are learning by this examples and not just plagarizing the code.

Last edited by Millennium; 12-01-03 at 12:39 AM.
Reply With Quote
  #4 (permalink)  
Old 12-01-03, 01:40 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
note

this line:

if ($n > 3 && $n < 31) {&third_sub;}

should be:

if ($n > 3 && $n < 21) {&third_sub;}
Reply With Quote
  #5 (permalink)  
Old 12-01-03, 03:34 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
And in the spirit of TIMTOWTDI in Perl:

Code:
#!/usr/bin/perl -w
use strict;

my $char = 0;
my $x = 0;
my $y = 0;

get_input();
print_box();
print_outline();

sub get_input {
  while ($char !~ /^[a-zA-Z]$/) {
    print "$/Enter a character: ";
    chomp($char = <STDIN>);
  }
  while ($x < 4 || $x > 70) {
    print "Enter a number between 4 and 70: ";
    chomp($x = <STDIN>);
  }
  while ($y < 4 || $y > 20) {
    print "Enter a number between 4 and 20: ";
    chomp($y = <STDIN>);
  }
}

sub print_box {
  print $/, $/;
  print $char x $x, $/ for (1 .. $y);
  print $/, $/;
}

sub print_outline {
  print $/, $/;
  print $char x $x, $/;
  print $char, ' ' x ($x - 2), $char, $/ for (1 .. $y - 2);
  print $char x $x, $/;
  print $/, $/;
}
~Charlie
Reply With Quote
  #6 (permalink)  
Old 12-03-03, 03:20 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
maybe the teacher is not the only deadbeat
Reply With Quote
  #7 (permalink)  
Old 12-03-03, 08:49 PM
dcass dcass is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
i don't suppose your taking this class from ltc? if not i've got a similar deadbeat teacher. that problem sounds real similar to one i had in ch 2 of e.b.chang book. if this is the case, you won't need to be using subroutines yet. that's in later chapter.

here's my answer to a:

#!/usr/bin/perl
# Exercise 2.10a

print "This program will create a rectangle given width,\n",
"height and character to use.\n";

# Get width and height from user, verify they will work.
do { #get the width
print "Enter a width between 1 and 80.\n";
chomp($w=<STDIN>);
}
until (0 < $w) and ($w <= 80);
do { #get the height
print "Enter a height between 1 and 24.\n";
chomp($h=<STDIN>);
}
until (0 < $h) and ($h <= 24);
print "Enter a character to use.";
chomp($char=<STDIN>);

# Create the rectangle.
$y=1;
do { #create rows
$x=1;
do { #create columns
print $char;
++$x;
}
until $x == ($w + 1);
print "\n";
++$y;
}
until $y == ($h + 1);


here's b:

#!/usr/bin/perl
# Exercise 2.10b

print "This program will create a rectangle given width,\n",
"height and character to use.\n";

# Get width and height from user, verify they will work.
do { #get the width
print "Enter a width between 1 and 80.\n";
chomp($w=<STDIN>);
}
until (0 < $w) and ($w <= 80);
do { #get the height
print "Enter a height between 1 and 24.\n";
chomp($h=<STDIN>);
}
until (0 < $h) and ($h <= 24);
print "Enter a character to use.";
chomp($char=<STDIN>);

# Create the rectangle outline.

#create top row
$x=1;
do { #create columns
print $char;
++$x;
}
until $x == ($w + 1);
print "\n";

#create middle rows
if ($h > 2) {
$y=1;
$b=" ";
do {
print $char;
++$y;
if ($w > 2) {
$x=1;
do {
print $b;
++$x;
}
until $x == ($w - 1);
print $char
}
print "\n";
}
until $y == ($h - 1);
}

#create bottom row
$x=1;
if ($h > 1) {
do { #create columns
print $char;
++$x;
}
until $x == ($w + 1);
print "\n";
}

get's kind of lengthy without subs
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl newbie has a problem... fowler23 Perl 4 03-24-10 05:14 PM
Includes in Perl digitalsea Perl 12 08-23-04 01:18 PM
Running Perl scripts under Microsoft IIS retrocom Perl 3 08-23-03 04:38 PM
looking for class registration script KingSky PHP 4 06-27-03 01:44 PM


All times are GMT -5. The time now is 04:47 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.