CMSC 15200 - Summer 2013

Lab 1: Getting Acquainted

Preliminaries

UNIX systems in general, and Linux distributions in particular, usually provide users with two interfaces:
  • Command-line interface(or shell) through the Terminal program: Allows the user to interact with the system through the use of commands which you must type in using the keyboard. There are many different types of shells, such as BASH, CSH, TCSH, ...
  • Graphical interface: Allows the user to interact with the system through the use of graphical elements such as windows, buttons, text fields, etc. mainly using the mouse. Although the taxonomy of graphical interfaces in UNIX can be a bit complex, most new users will generally interact at first with high-level desktop environments such as KDE and GNOME, which are similar in many respects to graphical interfaces in Windows and Mac systems.

In this course, we will use both interfaces. We will compile and execute our C programs within the command-line interface and write our programs using the graphical interface via text editors.


Part I: Getting Started

  1. Login on the Linux machines using your CS account that you created earlier. Press the Applications menu found on the upper-left corner of the screen. Go to Accessories->Terminal. You will be typing most of your commands in this terminal.

    We will be using a project management system called SVN (short for Subversion). SVN is a powerful tool that is useful for backing up versions of your code, collaborating with others, syncing your work across computers, etc. For now, you will primarily use it as a way to submit your work.

  2. Type in the following command into the Terminal:
    svn co https://phoenixforge.cs.uchicago.edu/svn/yourusername-cs152-sum-13
    replacing yourusername by your CNet username in lowercase. You will be prompted for your CNet password. If you get a prompt asking you if you would like to accept the certificate, press p (meaning to accept permanently).

    (For example, since my username is lamonts, I would type svn co https://phoenixforge.cs.uchicago.edu/svn/lamonts-cs152-sum-13.)

    If you registered late or not at all, this step may not work for you. Please raise your hand if you are having trouble. You will need to submit your work through e-mail rather than SVN until your SVN repository is setup.

    What we just did is check out the current version of your personal repository for this class. The repository is stored on a central university server, and is accessible only by you and the course staff. You will use it to save and submit your homeworks and labs.

    If all goes well, the Terminal will respond with Checked out revision 0.

  3. Type these commands, pressing Enter after each one.
    cd yourusername-cs152-sum-13
    ls
    The first command takes you into your project directory, which is yourusername-cs152-sum-13 and the second command lists the directory's contents. (In UNIX-speak, cd is short for "change directory" and ls stands for "list contents".)

    If you are on the right track, the Terminal will tell you that there is nothing in the folder by showing you nothing on the terminal.

  4. Next we will create a folder named lab1. This folder will store our lab1 files. The mkdir command allows you to create folders on the terminal. Perform this by typing on the terminal the command
    mkdir lab1
  5. Now, type in the ls command as before:

    ls
    You should now see the lab1 folder that you just created listed on the terminal.

  6. Although you just created the lab1 folder, you still need to add this folder to your repository that is stored on the central university server. The lab1 is only stored locally on your machine but you need to use svn to upload the folder to the server.

    Type in the following command

    svn add lab1
    and the terminal will respond by showing you,
     A         lab1
    Now you have told svn to add the lab1 folder to your repository.

    Note: Anytime you create a new file,folder, or want to add a file to your repository for the first time you will need to use svn add . You only need to do svn add only once.

  7. Leave this terminal window open for now. We will come back to it later.

Part 2: Creating your first C program

You will write and compile a simple C program that displays a message to the screen.

  1. You will write your programs in any text editor of your choice.( For those of you who are familiar with emacs or vi go ahead!) For those of you who are not familiar with text editors on Linux, we suggest using the default text editor (Press the Applications menu found on the upper-left corner of the screen. Go to Accessories->Text Editor)
  2. Once you opened up a text editor and create a blank file (or one should already appear in the editor), Save the file as lab1.c inside your lab1. directory.
  3. Inside the lab1.c file type the following C code:

    /* FirstName LastName, username */
    /* CS152, Summer 2013 */
    /* Lab 1 */
    
    #include <stdio.h>
    
    int main(void) {
       printf("Yay my first c-program executed\n"); 
       return 0;
    }
    

    Changing FirstName,LastName,username to your actual information.

    Make sure to save the file.

  4. Now go back to the terminal window from earlier and enter the lab1 folder using this command
  5. cd lab1

  6. You will compile your programs using gcc, the GNU C compiler. To compile a file (i.e. lab1.c), type
    $ gcc lab1.c
  7. If the compilation is successful, the compiler will create an executable named a.out. You can run the executable by typing
    $ ./a.out

    Once you run the ./a.out executable, you should see the message:

    Yay my first c-program executed! 
    
    You can specify a different filename other than a.out with the -o flag:
    $ gcc -o lab1 lab1.c
    and then run it with ./lab1. You should see the same message as before.

Remember you need to run this list of commands after saving the program file everytime.


Part 3: Actual Lab Exercise (Scale Conversion)

In an effort to improve relations with metric system countries, the US Government has embarked itself on an ambitious project to develop software that converts temperatures from the Fahrenheit scale to the Celsius scale and vice versa. You must write a C program that asks the user for a temperature, what scale that temperature represents (i.e. 'f' for Fahrenheit or 'c' for Celsius) and then outputs the conversion to the opposite scale. If the user enters in a character other than 'f' or 'c' print a message that says "Invalid temperature scale" and exit the program. Also when displaying the temperatures format them to two decimal places. Modify the lab1.c (i.e. remove the printf message) and write this program in the main function.

Here's an example of how your program should work:

 
Enter in a temperature scale ('f' for Fahrenheit or 'c' for Celsius): c Enter in a temperature: -17.78 -17.78 C = 0 F

Here's another example:

 
Enter in a temperature scale ('f' for Fahrenheit or 'c' for Celsius): f Enter in a temperature: 89.36 89.36 F = 31.87 C

Hint: Here are the formula's for doing the converisions:

°F = °C  x  9/5 + 32

°C = (°F  -  32)  x  5/9

Once completed, save the file and follow the steps in Part 2 to compile and test your program.


Part 4: Committing your work

  1. Anytime you make changes to your repository (i.e. adding new or modified files/folders) you need to svn commit your changes. The command svn add did not save your lab1 folder to you repository. It only told svn that when you perform a svn commit that these files and folders need to be saved to your repository.
  2. Before committing, you need to svn add your lab1.c to your repository. Follow the step before when you added your lab1 directory but now you want to add your lab1.c file.
  3. Note: ONLY add the .c files or to your repository for all labs and homeworks unless otherwise noted!

  4. Now, submit your lab1 folder and your lab1.c to your svn repository. To do so, you need to go back to your main folder named "yourusername-cs106" by typing in
    cd ..

    The .. means go up one level (i.e. go back up one directory)

  5. Now, Type in
    svn commit -m "Commiting the lab1 directory and files"
    This uploads your current work into the repository. The string after the -m flag is a message that explains your commit, and can be anything you like. Meaningful messages help you keep track of any additions and modifications to your code at each commit.

    You should get a message saying

    Adding lab1
    Adding lab1/lab1.c
    Transmitting file data .
    Committed revision 1.

    You have completed the lab! Call me over to show me your lab for credit.

So What's Next?

We encourage you now to start working on your HW#1 assignment while you have teaching staff present. Alternatively you can go outside and enjoy the sun (hopefully it's out!).