CS21 Lab2: Numbers and Strings

Due 11:59pm Tuesday, 2 February

Run update21, if you haven't already, to create the cs21/labs/02. Then cd into your cs21/labs/02 directory and create the python programs for lab 2 in this directory (handin21 looks for your lab 2 assignments in your cs21/labs/02 directory):


$ update21
$ cd cs21/labs/02
$ pwd
  /home/your_user_name/cs21/labs/02

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. Computing volume and surface area of a squard-based pyramid

Write a program, in a file named pyramid.py, that asks the user to enter the length of the base and the height of a pyramid. Your program will then compute and print out the volume (V) and the surface area (A) of the pyramid using these formulas:

(In the diagram above, A is the surface area and V is the volume.)
Here is what two runs of your program might look like:


$ python pyramid.py

Thiss program calculates the volume and surface area of a pyramid.
Please enter the length of the base:  8
Please enter the height:  3
The volume of the pyramid is:  64.0
The surface area the pyramid is:  144.0

$ python pyramid.py


This program calculates the volume and surface area of a pyramid.
Please enter the length of the base:  5
Please enter the height:  4.2
The volume of the pyramid is:  35.0
The surface area the pyramid is:  73.8773976394


Try using string formating to limit the number of places printed beyond the decimal point.

To use math library functions, remember to add this to the top of your program:

from math import *


2. Computing the sum of the first n even integers

Write a program named evensum.py, that takes as input a positive integer value n, and then computes the sum of the first n even integer values. For example, if the user enters 5, your program will compute the sum of 2, 4, 6, 8 and 10.

A couple runs of your program may look like this:


$ python evensum.py
This program computes the sum of the first n even integers
Enter a value for n: 5

the sum of even integers between 2 and 10 is  30

$ python evensum.py
This program computes the sum of the first n even integers
Enter a value for n: 30

the sum of the integers between 2 and 60 is  930
You should computer this sum using a loop.

3. Pattern of stars
Write a program in a file named stars.py, that prompts the user to enter a value for n, and the prints out n rows of a pattern of stars that looks like the following examples:
$ python stars.py
This program prints out a pattern of stars
Enter a value for the size of the pattern: 5

- - - - - * 
- - - - * * 
- - - * * * 
- - * * * * 
- * * * * * 


$ python stars.py
This program prints out a pattern of stars
Enter a value for the size of the pattern: 8

- - - - - - - - * 
- - - - - - - * * 
- - - - - - * * * 
- - - - - * * * * 
- - - - * * * * * 
- - - * * * * * * 
- - * * * * * * * 
- * * * * * * * * 
Hint: start by writing a program that prints out these two patterns of stars, then think about how you can use these solutions to lead you to solving the above problem:
This program prints out a pattern of stars
Enter a value for the size of the pattern: 5

* 
* * 
* * * 
* * * * 
* * * * * 


* * * * * 
* * * * 
* * * 
* * 
* 


Extra Challenge
This is not a required part of the lab 2 assignment, but is an extra challenge that you can try. Only try this after you have completed all the regular problems in lab 2.

Write a program, morestars.py, that prints out the following patterns of stars given user input values for the size of the patterns:

This program prints out a pyramid of stars
Enter a value of for the size of the pyramid: 8

        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
   * * * * * * 
  * * * * * * * 
 * * * * * * * * 

This program prints out a diamond of stars
Enter a value of for the size of the diamond: 5

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 


Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.