Assignments | Class Photo | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 180
Data Structures

Michael Goldwasser

Spring 2012

Dept. of Math & Computer Science


Lab Assignment 00

Topic: A First Glance at C++
Pre-lab Due: Thursday, 19 January 2012, 10:00am
Submission Deadline: Friday, 20 January 2012, 11:59pm


Collaboration Policy

The pre-lab requirement must be completed and submitted individually.

The remainder of the lab activity should be completed working in pairs. One person should submit the result, making sure that both partners' names are clearly identified in that submission.

Please make sure you adhere to the policies on academic integrity in this regard.


Pre-Lab Requirement

If you have not already done so, please fill out the online questionnaire for the course, in order to establish credentials for submitting assignments.

Lab Activity

  1. Open up a console window and change to the working directory of your choice. You may create a new directory with a command such as
    mkdir csci180
    and then change to that directory with the command
    cd csci180

  2. We are providing a directory containing sample C++ source code named gcd.cpp. Copy our directory into your working directory with the following command (note well the final period)
    cp -R /Public/goldwasser/180/labs/lab0 .
    and then change into your copy of the directory as
    cd lab0

    Note: if you are working on your local computer, rather than turing, you may download the necessary file gcd.cpp from this web page.

  3. That directory will contain the file gcd.cpp. You should see it in the listing of your directory with the command
    ls

    View the source code by opening the file with a text editor. A simple text editor on turing is called kate. You may either type kate gcd.cpp in the console to start the program or look for it in the "Utilities" folder of the "K" menu at the bottom-left corner of your workspace. If you want a more advanced text editor, my personal favorite is named emacs.

  4. Our next goal is to compile the source code. We recommend doing this with the command
    make gcd

    make is not actually the C++ compiler, but a convenient utility that assits when building applications. When you execute that command, it invokes the actual compiler with a command such as
    g++ gcd.cpp -o gcd

    If you work on a system other than turing and you do not have make installed, you can compile your code by directly invoking g++ as shown above. Our reasons for prefer the use of make are that it is a simpler syntax and that make can better handle complex builds when we start to use multiple source files. Make is also clever in that it won't recompile if the source code has not been modified since the last build. For example, type make gcd again and see how it responds.

  5. After a successful compilation, the output of the compiler will be an executable named gcd (that file name was dictated with the syntax -o gcd when g++ was invoked).

    You should be able to see that file in the listing of your directory with the command
    ls

  6. The executable can be run from your working directory using the command
    ./gcd
    try the following interactions

    First value: 30
    Second value: 18
    gcd: 6
    
  7. Use the program to calculate the greatest commond divisor of the values 9772444 and 3297294. Record the result.

To complete this lab, create a text file named lab0.txt with the following format:

Lab 0
Your Name(s)
gcd_result_here
then submit the file lab0.txt using the online submission system from our course web page.

Generally, when working on pair assignments, only one person should submit the solution (but with both names noted in the file). However, since one goal of this lab is to have everyone test out the online submission system, we ask that each student submit for this lab.


Extra time?

Open the source code and try the following.
  1. Go to line 7 and insert the characters // at the beginning of the line (turning that line into a comment). Now go back to the console and type
    make gcd
    What is the compiler's complaint?

    Uncomment line 7 to restore the original file.

  2. Delete the first int that begins line 4 and try to rebuild the program. What is the compiler's complaint?

    Restore line 4 to its original form.

  3. Go to line 19 and change the characters >> to <<. What is the compiler's complaint?

    Restore line 19 to its original form.

  4. Go to line 8 and remove the { character near the end of the line and try again to make the project. This time, the compiler complains about line 13. Why?

    Restore the { at line 8 before continuing.

  5. Comment out line 13 (by prefixing it with // and try to rebuild. This time, the make succeeds. Rerun the resulting executable and recalculate the gcd of 30 and 18. What happened?

    Restore line 13 to its original form.

  6. Experiment with other changes to the source code and see what happens.


Michael Goldwasser
CSCI 180, Spring 2012
Last modified: Thursday, 19 January 2012
Assignments | Class Photo | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit