Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a Perl program that asks the values of the length and width of a rectan

ID: 3589527 • Letter: 1

Question

1. Write a Perl program that asks the values of the length and width of a rectangle and prints the area of the rectangle. (10 points. File name:hw3_1)

2. Write a Perl program that asks a number and prints the sum from 1 to the entered number. (10 points. File name:hw3_2)

Ex) $ Enter a number: 10 $ The sum from 1 to 10 is 55

3. Write a Perl program that asks a series of words (with one word per line) until end-of-input then prints the series of words in an inverse order. (15 points. File name:hw3_3)

Ex) $ Enter a series of words: Hello Apple World Good $ The result is: Good World Apple Hello

4. Write a Perl program that asks a series of words (with one word per line) until end-of-input, prints the above words in ASCII order, and then prints the repeated words. (15 points. File name:hw3_4)

Ex) $ Enter a series of words: Fred Barney Wilma Betty Fred $ The result is: Barney Betty Fred Wilma $ The repeated words: Fred

1. Write a Perl program that asks the values of the length and width of a rectangle and prints the area of the rectangle. (10 points. File name:hw3 1) 2. Write a Perl program that asks a number and prints the sum from 1 to the entered number (1U points. File name:hwš_2) Ex) Enter a number: 10 $ The sum from 1 to 10 is 55 Write a Perl program that asks a series of words (with one word per line) until end-of-input then prints the series of words in an inverse order. (15 points. File name:hw3 3) Ex) $ Enter a series of words Hello Apple World Good $The result is: Good World Apple Hello 4. Write a Perl program that asks a series of words (with one word per line) until end-of-input, prints the above words in ASCIl order, and then prints the repeated words. (15 points. File name:hw3 4) Ex) $ Enter a series of words Fred Barney Wilma Betty Fred $The result is: Barney Betty Fred Wilma $The repeated words Fred

Explanation / Answer

1.hw3_1.pl

print "Please enter the following values for a rectangle ";

print "Length = ";

$l=<>;#takes the length

print "Width = ";

$w=<>;#takes the width

$a=$l*$w;#calculates the area

print "Area of the rectangle is = $a";

2.hw3_2

print "Enter a number ";

$n=<>; #takes the number

$a=1;

$sum=0;

#the loop will execute from 1 to $n+1

while($a<($n+1))

{

$sum=$sum+$a; #calculates the sum in the loop

$a=$a+1;

}

print "The sum from 1 to $n is $sum";

3.hw3_3

print "Enter the series of word ";

$index = 0;

$a = 0;

while ($a != 1)

{

$temp = <>; #takes input from user

if($temp eq " ") #this condition will break the loop when it gets a blank line

{

$a=1;

}

else{

@array[$index] = $temp; #stores the word in the array

$index = $index+1;

  

}  

}

print "The result is ";

#this will print the array in reverse order

for($a=$index-1;$a>=0;$a=$a-1)

{

print "@array[$a] ";

}

4.hw3_4

print "Enter the series of word ";

$index = 0;

$a = 0;

$isUnique = 0;

$r=0;

while ($a != 1)

{

$temp = <>; #takes input from user

  

if($temp eq " ") #this condition will break the loop when it gets a blank line

  

{

$a=1;

  

}

  

else{

@array[$index] = $temp; #stores the word in the array

  

$index = $index+1;

}

}

#elimintes the repeated value

for($a=0;$a<$index;$a=$a+1)

{

  

for($b=$a+1;$b<$index;$b=$b+1)

{

$check = @array[$a] cmp @array[$b];

if($check == 0)

{

@array[$b] = ' ';

$isUnique = 1;

}

}

if($isUnique == 1)

{

@repeat[$r]=@array[$a];

$r=$r+1;

}

  

}

#implementing bubble sort for ordering

for($a=0;$a<$index-1;$a=$a+1)

{

for($b=0;$b<$index-1-$a;$b=$b+1)

{

$check = @array[$b] cmp @array[$b+1];

if($check>0)

{

$temp = @array[$b];

@array[$b] = @array[$b+1];

@array[$b+1]=$temp;

}

}

}

#printing the result

print "The result is ";

for($a=0;$a<$index;$a=$a+1)

{

  

if(@array[$a] ne " ")

{

  

print "@array[$a] ";

}

  

}

print "The repeated words ";

for($a=0;$a<$r;$a=$a+1)

{

  

if(@repeat[$a] ne " ")

  

{

  

print "@repeat[$a] ";

}

}