Perl Programming/Exercise 1 Answers

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A. Getting started, displaying text[edit | edit source]

#!/usr/bin/perl
print "Hello World!\n";

B. Displaying Numbers[edit | edit source]

#!/usr/bin/perl

# Begin variable definitions

$numerator = 4000; # This is the numerator
$denomenator = 7; # This is the denominator
$answer1 = $numerator / $denomenator; # Answer to part i
$dec_places=3;

printf ("%.2f",$answer1);

# End variable definitions

print "$numerator divided by $denomenator is: $answer1\n";

$mod_num = $numerator * 10**$dec_places; # Modified Numerator
$remainder = $mod_num % $denomenator;
$num_div_denom = ($mod_num - $remainder); # $num_div_denom is divisible by denomenator
$no_dec_places = $num_div_denom / $denomenator; # This number has no decimal places
$answer2 = $no_dec_places / 10**$dec_places; # Answer to part ii

print "$numerator divided by $denomenator to 3 decimal places is: $answer2\n";

# Rounds up if the remainder / denominator is > 0.5

$round = $remainder / $denomenator;
if ($round > 0.5) {
	$no_dec_places += 1;
	}

$answer3 = $no_dec_places / 10**$dec_places; # Answer to part iii
print "$numerator divided by $denomenator to 3 decimal places, rounded is: $answer3\n";

print "The number with three leading zeros: ";
print "0" x 3 . "$answer2\n"; # Answer to part iv

if ( $answer1 >= 0 ) {
	print "The number is positive (+): +$answer\n"; # Answer to part v, first part
	}
	else {
		print "The number is negative (-): $answer\n"; # Answer to part v, second part
	}

C. Functions[edit | edit source]

The function:

sub evaluate_delta_and_answer {
        my($x,$y,$z) = @_;
        if ($x != 0) {
                $delta = ($y**2 - (4 * $x * $z));
                if ($delta < 0) {
                        print "b^2-4ac is less than zero. Both roots undefined.\n\n";
                        print "Program Terminated. Goodbye, Dave.\n\n"
                        }
                elsif ($delta == 0) {
                        $root = (0 - $y) /(2 * $x );
                        print "b^2-4ac = 0. There will be only one root: " . $root . "\n\n";
                        print "Goodbye, Dave.\n\n";
                        }
                elsif ($delta > 0) {
                        print "b^2-4ac > 0. There will be two roots.\n\n";
                        $root1 = ((0 - $y) - ($delta)**(0.5)) / (2 * $x);
                        $root2 = ((0 - $y) + ($delta)**(0.5)) / (2 * $x);
                        print "The first root, x1 = " . $root1 . "\n\n";
                        print "The second root, x2 = " . $root2 . "\n\n";
                        print "Goodbye, Dave.\n\n";
                        }
        }
        else {
                print "a = 0. This is not a quadratic function.\n";
                print "Goodbye, Dave.\n";
        }
}

The rest of the program:

print "This program takes three numbers (a, b and c) as coefficients\n";
print "of a quadratic equation, calculates its roots, and displays them\n";
print "on the screen for you.\n\n";
print "Please enter the value of a and press <ENTER>: ";
$a = <STDIN>;
print "\n";
print "Please enter the value of b and press <ENTER>: ";
$b = <STDIN>;
print "\n";
print "Please enter the value of c and press <ENTER>: ";
$c = <STDIN>;
print "\n";

evaluate_delta_and_answer($a,$b,$c);