Lab 1: Getting Started with Linux and C

This lab assumes that you have read the assigned chapters 1 and 2. Please bring your book to lab, as you will need it as a reference.

The objectives for this lab are:

  • Learn basic Unix skills in navigating the filesystem and editing files.
  • Compile and run your first C program.
  • Demonstrate your understanding of basic I/O functions and mathematical expressions in C.
  • Getting Started with Linux

    Open a terminal window on your screen. This is probably the menu item Applications->System Tools->Terminal, but may differ between various kinds of Linux. Inside the terminal window, you will see the shell program, which prints a prompt and waits for you to type a command. The TA will present an overview of useful Linux commands in lab session one.

    To get started, you can follow this recipe. First create a directory for this course, then a sub-directory for lab one:

    mkdir cse20211
    mkdir cse20211/lab1
    Now, change directory (cd) into the newly created directory.
    cd cse20211/lab1
    In this class, we will show you how to use the nano editor, because it is easy to explain, and easy to use when remotely connected. Most Linux systems have several editors, such as gedit, emacs, vi, which you are welcome to figure out and use on your own. To create a text file called myfile.txt, run this command:
    nano myfile.txt
    The nano editor will start, and show you a mostly blank screen with one title line at the top, and a menu of options at the bottom. Type about five lines of text (anything you like) into the editor. Now, look at the menu at the bottom, where you should see items like ^X Exit, ^K Cut Text and ^U UnCut Text. The ^ (caret) symbol stands for the Control key, so you press Control-X to exit, Control-K to cut text, and so forth. Come back and experiment with the various control keys later. Now, press Control-X to save your file and exit. nano will ask you to confirm and give you the option to change the file name.

    Now, you are back at the terminal prompt. You can use the ls command, which lists the files in the current directory:

    Adding the -l option will show more details about the file:
    ls -l
    The mv command will move (or rename) a file. Try renaming your file, and then listing the directory again, like this:
    mv myfile.txt newfile.txt
    ls -l
    The cp command will copy a file under a new name. Try copying like this:
    cp newfile.txt newfile2.txt
    ls -l
    Finally, the rm command will remove (delete) a file. Try this:
    rm newfile2.txt
    ls -l
    You now know about the following Linux commands: mkdir, cd, ls, mv, cp, and nano. That's enough to get started on most of the assignments in this class. There is much more to learn about Linux, but you will pick it up week by week with practice.

    Part 1: Hello World

  • Use nano to create a file called prog1.c. Enter in the source code of figure 2.1 in the Deitel book, and save it.
  • Compile with the following command: gcc prog1.c -o prog1. If there are any errors, go back and make sure you entered the code correctly. If the compile was successful, there will be no output.
  • Notice that a successful compile will create the file prog1. Use ls -l to see that it is there.
  • Enter ./prog1 to run the program and see the output Welcome to C!
  • Congratulations, you have written your first C program. (Don't worry, it gets harder.)

    Part 2: Football Score Formula

  • Use nano to create a file called prog2.c
  • Write a C program that asks the user for the number of touchdowns, extra points, field goals, and safeties scored by the Irish, then computes and displays the total number of points scored by the team.
  • Hint: Figure 2.5 in the book gives an example of reading input values, then computing a simple formula.
  • Compile your program with gcc prog2.c -o prog2 If there are any errors, edit the file and try again.
  • Test your program by running ./prog2
  • Part 3: Custom Calculator

    (You know the commands to edit and compile programs now, so I will just give you general instructions for the program.)

    Create a C program (prog3.c) that computes the result of a non-trivial formula of three or more inputs that is useful to you. Choose a formula that you are familiar with, perhaps something from another math or science class. The program should ask the user for each of the relevant input variables, then display the result of the formula. If any of the given inputs is inappropriate for the formula, then you should instead display an error message and stop the program.

    For example, your program output might be:

    This program computes the airspeed of the common sparrow.
    Enter the wingspan in inches: 6
    Enter the altitude in feet: 150
    Enter the temperature in Celsius: 32
    The flight speed is 348 furlongs per fortnight.
    On the other hand:
    This program computes the airspeed of the common sparrow.
    Enter the wingspan in inches: -5
    Sorry, the wingspan must be greater than zero.
    A few hints to get the program right:
  • If your formula needs non-integer values, declare your variables as float instead of int and use %f instead of %d in printf and scanf.
  • If you discover an invalid input, use return 0; to cause the program to exit prematurely.
  • Lab Report

    For each lab, you will create a short report that explains the creative portion of the lab. This will help the grader to understand what you have written, and assure us that you did it yourself. Don't try to impress us with flowery language, just get to the point with a plain, clear explanation. Take care to organize your thoughts into paragraphs with correct grammar and spelling. The first lab report will be quite trivial but later reports will require more detail.

    This lab report should explain the following things:

  • What your program is meant to do, from the user's perspective.
  • How the program is structured, from the programmer's perspective.
  • How you evaluated the program for correctness. Did you verify that it produces correct results? Are there any cases where it produces incorrect results? Why?
  • Please write your lab report in plain text using nano and save it in the file report.txt:
    nano report.txt

    Turning In

    This assignment is due on Monday, Sept 9th at noon. Late assignments are not accepted.

    All submissions will be done electronically, so as to save a few trees, and make life a little easier for the graders.

    Copy all of your source files (prog1.c, prog2.c, prog3.c) and the lab report (report.txt) into your handin directory. Your handin directory is /afs/ (Obviously, replace NETID with your netid.)

    For example, to copy part one to your handin directory:

    cp prog1.c /afs/
    (Hint: type in one command, then use the up-arrow to modify the command, and then repeat it.) You can verify that your files are handed in by running the following command:
    ls -l /afs/
    If you find a mistake after turning in but before the deadline, you can update your submission by simply copying the necessary files again. You are free to turn in assignments multiple times before the deadline expires. It would be a good habit to turn in an incomplete but working assignment on a daily basis. Thus, there is no excuse for failing to turn in an assignment: everyone should turn in something long before the deadline.