Name: _____________________ Class: Comp 217
SSN/ID:   _____________________ Section & Group: ____________
Software Design Review


Enter YOUR Information:


Your name:
Your email address:
Your student ID number:

You can use this button to work AFTER you've completed everything.


Lab Objectives

To gain experience with


R1. Becoming familiar with your computer

All modern computers offer their users an interface to their physical, electrical, and digital systems. Your computer's operating system provides fundamental services from behind the scenes. A large part of a computer's work is simply storing and retrieving data so that it can be available to use.

Put simply, the operating system takes care of a lot of the filing work - storing, moving, and remembering where things are - so you can do useful things at a higher level. You interact with the operating system via a Graphic User Interface (GUI), for example a windowing system using a pointing device like a mouse, or a Command Line Interface, using only a keyboard and a single window.

What are some examples of tasks you would tell the computer to do via the operating system's command line interface?

Operations like browsing the web and editing text in a word processor - most of the things you'd have the computer do - involve your operating system executing some program. The operating system itself is a program which is running all the time. It executes your instructions and, in turn, it can run other programs.

It is useful to think of a program as a sequence of instructions. Both executable instructions and digital data can be represented as files, for example, on your hard drive. Both are sequences of symbols, just like the letters that make up the words in this sentence. Your job as a programmer is of course to provide the instructions that operate on the data.

We will begin to get some experience with the operating system by finding a file. We will locate cmath, an include file used by C++. If you get stuck, please feel free to ask for help (but ask your colleagues first!).

What did you do to find cmath?

How did you read the contents of math?

The program that you will be running to write computer programs is your text editor, which often is part of an integrated compiler environment. Our editor of choice is emacs. Go ahead and start it now from the command line (don't forget to to put it in the background!).

Again, what did you do?


R2. Compiling and running programs from within your development environment

Frequently in these labs, you will be asked to compile a sample program. Below is a copy of a C++ program that displays a drawing. Copy and paste it into your compiler's editor, and from there save it as art.cpp.

Describe what you did.

/*
  PURPOSE: Display an 'art' drawing
*/            

#include <iostream>
#include <string>
using namespace std;

int main() {
   string s1 = " *   *   *   *   *   * ";
   string s2 = "   *   *   *   *   *   ";
   string s3 = "__________________________________\n";
   string s4 = "_________________________________________________________\n";   

   cout << s4 << s1 << s3 << s2 << s3;
   cout << s1 << s3 << s2 << s3;
   cout << s1 << s3 << s2 << s3;
   cout << s1 << s3 << s2 << s3;
   cout << s4 << s4 << s4 << s4 << s4;

   return 0;
}

Once you have typed in (or, in this case, pasted in) a program, you need to to compile it to to create an executable file. Again, these steps depend on your compilation environment. In our case, we use the GNU Compiler Collection's g++ as our compiler of choice. Go ahead and compile art.cpp to an executable file now.

Describe what you did.

Finally, execute the program. Once again, the steps depend on your computer system. On a Unix-like system like Linux, we use the command line interface (via the terminal shell) to execute the compiled programs we create. Go ahead and execute your compiled program now.

Describe what you did to execute the program.

Describe what happened when the program executed.


P1. Writing simple programs

Your initial C++ programs will be contained entirely in one file and there are some elements that they all will have because of requirements of the C++ language. Essentially, these are:

  1. Directives to the compiler that alter its behaviour and capabilities (include pre-processor directives, using directives, etc.).
  2. Markers for the compiler - to know where your program begins and ends.
  3. Files that your program also needs in order to operate.
When you build a program, your compiler looks for code of the form:

#include <iostream>
using namespace std;

int main() {

   /*
      your work goes here
   */
   
   return 0;
}   

The textbook has a program that prints the message Hello, World! on the screen.

Try changing your program now to display Hello, Universe!

Type the program into your compiler's editor, compile it, and test it. Then paste the source code in the following text box.


P2. Detecting syntax and logic errors

There are numerous opportunities for error in any program, many times in places that seem too simple to require close attention. What do you think the following program is supposed to do?

// Volume: (4/3) PI R^3; Surface Area: 4 PI R^2
#include <iostream>
using namespace std;

int main() {
   double radius = 1;  /* centimeters
   double pi  = 3.14
   double sphere_volume = (4/3)*pi*(radius * radius ** radius);
   double surface_area = 5 * pi * radius;
   cout << "Sphere Volume = " < sphere_volume;
   cout << "Sphere Area = " << surface_area;
   return 0;
}   

Will it work as it is?

Try compiling the program. What were the results? (Use copy and paste to place a copy of your compiler's error messages here.)

Fix the syntax errors. Place a copy of your program's output here.

The program has two logic errors. Fix them both and paste the corrected program here.


R3. Algorithms

An algorithm is a recipe to carry out a task. To be programmable, such a recipe must be

While an algorithm will eventually be expressed in a particular programming language, it is frequently helpful to describe its operation in natural language, before undertaking the task of coding it up. Suppose for example that you would like to calculate your future salary, knowing that you will receive a 3% raise every year.  If you made $20,000 this year, your salary next year would be: $20,600 (which is $20,000 + ($20,000 * .03)) and $21,218 the year after ($20,600 + ($20,600 * .03)), $21,854.54 the year after($21,218 + ($21,218 * .03)), etc.

Describe an algorithm (in pseudocode) to do this computation for a salary of $30,000 after any number of years. Your algorithm should be so detailed that anyone can carry out the steps and arrive at the correct answer. Please refer to the text, lecture handouts, and programming stylesheet for more on pseudocode (TODO: Add description here).


P3. Additional Programming Practice

Often you will find that a program can be written by modifying another program that you have written before. For the calculation that we just wrote, the algorithm can be generalized as follows:

P = C (1 + r)

Where P is the final pay rate, C is the starting pay rate, r is the percentage increase (expressed as a decimal), and t is the number of years. So, for example, to compute your salary after 5 years, starting at $20,000 with 3% increases per year, we would get:

P = 20,000 (1 + .03)5 = 23,185.48 

Now, please write a valid C++ program based on the pseudocode you wrote in the previous section (P2) to calculate your pay after being with a company for 10 years.  Assume that you had started with a salary of $18,000 and that you receive a 3% raise every year. Paste your program here:

Now, compile it and execute it.  Paste your output in the space below:


Don't forget to send your answers when you are finished.

Hand In: Submit your answers when you are finished.


Ricky J. Sethi <rickys at sethi.org>
Last modified: Tue Apr 25 16:02:18 PDT 2006