/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

Algorithm Lab — Version A

Introduction

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.txt

You 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.

Executing Algorithms

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.

Triangle of X's

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

Algorithm Analysis

Answer the following questions:

  1. Which steps perform input?
  2. Which steps perform output?
  3. Which steps modify the value of a variable?
  4. Which steps involve a mathematical operation?
  5. Which steps perform iteration?
  6. This algorithm contains a "bug": a logic error that results in incorrect output. Where is the problem? How can it be fixed?

Creating Algorithms

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.

Writing Simple Algorithms

  1. Read and understand the following task:

    Write an un-named algorithm for drawing a square that is 2 inches on each side.

  2. Exchange algorithms with your partner.
  3. Without discussing it, execute your partner's algorithm exactly as it is written.
  4. Discuss the problems you had executing your partner's algorithm with your partner.
  5. What was incorrect or imprecise about your algorithm?

Writing Algorithms that Require Input

  1. Read and understand the following task:

    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.

  2. Exchange algorithms with your partner.
  3. Without discussing it, execute your partner's algorithm exactly as it is written.
  4. Discuss the problems you had executing your partner's algorithm with your partner.
  5. What was incorrect or imprecise about your algorithm?

Writing Algorithms that Require Calculations

  1. Read and understand the following task:

    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.

  2. Exchange algorithms with your partner.
  3. Without discussing it, execute your partner's algorithm exactly as it is written.
  4. Discuss the problems you had executing your partner's algorithm with your partner.
  5. What was incorrect or imprecise about your algorithm?

Writing A More Complicated Algorithm

  1. Read and understand the following task:

    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.)

  2. Exchange algorithms with your partner.
  3. Without discussing it, execute your partner's algorithm exactly as it is written.
  4. Discuss the problems you had executing your partner's algorithm with your partner.
  5. What was incorrect or imprecise about your algorithm?

Acknowledgments

This lab is based on activities originally developed by David Bernstein.