Practice exercises for COMP 364 - Lecture 9 - Jan 21

Exercise: Even or odd

Write a Perl program that takes a number as input and prints out whether the number is even or odd. For example, a run of your program may look like:
[perkins][lab2-2][~/Lecture7] perl EvenOrOdd.pl
Enter your number: 3
The number 3 is odd.
[perkins][lab2-2][~/Lecture7]
You may want to use the % operator in your program, though there are ways of figuring out whether a number is even or odd without using %.

Exercise: Counting down

In class, we wrote a program called Countdown.pl that took a number as input and counted down from that number to zero. The program we wrote in class was essentially:
$Count = <>;
chomp $Count;
while ( $Count >= 0 ) {
  print "$Count\n";
  $Count--;
}
Consider a slightly different version of this program, which we'll call version 2:
$Count = <>;
chomp $Count;
while ( $Count > 0 ) {
  print "$Count\n";
  $Count--;
}
Or consider another variant, which we'll call version 3:
$Count = <>;
chomp $Count;
while ( $Count > 0 ) {
  $Count--;
  print "$Count\n";
}
How do the outputs of these three versions of the program differ? You may want to enter these programs and run them on your computer. But you may also want to try running them "in your head" -- that is, think through how the programs would run and what output they would produce for different inputs. In particular, think how the different versions would run if the user entered different values for $Count. What do they output if $Count starts at 0? At 1? At 2? Etc.

Exercise: Quadratic formula

Recall that a quadratic equation looks like ax2 + bx + c = 0. If we assume that a, b and c are known, but x is unknown, we may want to know which values of x satisfy the equation (that is, which values of x make the equation true). Usually, there are two value of x for which the equation is true, and the quadratic formula tells what they are:

(-b + sqrt(b2 - 4ac)) / 2a

and

(-b - sqrt(b2 - 4ac)) / 2a

In these to formulas, sqrt indicates computing the square root. Write a program that asks the user to input values for a, b and c, computes the two values of x that satisfy the quadratic equation ax2 + bx + c = 0. For simplicity, you may assume that b2 - 4ac is greater than or equal to zero. (Otherwise, the solutions are complex numbers, not reals, and Perl will complain when you try to take the square root.) For example, a run of the program might look like:

[perkins][lab2-2][~/Lecture7] perl Quad.pl
Enter a: 1
Enter b: -6
Enter c: 8
The values of x that satisfy the equation are 4 and 2.
[perkins][lab2-2][~/Lecture7]
P.S. You can check whether your program has computed the correct possibilities for x by substituting them back into the formula ax2 + bx + c and checking that it comes out to zero.

Exercise: Quadratic formula, take two

In the previous exercise, if b2 - 4ac is negative, no real-valued x can satisfy the equation. Instead, a complex x is needed -- one with real and imaginary parts. (Recall that a complex number x can be written as y + zi, where y and z are real numbers and i is the square root of -1.) When b2 - 4ac is negative, the two solutions to the quadratic equation can be written as:

(-b / 2a) + (sqrt(-b2 + 4ac) / 2a)i

and

(-b / 2a) - (sqrt(-b2 + 4ac) / 2a)i

Write a program that asks the user to input values for a, b and c. If b2 - 4ac is greater than or equal to zero, output the two real values of x that solve the quadratic equation. If b2 - 4ac is negative, output the two complex values of x that solve the equation. For example, a run of the program might look like:

[perkins][lab2-2][~/Lecture7] perl Quad2.pl
Enter a: 1
Enter b: -6
Enter c: 8
The values of x that satisfy the equation are 4 and 2.
[perkins][lab2-2][~/Lecture7] perl Quad2.pl
Enter a: 1
Enter b: -2
Enter c: 2
The values of x that satisfy the equation are 1+1i and 1-1i.
[perkins][lab2-2][~/Lecture7]

Exercise: Print a row of *'s

Write a program that takes a number as input and prints out a row of that many * characters on one line. For example, a run of your program might look like:
[perkins][lab2-2][~/Lecture7] perl StarRow.pl
How many stars? 10
**********
[perkins][lab2-2][~/Lecture7] 

Exercise: Print a triangle of *'s

Write a program that takes a number as input and prints out a triangle of stars as shown below.
[perkins][lab2-2][~/Lecture7] perl StarTriangle.pl
Enter a number? 10
*
**
***
****
*****
******
*******
********
*********
**********
[perkins][lab2-2][~/Lecture7]