CS107 - Lab 1

In this lab you will cover:

The reading for this lab is Chapter 1 in the textbook.

Getting used to the computer

Login to your machine and pick your favorite web browser (a browser is a program that can access documents on the World Wide Web written in HTML) --- Safari, Netscape, Internet Explorer. Note: listed in my order of preference :). You may be used to Internet Explorer, but do try and use Safari for this class---it is the default Mac OS X web browser.

Access the class webpage at http://www.bowdoin.edu/~ltoma/teaching/cs107/spring05/. Browse over the syllabus and the course information, and follow the link to Lab 1. You will be reading this page.

When you log onto the machine, you will see a Macintosh HD folder (corresponding to the local hard disk), and two network folders, one called CLASS'XX (your class), and the other called your-login-name (your home directory on the Bowdoin network). Both these folders should be mounted automatically when you login (you will be asked twice for your password; if you do not enter the password the second time the two folders listed above will not be mounted).

Open a new browser window and open the URL http://www.google.com. Google is computer scientists' favorite search engine. It was developed by two graduate students at Stanford. Now Google is a big company. Try searching for something by typing in some keywords. Everybody likes Google because it is very good at finding information (one of the first few hits is typically what you were looking for).

Using basic Unix commands

Open a Terminal, from you MacintoshHD->Application->Utilities->Terminal. You will get a window and a prompt that looks like this:

bash-2.05a$

or like this

imac213:ltoma%

This is the operating system prompt. On the Macintosh machines in this lab, the operating system is Mac OS X, which is built on top of Unix FreeBSD. Therefore, in the terminal window you can type Unix commands.

Everything you type at the prompt is interpreted as a command and the operating system tries to execute it. If it can, you will see the result, otherwise you will get an error message.

In the terminal window you are working within a current directory (folder). The default directory when you open the terminal is your home directory in this lab (yes, it is a bit confusing that you have two home directories, one on the network and one on the macs, but...hopefully in the future Bowdoin will have a single file system). You will be able to access this home directory from anywhere in the lab. To find out the location of your home directory type:

pwd

To find out what are the current contents of your home directory type ls (comes from list):

ls

We will be using little unix in this class. However, if you would like to learn more about the tree-structure of unix file system and commonly used Unix commands talk to me.

Creating a web page

You can find instructions for setting up your Bowdoin web page here, or follow the steps below.

To create a web page you will need to create in your network home directory a folder (directory) called public_html and inside it a file called index.html or welcome.html. This file will be your "homepage". You should be able to access its contents at http://www.bowdoin.edu/~your-login-name/.

To create this file you need to remotely login onto the Unix server, called polar. To do this, in the terminal window, at the unix prompt, type:

man ssh

The man command displays the manual help page of the command the follows after man. You can use this command whenever you want to find out what a unix command does or how to use it. Read briefly through the ssh man page, and try to get an idea of what it does. Basically ssh lets you login (connect) on a remote machine, securely (ssh stands for secure shell). It is important that the connection is secure, this meaning that the entire communication between you and the remote machine (host) will be encrypted and therefore nobody else can "listen" to it. To connect remotely to polar type at the unix prompt:

$ssh polar

and enter your login name and password. You should get a similar Unix prompt on polar:

polar>

You are now in your network home directory on polar. This is the directory mounted on your desktop. To check this, at the polar prompt, type

polar>ls

and you should see the exact same content as in the folder your-login-name that is mounted on your desktop.

To create the default webpage: at the polar prompt, type

polar> websetup

and follow the instructions. Basically, websetup is a program written by the IT people at Bowdoin that sets up your default initial homepage welcome.html automatically. Look at the default web page that you have just made by typing in the web browser http://www.bowdoin.edu/~your-login-name/.

You can now start modifying your page to make it look the way you like it. To do this, you will need to edit the file welcome.html using a text editor. Modify it, save it, and then re-load the page in the browser to see the changes. A basic file will look something like this. You can use Safari->View->ViewSource from the browser's menu to see precisely how an html file looks like.

Your task for the first lab is to figure out the basics of the html language by looking at the source of this. Which part centers the text? Which part writes the title on the bar? Which part is for the image? To see if your answers are right, edit your welcome.html, save it and re-load it in the browser. The goal is to create a simple webpage which contains some information about yourself and includes a photo. You will update the homepage throughout the semester. When you are done send me an email with the address of your website.

Algorithmmic problem solving

  1. [Problem 4/chapter 1:] Write an algorithm for adding two n-digit numbers discussed in class so that it does not print out nonsignificant leading zeroes; that is 149+029 would give answer 178 rather than 0178.

  2. [Problem 6/chapter 1] Below are two shampooing algorithms. Which do you think is a better general-purpose solution? Why? (Hint: What if you wanted to wash your hair 1000 times?)
    Algorithm 1:
    1. wet hair
    2. set value of washCount to 0
    3. repeat steps  4 through 6 until the value of washcount equals 2
        4. lather hair
        5. rinse hair
        6. add 1 to the value of washCount
    7. stop
    
    Algorithm 2:
    1. wet hair
    2. lather hair
    3. rinse hair
    4. lather hair
    5. rinse hair
    5. stop
    

  3. [Problem 7/chapter 1] Here is Euclid's 2300-year-old algorithm for finding teh greatest common divisor of two positive integers I and J.
    Euclid's algorithm:
    
    1. Get two positive integers as input. Call the larger value I and the
    smaller value J.  
    2. Divide I by J, and call the remainder R.  
    3. If R is not 0, then reset I to the value of J, reset J to the value
    of R, and go back to step 2. 
    4. Print out the answer, which is the value of J. 
    5. Stop.
    
    a).Go through this algorithm using the input values 20 and 32. After each step of the algorithm is completed, give the values of I, J and R. Determine teh final output of the algorithm.

    b) Does teh algorithm work correctly when the two inputs are 0 and 32? Describe exactly what happens, and modify the algorithm so that it gives an appropriate error message.

  4. [Problem 8/chapter 1] A salesperson wants to visit 25 cities while minimizing the total number of miles she has to drive. Because she has studied computer science, she decides to design an algorithm to determine the optimal order in which to visit the cities to (1) keep her driving distance to a minimum and (2) visit each city exactly once. The algorithm she devised is the following:
    The computer would first list all possible ways to visit the 25
    cities and then, for each one, determine the total mileage associated
    withthat particular ordering (assume the computer has accessto a
    roadmap that provides the distances between all cities). After
    determining the total mileage for each possible trip, the computer
    would search for the ordering with the minimum mileage and print out
    the list of cities on tha toptimal route, that is, the order in which
    the salesperson should visit her destinations.  
    
    If a computer could analyze 10,000,000 separate paths per second, how long would it take teh computer to determine teh optimal route for visiting these 25 cities? On the basis of your answer, do you think this is a feasible algorithm?

  5. One way to do multiplication is by repeated addition. For example, 47 x 25 can be evaluated as 47 + 47+47+...+47 (25 times). Sketch out an algorithm for multiplying two positive numbers a and b by using this technique.

  6. [Problem 1/chapter 2] Write pseudocode instructions to carry out each of the following operations:

    a) Determine the area of a triangle given values for the base b and the height h.

    b) Compute the interest earned in one year given the starting account balance B and the annual interest rate I and assuming simple interest, that is, no compounding. Also determine the final balance at the end of the year.

    c) Determine th eflying time beween two cities given the mileage M between them and the average speed of the airplane.

  7. [Problem 3/chapter 2] Write an algorithm that inputs 4 numbers corresponding to the scores received on three semester tests and a final examination. Your algorithm should compute and display the average of all four tests, weighting the final exam wtice as heaavily as a regular test.

  8. [Problem 4/chapter 2] Write an algorithm that inputs the length and width of a carpet in feet and the price in dollar per square yard. The algorithm should print out the total cost of the carpet, including a 6% sales tax.

  9. [Problem 5/chapter 2] Write an if-then-else instruction to do each of the following:

    a) Compute and display the value x/y if the value of y is not 0. Otherwise, display the massage "Unable to perform division!".

    b) Compute the area and circumference of a circle given the radius r if the radius is greater than or equal to 1.0. Otherwise you should compute only the circumference.

Be sure to justify your answers! You may do this work either by hand or with a word processor (e.g. MS Word). Also, you may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on Tuesday, at the beginning of class. Do your best to write legibly and leave space between problems.

What to turn in:

Also, have your webpage ready by the due date.