60-140

Introduction to Algorithms and Programming I

Dr. Christie Ezeife

Lab. Exercises  #1 (Lab Date: Week 3 of Classes)

 

Objectives are to:

1.                                                Learn basic Unix commands for preparing a source C program file, compiling a C program and executing a C program with input data from the keyboard. Also, learn how to use script file for handing in record of source program file, compilation and execution of programs.

2.                                                Practise on concepts taught in chapter 1 of text including how to present solutions to simple problems in the form of an algorithm (or program), how to identify the necessary input and output data of a problem. Also, practise on conversions of numeric values from one number base to another, and character data from character to ASCII code and vice versa.

 

Que. 1. Type, compile and run the C program of  Figure 1.15 in the course book.  This program computes and prints the binary number equivalent of any given positive decimal number. Hand in or show your source program file, the input data and the output of your run.  Use the following set of input data:

            52

            300

            436

            16

            98

           

Hints on how to solve

 

Materials needed to complete this exercise are: a UWindsor computer account (ensure your computer account is already activated), a computer connected to either main arc1.uwindsor.ca server or sol.cs.uwindsor.ca or luna.cs.uwindsor.ca server (if in a lab, the computer terminals are already connected to these servers). Computer connected to, must have a text editor and a C compiler for typing, compiling and executing your programs.

 

Now follow these sequence of steps to complete the required task.

i.             Connect to arc1.uwindsor.ca server or sol.cs.uwindsor.ca or luna.cs.uwindsor.ca server.

ii.           Logon to the server using your computer user id and password.

iii.          Type the program in Figure 1.15 of text book and save in a file called lab01.c. To do this, you need to first use the appropriate Unix commands to create a directory and file with a text editor as follows.

iv.         Open a Unix shell terminal for issuing Unix commands.

v.           Create a directory called 60-140.
mkdir  60-140   <Enter>

vi.         Change to directory 60-140.
cd   60-140       <Enter>

vii.        Open a terminal window with either an editor like nedit or pico to type the program of Figure 1.15 and save as lab01.c.
nedit   lab01.c     <Enter>
or
pico    lab01.c    <Enter>
[Then, type the program in this window and save back as lab01.c]

viii.      Open a new terminal window for compiling and running your program.

ix.         In the new window, type:
ls                         <Enter>
[This Unix command lists all files in the current directory. You will see the file lab01.c listed]

x.           Now compile this program by typing:
cc  lab01.c     <Enter>
[If there are syntax errors, they will be listed on the terminal and you must go back to the program window, correct them, save the corrected program, and repeat the compilation of the corrected program. When there is no more compilation error, you are ready to run this program by moving on to the next step xi.]

xi.         Run (execute) the compiled program by typing:
a.out             <Enter>
[After successful compilation, the executable code is stored in a default file called a.out by C compilers
 so that typing the name of this file starts the process of running the program.]

xii.        If the cursor is blinking at a blank line during execution of this program, it means that it is executing the scanf instruction, where the program tells the CPU to read a positive decimal number from the keyboard.  Thus, the user (yourself) must type in one decimal number for the variable and type a <Enter> after each data line.  Since the program instructs the CPU to pick one positive decimal number, you need to re-execute the program each time to compute the binary equivalent of a new decimal number. You should find the binary equivalents of the following decimal numbers.
52

        300

        436

        16

        98

xiii.      You can see the result of the program printed on the terminal window.

 

Que. 2. Compile and run the same program of Figure 1.15 of text and show the source code, compilation, execution, program input and output data in a script file.

 

Hints on how to solve

 

A record of all Unix commands executed during a logon session or part of a logon session can be saved in a script file by simply initiating the recording with the Unix script command and ending the recording when completed, with an exit command as follows.

 

i.          Open a Unix terminal window and type:
            script    lab01_scriptfile             <Enter>
            [The general command is script  filename, our script file here is lab01_scrptfile]

ii.          Now display your source program file with the Unix cat command as:
            cat        lab01.c                         <Enter>
            [The command cat  filename is used to display contents of filename on the screen]

iii.         Now compile the program by typing:
            cc         lab01.c                         <Enter>

iv.         Now Run the program by typing:
            a.out                                         <Enter>

v.         Now, the CPU is waiting for you to type in a positive decimal number as before.           

You can re-run the program with the following decimal numbers.
            52

          300

          436

          16

          98

vi.          After the result of the program has been displayed, you must exit script session by typing:
exit                                          <Enter>
[Failure to exit will prevent the script file from being saved and created.]

 

Que. 3. Practise with other Unix commands to list all the files in your directory, see the contents of your script file, send the script file to your GA, send your script file to your home computer so that you can print it.

 

Hints on how to solve

 

i.        To see all files in the current directory including the script file, type:
ls                                                <Enter>

ii.        To see contents of the script file, type:
cat    lab01_scriptfile                   <Enter>            or
more  lab01_scriptfile                 <Enter>

iii.       To send a file like your script file lab01_scriptfile (which is currently on the server.uwindsor.ca) to your home computer so that you can print it, you need to use SSH file transfer program (SFTP) to transfer files from one computer (e.g., sol.cs.uwindsor.ca) to another (e.g., a PC or laptop). Note that you can only do this exercise when you have a PC or laptop and still connected to a Unix server. Thus, you can complete it at home to learn how to transfer your files and assignments. To use SFTP, follow the steps below:

a.    From your home PC computer or any PC or laptop that has internet connection and already has the SSH client installed on it, launch the SSH client software to log on to a campus Unix server from PC. You can log on to sol.cs.uwindsor.ca or any new server name available at the time of doing this lab.     

       While the SSH terminal or window is used to issue any Unix command on the remote server, the SFTP terminal or window is used to transfer files between the two computers. Open up the SFTP terminal (the secure file transfer program) icon. This SFTP window looks like popular Windows explorer window.  On the SFTP window, there are two panes where the left pane has a listing of files in the local computer (e.g., PC or laptop) and the right pane has a listing of files in the remote computer (e.g., the Unix server like sol.cs.uwindsor.ca).

b.   Use the SFTP menu commands to download files from Unix server to PC and to upload files from PC to Unix server. You can also just drag and drop files from one computer (e.g., right pane) to another (e.g., left pane) the same way you would do it with Windows explorer.

       Now, download your scriptfile called lab01_scriptfile.

c.        Print your scriptfile lab01_scriptfile with a printer connected to the PC or laptop.  Once you have downloaded your scriptfile lab01_scriptfile onto your PC, you can open it up with Wordpad or Notepad and print the file so it can be handed in for marking later.

iv        Practise with other Unix commands for creating and deleting directory, deleting file, changing directory, renaming a file, making a copy of a file and so on.

            To create a new directory on Unix, use the mkdir command as:
                       mkdir    dirname                       <Enter>

v.        To change to a different directory on Unix, use the cd  as:
           cd               dirname                 <Enter>
           [Note that dirname stands for the entire path of the directory from                                              root  (/)or home directory (~)].

vi.       To move up one directory, for example to the parent directory, use:
           cd   ..                                       <Enter>

vii.      Wherever you are, to go to your home directory, use:
           cd    ~                                      <Enter>

viii.     To make a copy of lab01.c file and keep in lab01cp.c, use:
           cp  lab01.c   lab01cp.c             <Enter>

ix.       To delete a file like delete lab01.c after making a copy, use:
           rm   lab01.c                              <Enter>

x.        To rename a file from lab01.c  to lab02.c, use:
           mv   lab01.c   lab02.c               <Enter>

 

Use the ASCII table in Appendix A of course text, for exercises involving ASCII conversion.

Que. 4.  Here is a message coded in ASCII using eight bits per symbol.  What does it say?


01001001  00100000  01110111  01101111  01110010  01101011

00100000  01101000  01100001  01110010  01100100  00101110

 

Que. 5.  Show how the following instructions are represented in binary using ASCII by writing the codes for the instructions.

            Q /= 20;

            a--;

 

Que. 6.  Convert each of the following binary representations to its decimal form.

(a)     110100      (b) 1000000     (c.) 1111111

 

Que. 7. Convert each of the following decimal representations to its equivalent binary form.

(a)     21              (b) 53               (c.) 400

 

Que. 8.  Name all the hardware components of the computer and identify the function of each component.

 

Que. 9.What is an algorithm?  What is a program?, What is a problem?, What are input and output data?

 

Que. 10. For each of the conversions you did in problems (4) to (7) above identify the input, output and algorithm.