Name: Lab 1 Introduction to UNIX and C This first lab is meant to be an introduction to computer environments we will be using this term. You must have a Pitt username to complete this lab. The doc is available at /afs/pitt.edu/home/y/u/yuz69/public/449/lab1/lab-1.doc Recitation TA: Yuyu Zhou Email: yuyuzhou@cs.pitt.edu, SENSQ 6148, Recitation time: 12:00-12:50PM, 2:00-2:50PM SENSQ5505 Office hour: Tuesday 10:00-11:59AM, Wednesday 3:00-5:00PM Responsibility: Recitation, lab and quiz grading Grader: Swarupa Vaishampayan Email: SSV12@pitt.edu Responsibility: project grading NOTE: Text in UNIX is case-sensitive. CS449 is different from cs449 is different from Cs449. All filenames of concern in this lab are lowercase. Please follow the instructions as listed in this document. Here are a few common UNIX commands: cd change directory ls list all files in current directory pwd display the current directory mv move or rename a file cp - copy a file gcc compile a c file pico edit a text file Part I In a Windows Lab: 1) To login to the computers, you will need to use an SSH client. The SSH client that we will be using is Putty located under Programs->Internet Tools in the Start Menu
We will connect to the machine (host name) unixs1.cis.pitt.edu. This machine will be used temporarily for the first part of the class. Eventually you will be using thot.cs.pitt.edu. You can also do this from home or anywhere you have an internet connection. For Windows users, you can download PuTTY (linked to from the course website). Linux and Mac users can open a terminal window and use the ssh command. ssh usrename@unixs1.cis.pitt.edu 2) When you login first, you are placed in your home directory. The command ls will list all of the files and directories there. The one we are most concerned with is the private directory. It is special in that only you can access files inside this directory. It will keep your work safe from other people. 3) Let s move into the private directory so we can work there: cd private Changes Directory to the private directory 4) For this class, we ll keep all of our files organized into a cs449 directory. Make it by typing: mkdir cs449 If you want to double check that it worked, type ls to list. 5) We now want to move into the cs449 directory to do our actual work. cd cs449 Part II. 1) While still in the cs449 directory type: mkdir lab1 cd lab1 to make a directory for today s lab. Now type: pico hello.c
pico is a very simple text editor, a lot like Notepad on windows. It is one option for creating and editing code under UNIX/Linux. 2) Type the following text in exactly as it is shown: #include <stdio.h> int main() { printf( Hello World!\n ); return 0; } 3) Save the file by hitting Ctrl + O and then enter. Exit pico by typing Ctrl + X. At the bottom of the pico window, it shows what keys do special things. The ^ means to hold Ctrl while pressing the key 4) Back at the prompt type: gcc o hello hello.c which will make our program. A file named hello will be in the directory if we type ls 5) Run it by typing:./hello Part III: Manual Pages If you ever want to see how a command works or you forget the various options you could use, you can consult the man pages on the command by typing: man COMMAND for example: man ls This will let you scroll through the online help about the ls command. The SPACE BAR will scroll the document one screenful at a time and the ENTER key will move one line at a time. At any time you can quit by pressing q In the space below, explain the purpose of the -S switch (capital S, not lowercase.)
1.) Part IV: Implementing Horner s method Write a C program that implements Horner's method of evaluating a polynomial. The coefficients of the polynomial will be inferred one at a time from the keyboard as digit characters. Download the lab-1.c starter file for the program you must finish. cp /afs/pitt.edu/home/y/u/yuz69/public/449/lab1/lab-1.c./ You are to repeatedly allow the user to enter in a single digit key from the keyboard (stdin) followed by a return. Use scanf("%c",&charvar) or charvar = getc(stdin) to read the entered key from stdin. After reading the digit, call getchar() to eat the CARRIAGE RETURN that gets entered after every digit key. We don't want that CARRIAGE RETURN code to be confused with our digits. After you read and store that digit key into a char variable, you must convert that ascii code to the numeric value of the pressed key. If the user typed in the '5' key, you must convert that ascii value in the char variable to be the number 5 which is the bit pattern 00000101. This is easily done by subtracting the literal char '0' from the variable you stored the keystroke into. Alternatively you can use the atoi() library call. After that is done you must multiply your running total by 10 and add the value of this latest char you just processed. When the user enters a non-digit value from the keyboard, that is the signal to abort the processing. Your program will then print out the final value of the number. When you are done, email your lab-1.c and answer for S switch to yuyuzhou@cs.pitt.edu before next week s recitation; please include your name and pitt username in your email. Extra tip: try input 2147483647 and 2147483648, observe the integer overflow. You don t need to solve integer overflow problem for this lab. Helpful Hints
Keep this sheet as a guide until you get comfortable in UNIX Every time you want to write a program you will do the following after logging into your account: cd private cd cs449 If you want to use vim or emacs instead of pico, that is fine. You may prefer to edit files under a GUI and use sftp to upload files, the choice is yours. Remember: Case matters!