Lab 1:  Compiling and runnning C++ programs

Lab 1 - Aug. 29

  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
    and then change to that directory with the command
  2. Next, you need to begin creating the file gcd.cpp. Following is a snapshot of Figure 1 from Goldwasser and Letscher's Transition Guide from Python 2.x to C++:

    This figure shows the equivalent Python and C++ code for computing the greatest common denominator (gcd). You've likely seen the python code before; now we'll begin creating the C++ version.

  3. You may use any editor you desire (e.g. geany, kate, vi, emacs, etc.), but one easy one to get started with is kate. To begin creating the file, you can begin editing a new file for gcd.cpp with the command:

    Go ahead and begin typing in the code for the C++ version. Remember to be careful with upper- and lower-case, as C++ is case-sensitive. Also be sure to be careful with all syntax, e.g. { } ; ( ) << >> etc.

    Depending upon your comfort with typing in the code, you may want to enter the code in parts, compile it (to verify correctness), and then enter more code. The best way to do this is to enter the procedure declarations first with empty code bodies, compile, then enter the first level code bodies, compile, and then finally the second level code bodies (i.e. the while loop's code body), and compile. See below for the commands for compiling.

  4. That directory will contain the file gcd.cpp. You should see it in the listing of your directory with the command
  5. Our next goal is to compile the source code. We recommend doing this with the command
    Alternatively, you can also type make gcd; make is a program that compiles C++ programs for you which we will use throughout the semester. If you work on a system other than hopper 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
    again and see how it responds.
  6. 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
  7. The executable can be run from your working directory using the command

    Try the following interactions
         First value: 30
         Second value: 18
         gcd: 6
    
Now, open the source code again in a text editor 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

    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. Recomplile and use the program to calculate the greatest commond divisor of the values 7854 and 4746.
  7. Recomplile and use the program to calculate the greatest commond divisor of the values 109376842 and 5603859382. Why does this answer strike you as odd?
  8. Finally, email your gcd.cpp file, and answers to each of the above questions, to Dr. Fritts.
  9. You're done!