CSE 4208: Studio 0

Intro to Multi-Paradigm Programming


In today's studio you will set up the programming environment you'll be using throughout this semester, make sure everything is working, and then iteratively refine a simple program to use additional features of the C++ language and its libraries.

Throughout the semester, we will use the gcc 8.x version of the g++ compiler and a CMakeLists.txt that tells g++ to enforce the C++17 standard by giving it the -std=c++17 switch on the command line that runs it. If you are interested in learning more about the C++17 features supported by different g++ compiler versions, please see https://gcc.gnu.org/projects/cxx-status.html#cxx17.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload (1) a file containing your answers and (2) a file containing your C++ code (with the modifications you made in the exercises) on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password. On a Mac or Linux computer, you can just run ssh directly from within a terminal window. On many versions of Windows, you can run ssh directly from the command prompt. If the command is not recognized, you will need to install and use an SSH client, such as Putty or WinSCP.

    Be careful! When you connect with ssh, you should specify your username, e.g. ssh username@shell.cec.wustl.edu or you may risk getting locked out of the server if the default username isn't the same as your WUSTL Key username (e.g., on a Raspberry Pi if you took CSE 422S). If you connect with the wrong username or password 5 times in 30 minutes, your IP address may be blocked.

    To help prevent lockouts due to incorrect password attempts, you may want to log in using an SSH key instead of a password, as detailed on the ssh_key_generation.html page.

    Because compiles may be computationally intensive (especially for longer lab assignments later in the semester), you should not run them on the shell.cec.wustl.edu server, and instead you should run:

    qlogin

    which will let you log into a dedicated Linux host using your WUSTL Key id and password.

    As the answer to this exercise, write down the hostname of the linuxlab machine you're using, which you can find out at any time by issuing the command hostname if you didn't write it down immediately when you logged in.

  3. You will now set up your environment (specifically its PATH variable) to use the gcc 8.3.0 version of the g++ compiler, by issuing the following command:

    module add gcc-8.3.0

    Note: You should please also add that command as an individual line at the end of the file ~/.bashrc.

    To confirm that your environment is using the correct version of the g++ compiler, please issue the following commands:

    which g++

    and then

    g++ -v

    and as the answer to this exercise please show the output that each of them produced.

  4. You should now create a directory where you will keep your code for this course this semester (named something like e.g., cse4208_fall25) and cd into that directory and in it create a directory for this studio and cd into it.

    Please upload (e.g., using sftp) the following files into the directory for this studio (e.g., using sftp): studio0.cpp and CMakeLists.txt.

    Issue the command cmake -B build && cmake --build build, which should compile the code in studio0.cpp using the settings specified in the CMakeLists.txt.

    Run the compiled program by issuing the command build/studio0, and as the answer to this exercise please show the output that was produced by compiling the program and by running it.

  5. Arguably, the program is already using procedural programming features (since it has an effect by producing output) and functional programming features (since it returns a result to indicate whether or not it was successful), but in this exercise you will expand a bit more on those.

    Modify the signature of the program's main function so that its input parameters are named argc and argv, indicating the number of command line arguments passed to the program and the arguments themselves, respectively.

    Declare additional non-zero const int variables (with appropriately descriptive names) that the program can return to indicate different ways it can fail based on the further modifications you will make in this exercise and the one that follows it.

    Modify the program so that its main function tests whether any command line arguments were given, and if none were provided prints out a helpful usage message like the following:

    usage: studio0 <arg1> [<arg2> ...]

    and returns an appropriate non-zero error value. Please note that the 0th element in argv contains the name of the executable program, so it is good programming style to use that value rather than hard-coding the program's name as a string in any usage messages.

    Compile your program and run it with no command line arguments and then immediately issue the following command to see what value the program returned:

    echo $?

    Then run your program with one command line argument (after the program name) and immediately check what value it returned. As the answer to this exercise please show the program's output and the value it returned, when run with no arguments vs. when run with one argument.

  6. Modify your program so that instead of writing to the standard output stream it treats each command line argument it is given as the name of a file that it uses an output file steam (ofstream) to open, write the same output as before into, and then close. Declare additional const int variables for any non-zero error values that the program should return if it fails to open any file whose name it was given.

    Test your program with the name of a file that does not exist, and confirm that the program worked correctly by checking the value it returned and then using the cat command to print out the contents of the newly created file.

    Then, use the chmod a-w command to make the file read-only, run your program with the name of that file again, and confirm that the program returns an appropriate error value. As the answer to this exercise, please show the values that were returned by each run of the program, and the contents of the output file after each run.

Things to Turn In:

For this studio, please turn in the following: