Assignment P2

Perl Assignment P2


N.B. These exercises come from Learning Perl.

  1. Write a program that computes the circumference of a circle with a radius of 12.5. The circumference is 2 times pi times the radius, where pi = 3.141592654.

  2. Modify the above program to prompt for and accept a radius from the person running the program.

  3. Write a program that prompts for and reads two numbers, and prints out the result of multiplying the two numbers together.

  4. Write a program that reads a list of strings and then selects and prints a random string from the list. To select a random element of @somearray, put the statement srand; at the beginning of your program to initialize the random number generator, and then call rand(@somearray) to return a value from 0 to n-1 where n is the length of @somearray.

    Addendum: I just realized that Perl's 'rand' function returns a real (i.e. fractional) number, and obviously in this case you don't want values like 1.897453. You can turn those into integers using 'int'. Here's an example:

          #!/usr/local/bin/perl -w
          srand;
          $firstnum = int(rand(4));
          print "A random number between 0 and 3 is: $firstnum\n";
          $secondnum = int(rand(4));
          print "Another random number between 0 and 3 is: $secondnum\n";
        
    Notice the call to srand to initialize the random number generator.

  5. Write a program that asks for the temperature outside, prints "too hot" if the temperature is above 75, prints "too cold" if it's below 68, and "just right!" otherwise.

  6. Write a program that reads a list of numbers (on separate lines) until the number 999 is read, where 999 is a signal that indicates the user is done entering numbers. The program should print out a total of all the numbers added together (ignoring the 999!). For example, if the user entered 1, 2, 3, followed by 999, the program should say that the answer is 6 (i.e. 1+2+3).