# Week 01 Laboratory Exercises

### Objectives

In this Lab, you will:

• Learn how to access your CSE account
• Become familiar with the Linux environment
• Use a text editor to create small C programs
• Use dcc to compile a C program
• Run a C program in a Linux terminal

### Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth one mark in total:

Worth half a mark in total:

Worth half a mark in total:

For your interest, but not for marks:

## Task #0: Understanding what this lab is!

In this first COMP1511 lab, the primary aim is for you to familiarise yourself with the process of creating, compiling and executing simple C programs.

The material you will need to know can be found in the week 1 lecture notes.

This exercise will simply ensure you are capable of writing a small program and getting it to run.

There are no marks for this week's work, but you should practice testing and submitting your answers in preparation for future weeks.

If you get stuck with any of this, don't hesitate to ask one of your tutors for help, that's why they're here!

You are done if: You have read this section.

Since the course this term is online, you will need to work on your own computer.

Don't worry about the age or specifications of the computer itself, even reasonably old computers will be able to handle the work we're doing in COMP1511.

If you don't have a computer where you live, please contact cs1511@cse.unsw.edu.au and we'll see what we can do to help.

Most COMP1511 students use a program called VLab to connect to CSE. It shows you an interface that looks identical to the ones in the Computer Science labs run by the School. There's more information about how to do set it up at the Home Computing link.

You are done if: You have installed VLab on your computer.

## Task #2: Logging into VLab

To log in, you use your zID (which looks something like z1234567) and your zPass (which is used to log into all other university online services).

The CSE labs use the Linux operating system (not Windows or OSX or ...).

When you log in, you will be shown some information about the rules of using VLab, and about the resources available to you in VLab.

If you are asked to select a panel click on Use default config, as in the image below.

If you selected the other option, you will see an empty screen. Ask your tutor to show you how to use the 1511 reset_panel command, and then log back in!

You are done if: You see an interface that looks like this:

## Task #3: Open a Web Browser

Along with menus that you can see, such as the Applications Menu in the top left corner, it also has a simple menu you can access by right-clicking anywhere on the desktop.

Have a look around and see if you can work out how to open a web browser.

You are done if: You opened a web browser.

## Task #4: Open A Terminal

Although there are lots of things you can do by using the graphical user interface (GUI) - clicking on menu items and icons, we really want you understand how to use the Linux command-line. This is a really efficient and powerful way of interacting with a computer.

If there isn't a terminal open, right click on the desktop and select 'Open Terminal Here'

This will bring up a window where you can type in Linux commands.

You are done if: You opened a terminal, and can now type things in.

By default, the terminal VLab uses is small and un-intuitive.

To fix this, you should run the 1511 setup command. This will set some defaults inside VLab, including changing to a more user friendly terminal.

Type exactly 1511 setup into your terminal. It will look a little like the following:

  1511 setup
Welcome to COMP1511!

Setting up some sensible default values for indentation, line numbers, etc.....

[... some text]
All done! Enjoy your gedit experience :)

Setting up default applications (if they aren't already set)
-----------------------------------------------------------
[... some text]
-----------------------------------------------------------
Done!


You should also now run 1511 colors, which will make your terminal prompt (the thing to the left of where you type commands) more colorful. The colors make it easier to use the terminal, in addition to looking beter.

Now you have done this, you should close and re-open your terminal.

Due to some issues this term, you may need to run the command bash to have your terminal look similar to the photos.

Congratulations! Your terminal is now setup. You do not need to run these commands again.

You are done if:

• You have run 1511 setup
• You have run 1511 colors
• You have closed and re-opened your terminal.
• Your terminal looks something like this:

From the terminal, you have access to many commands! Commands can do many useful things for you. Commands can also tell you many useful pieces of information.

It might look scary at first, but using a terminal can be fun plus you look like you are a computer guru! Don't be scared to try out different commands - practice makes you feel more comfortable using the command line.

Here's a list of commands you might want to try out:

• rquota tells you how much of your alloted file system storage you've used up (from creating files)
• acc tells you your name, what classes you're in, where your home directory is and other details that may or may not be useful
• date tells you today's date and time
• echo "some text" will print back out "some text".
• touch my_file will create an empty file, called my_file.
• mv my_file other_file will move (or rename) my_file to be called other_file.

You are done if: You have tried out at least 2 of the above commands.

## Task #7: Power-Using a Terminal

In COMP1511, you'll be spending quite a lot of time in the terminal.

There are some really useful features that terminals have which will make them much easier to use.

If you type a really long command (for instance echo "COMP1511 is awesome"), you might decide you want to edit the command. You cannot use a mouse to select earlier on the line. However, you can use the left and right arrow!

Try editing a command by typing something out, and then going backwards to fix it. Then, press enter to use the command.

Sometimes, you want to repeat a previous command. To do this, press the up arrow. This will show you the last command you ran. Use the up arrow to go further backwards in history.

Try running your last command again!

You are done if: You have successfully edited a command, and you have run a previous command.

## Task #8: Using Directories (a.k.a Folders)

Now we're going to learn about some commands for working with directories (directories are often called folders on other operating systems).

• pwd
• ls
• cd
• mkdir

The Linux command pwd tells you what directory you are in, often called your current working directory.

pwd stands for 'print working directory'.

If your username is 'z7654321', and you enter the 'pwd' command into the terminal, it might tell you:

pwd
/import/cage/1/z7654321/Desktop


This means that I'm in the Desktop directory inside my (z7654321's) home directoryand my home directory is stored on the file server called cage.

What does pwd print for you?

The Linux command ls prints a list of the files in the present directory. Since you have a new account there will probably be nothing in your Desktop directory.

ls


The Linux command cd is a command that you can use to change your directory.

If you type in cd on its own as follows, you will move into your home directory.

If you type in cd with a '..' after (cd ..), it moves one level back up.

Try this out and confirm your directory has changed by running pwd again.

pwd
/import/cage/1/z7654321/Desktop
cd
pwd
/import/cage/1/z7654321


If you run ls you'll see what is in your home directory. Since you have a new account, your home directory will most likely only contain your Desktop and a directory called public_html (which is where you can create files to create websites).

ls


The Linux command mkdir makes a new directory. To use it you must supply the name of the directory you wish to create. Let's use it to create a directory for this week's lab exercises.

mkdir lab01


Now the command ls should show you the directory you just created.

ls
Desktop lab01 public_html


Now we will use cd in a slightly different way where we tell it what directory we want to change into.

cd lab01


To confirm we really are in our new created lab01 directory type the pwd command again.

pwd
/import/cage/1/z7654321/lab01


If you have setup your terminal correctly, you may not need to use pwd that often, because your prompt (the text before you type a command) will show you what folder you're in.

You are done if: you have successfully created a folder called lab01 and moved into it.

### Exercise 1: Bad Pun (●◌◌)

Every week in COMP1511 you will need to complete "exercises" to get marks.

This is your first exercise -- exciting!

Exercises also have a "level of complexity indicator". Read the course outline for more details, but in general, the more filled in circles, the more complex the exercise.

Each week, a maximum of two marks will be available. To get full marks in the "lab exercises" component of this course, you need to get ten marks.

We expect most students will complete all the ●◌◌ and ●●◌ exercises. You can miss one week's lab, and still get full marks by completing all those exercises. Marks in ●●● exercises can make up for missed exercises elsewhere.

You are done if: you have read the above introduction.

## Task #11: Open a file

Now it's time to create a file. We are going to use the text editor gedit.

To create a file using gedit, type:

gedit bad_pun.c &


This will open up a graphical editor where you can manipulate text.

The & at the end of the line is very important!

It makes sure that you can keep using your terminal while editing code. If you forget it, your terminal will wait until you close gedit to accept any more commands.

Here's an example of a simple C program. Copy it into the gedit window, and save it.

// A simple C program that attempts to be punny
// Written 23/2/2017
// by Angela Finlayson (angf@cse.unsw.edu.au)
// for COMP1511 Lab 01 Exercise 1

#include <stdio.h>

int main(void) {

printf("Hello, it is good to C you!\n");

return 0;
}


When you save, it will place the contents of the editor into the file bad_pun.c.

Handy Tip: On many linux systems copying can be achieved by simply highlighting with the left mouse button and pasting can be achieved by simply clicking with the middle button.

You are done if: you have copied and saved the text of bad_pun.c.

## Task #12: Create a Program

Once you have pressed save, click on the Terminal window again and type this command to compile your program:

dcc -o bad_pun bad_pun.c


The -o bad_pun option tells the compiler to give toe newly compiled program the name bad_pun.

If dcc does not print out any error messages, then your program has been successfully compiled. Otherwise you will need to find the errors in your code, fix them, save the file and compile again.

Run the program to test that it works. Type:

./bad_pun


The ./ before the program name specifies that the program is found in the current directory. Note that Unix executable (program) names do not require the .exe extension that you might have have seen under Windows.

You are done if: You have run ./bad_pun successfully.

The COMP1511 class account contains a script that automatically checks your solution to lab exercises.

While it is not important this week, let's run the autotest script so you can see what it does.

You can check bad_pun.c like this:

1511 autotest bad_pun bad_pun.c
1 tests passed  0 tests failed


Don't worry if you fail the check and you can't see why.

There are no marks for this week's lab but practice submitting your work electronically.

You submit your work with the give command like this:

give cs1511 lab01_bad_pun bad_pun.c


You are done if: The give command says it has accepted your submission.

## Task #14: Make a Bird

Now use the Linux command cp to copy bad_pun.c to a new file named bird.c, like this:

cp bad_pun.c bird.c


You are now ready to start editing the file bird.c using your favourite editor.

gedit bird.c &


Note that the basic structure of the program can be retained; you just need to change the comments, and modify and/or add printf statements to the program. Edit the file bird.c to produce a new program that behaves as follows:

dcc -o bird bird.c
./bird
___
('v')
((___))
^   ^


Make sure you save your modified program before you compile it.

Make sure you re-compile your program every time you modify the code.

Handy Tip: Linux remembers the commands we have recently typed in. By pressing the UPARROW key, it will bring up your previous command and save you retyping it in! Try it. You can press the UPARROW key repeatedly to go back to the second last command, third last command and so on.

Handy Tip: At your Linux command prompt, type in ./b and then press the tab key. Linux will automatically try to fill in your partially typed command for you!

Again try the automatic checking script in the class account.

1511 autotest bird bird.c
Test 0 (./bird) - passed
1 tests passed  0 tests failed


Again don't worry if you fail the checking - and you can't see why.

There are no marks for this week's lab but practice submitting your work electronically.

You submit your work with the give command like this:

give cs1511 lab01_bird bird.c


You are done if: The give command says it has accepted your submission.

## Task #15: Complete the Kangaroo

Copy the program kangaroo.c from the course account to your directory by typing (make sure you type the dot at the end):

cp ~cs1511/public_html/21T3/tlb/01/kangaroo.c .


The dot '.' is a shorthand for the current directory and there is a space between kangaroo.c and the next dot. You can check that the file has been copied by typing:

ls


You can examine the contents of the file by typing:

cat kangaroo.c
// A simple C program that prints an ASCII kangaroo
// Written 7/3/2017
/  by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511
...


Now try to compile kangaroo.c. You should see a list of confusing error messages.

dcc -o kangaroo kangaroo.c
kangaroo.c:4:1: error: expected identifier or ‘(’ before ‘/’ token
/  by Andrew Taylor (andrewt@cse.unsw.edu.au)
...


Your job is to fix the errors.

You'll need to use an editor to change the file kangaroo.c

Run gedit like this:

gedit kangaroo.c &


When you have fixed all the errors you should be able to do this.

dcc -o kangaroo kangaroo.c
./kangaroo

/\   ___
<__\_/    \
\_  /  _\
\_\ / \\
//   \\
=//     \==



Again try the automatic checking script in the class account.

1511 autotest kangaroo kangaroo.c
Test 0 (./kangaroo) - passed
1 tests passed  0 tests failed


Again don't worry if you fail the checking and you can't see why.

Submit your work with the give command like this:

give cs1511 lab01_kangaroo kangaroo.c


You are done if: The give command says it has accepted your submission.

Write a program about_me.c which prints out answers to some questions about you!

Copy the program about_me.c from the course account to your directory by typing (make sure you type the dot at the end):

cp ~cs1511/public_html/21T3/activities/about_me/about_me.c .


The dot '.' is a shorthand for the current directory and there is a space between about_me.c and the next dot. You can check that the file has been copied by typing:

ls


Your job is to fill in this program with your own code.

You'll need to use an editor to change the file about_me.c

Run gedit like this:

gedit about_me.c &

Your program should produce output as follows, substituting ... for your own responses:
  dcc -o about_me about_me.c
My favourite food is: ...
I'm doing COMP1511 because: ...
I hope to have achieved ... by the time I finish COMP1511.
Something I'm passionate about is: ...


Your tutor might discuss these answers with you over the next few weeks!

Submit your work with the give command like this:

give cs1511 lab01_about_me about_me.c


You are done if: The give command says it has accepted your submission.

Your tutor has sent an email to your official UNSW email account. You need to reply to it. We want to make sure you can receive official emails!

You are done if: You have replied to your tutor's email.

## Task 18: Post on the Course Forum!

You can ask any questions you have related to COMP1511 on the course forum.

Remember never to post assessment code on the course forum. Course staff can view your recently autotested/submitted code. You can also post anonymously, by clicking on your profile icon in the top right, then click on your zID, then click on "Enter Anonymous Mode".

There is already a thread on the forum to introduce yourself -- make a post!

You are done if: You have posted in the "introduce yourself" thread of the forum.

## Task #19: Message of the Day (Optional)

A Message of the Day (often called an "MotD") is a message traditionally shown when you log into a unix system. It usually gives you a warm greeting and tells you some information.

In this exercise, you will make your own MotD. You do not need to submit it, nor is it directly assessed in any way. It is, however, an opportunity for you to demonstrate your creative skills to your new friends and tutors. You can even keep adding to it throughout the term.

To get started, run the following command. It will show the following output.

1511 motd
Did not find a MotD! You should make a file called motd.c and compile it.


This program has just enabled an MotD on your shell, but now you need to specify what it will say.

Make a new file called motd.c in your home directory. If you're not in your home directory, type the command cd to go there.

You should make this program print out a message of the day, like "Hi Tom, I'm looking forward to COMP1511".

When you're done, you can run:

dcc -o motd motd.c
./motd
Hi Tom, I'm looking forward to COMP1511.


To get rid of the message, remove the motd file:

rm motd


Now that you've got it working, here are some ways to make your motd stand out:

You are done if: You are happy with your MotD.

### Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 2 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

#### Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec