CS 110X Jan 22 2014 : LAB ONE
Dare to be honest and fear no labor.
Robert Burns, poet
1 Nothing worth having was ever achieved without hard work
1.1 Homework Partner Pairs
First take a few minutes to locate your name on the programming partner list. The pairs have now been assigned. I may have missed a few students, because of add/drop requests and other issues, so please contact Professor Heineman if you do not find yourself on the list.
On Friday Jan 24 2014 this list will become final.
The partners were assigned based upon a number of factors. If two teams would like to suggest a "straight swap" that is easy to accommodate. If you know of anyone who has dropped the course, please let me know.
1.2 Lab Assignment
This first lab will put to the test the knowledge and skills you have learned in class and prepare you for the lecture on Jan 23 2014.
Given a square with side S, what is the area of the inner rectangle identified below given length a (a number between 0 and S)?
The area of this inner rectangle is equal to the
product of lengths c * e as identified by the above image. You are
given the known facts on the right hand side of the above image that
describe how to compute the various identified lengths.
With this information in hand, you can compute the area of the inner
rectangle, given S and a.
Think about the problem for a bit. Starting with just S and a you can compute each of the intermediate values of b, c, d and e.
You will need to write a function that performs the computation.
1.3 Structure
Your Python module should be called "lab1.py" and you should store this file someplace where you can retrieve it later. I recommend you store it on your \\filer.wpi.edu folder.
Run IDLE and open a New Window in which to program. The structure of your program should be as follows:
# Lab 1 # Author: <<Your Name>> def innerArea(S, a): print ("Replace With Computation")
Once you have completed this innerArea function, the output should look like the following when invoked from within the Python Shell window:
>>> innerArea(1, 0.5) S=1 a=0.5 Area=0.46875
That is, the output must appear on a single line by itself. Do not continue until you have a working innerArea method that computes the above value properly.
1.4 Completing Lab
The rest of this lab prepares you for tomorrow’s class. Now that you have properly written the implementation, you may wonder how the area changes as a and S vary. Now, you could try to manually try different values of a and S (as you did for the WindChill question on HW1) but instead you should automate this process.
To accomplish that task you need to learn a new controlstructure from Python.
1.5 Definite For loop
Be sure to leave a blank line after your innerArea function so it remains a distinct function from bigSignature.
def bigSignature(): print ("Your name here") print ("Your name here") print ("Your name here") print ("Your name here") print ("Your name here")
Run this function to verify it works. How do you do this? Simply run the module lab1.py on which you are working. When you see the ===== RESTART ====== prompt, type bigSignature() at the >>> prompt.
While this works, it sure doesn’t feel right! What if you had to print your name ten or a hundred times? Instead, you need a way to repeat a body of statements a fixed number of times.
Replace the above code with the following and demonstrate to yourself that it runs and produces the same output.
def bigSignature(): for index in range(5): print ("Your name here")
Note that you have to properly indent, once for the function definition, and once more for the for statement.
First run this function to verify that the output is the same. Now look at the code and try to "read it". For example, change the "5" to a "3" and I bet it would only print your name three times.
This statement is known as the definite for loop
1.6 Definite For loop executes body of statements
Just to be clear that you follow the logic, modify the bigSignature function as follows:
def bigSignature(): for index in range(5): print ("Hi") print ("Your name here")
Save this revised module and run it. The terminal will RESTART and when you invoke the bigSignature() function you will see that these two statements are executed "as a body" that is repeated five times. Thus the total output consists of ten lines of output, alternating between "Hi" and "Your name here".
1.7 What is purpose of index variable?
The for statement requires a variable that is used to iterate over the total number of requested iterations. The examples above defined a variable index for this purpose, but it can be any name that you choose.
The first time the body is executed, the value of index is 0; the second time its value is 1; the third time its value is 2; the fourth time its value is 3; the fifth and final time its value is 4.
Demonstrate this by modifying the function as follows:
def bigSignature(): for index in range(5): print ("Pass " + str(index)) print ("Your name here")
Note that you have to concatenate the index by converting it into a string as described in class yesterday.
Reload the module and invoke bigSignature() one more time. As you can see, the body of statements is executed as before, but this time the value of index is printed with each "Pass" through the loop.
1.8 Review
A solution will be posted tomorrow. If you don’t finish this lab in the allotted time, be sure to save it on your own file systems (either USB drive or the filer.wpi.edu\home file system so you can continue to work on it after the lab has ended.
Note that you should still submit via turnin before the end of the lab so you can receive credit for the lab.
1.9 Advanced ungraded question
Don’t attempt this question until you have used turnin to submit your labe file.
Given what you know, can you write a function areaTable() that produces the following output using the innerArea function you wrote earlier in this lab?
>>> areaTable() S=2 a=1 Area=1.875 S=3 a=2 Area=5.18518518519 S=4 a=3 Area=10.359375 S=5 a=4 Area=17.472 S=6 a=5 Area=26.5509259259 S=7 a=6 Area=37.6093294461 S=8 a=7 Area=50.654296875 S=9 a=8 Area=65.6899862826 S=10 a=9 Area=82.719
1.10 Version : 2014/01/23
(c) 2014, George Heineman