/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
STOP! Don't start this lab until you have a partner. One of you will work through version A, and the other should work through version B. Don't look at the other version.
When you are ready to proceed, download the following file and open it in a text editor:
lab1.txtYou will record your work in this file and submit it through Canvas at the end of the lab period. You must fill out this worksheet individually. Make sure to introduce yourself to your partner and include his or her name in the appropriate location.
You will be using two different kinds of paper for this exercise. Be careful not to confuse them.
One kind is for output. It contains numbered lines and looks like this:
Each cell on each line can contain exactly one character.
The other kind represents variables. It contains a place for a name and a place for a value, and looks like this:
When generating output, you must always use the normal left-to-right writing rule that is used with English.
Execute the algorithm below:
1. algorithm "drawTriangle" 2. Create a variable named "height". 3. Create a variable named "row". 4. Ask your partner how tall he or she wants the triangle to be. (This should be a number between three and five.) 5. Write the response (in pencil) inside the variable labeled "height" 6. Write the number 1 (in pencil) inside the variable labeled "row" 7. Repeat the following steps as long as the number in "row" is less than number in "height". 8. Look at the contents of the variable named "row". Move your pencil to the leftmost box of the that row. 9. Look at the variable named "row". Write that many "X"'s. 10. Add 1 to the value currently written in the variable "row". Write the result back into "row" (after first erasing the old value). 11. end algorithm
Answer the following questions:
You will write an algorithm for completing each of the tasks below. You are welcome to use the "variables" from the previous exercise, but that is not a requirement. After completing each algorithm, you should exchange seats with your partner (making sure to minimize your web browser so that the task is not visible), and execute your partner's algorithm. When executing the algorithm, you must follow the instructions exactly as they are written.
Write an un-named algorithm for drawing a square that is 2 inches on each side.
Write an un-named algorithm for drawing a generic right triangle (i.e., a right triangle whose size is not determined in advance). The person executing the algorithm should be instructed to ask for the base and height of the right triangle.
Write an un-named algorithm for calculating and displaying the perimeter of a generic rectangle (i.e., a rectangle whose size is not determined in advance). The person executing the algorithm should be instructed to ask for the width and height of the rectangle.
Write an un-named algorithm for calculating and displaying the total combined area of three shapes: a square, an equilateral triangle, and a circle. The person executing the algorithm should be instructed to ask for a single number that will be used for the length of the sides of the square, the length of the sides of the triangle, and the radius of the circle. (Note: You may look up any equations you have forgotten.)