Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

CSC 209 midterm

24 October 2011, 13:10

Aids permitted: Any paper.
Calculators are not permitted.

Total: 20 marks.
Time allotted: 45 minutes.

Since time is short, be careful not to get stuck on one question to the exclusion of others. Not everyone will necessarily be able to finish this test within the 45 minutes. The amount of marks or answer-space allotted does not indicate how long it will take you to complete the question, nor does the size of the answer-space indicate the size of the correct answer.

Answer all questions. Answer questions in the space provided.

Do not open this booklet until you are instructed to.


1. [8 marks]
Each subpart of this question builds upon the previous. If you can't get part of the question, you can write things like "part 'a' answer goes here".

a) Write a regular expression which matches "any positive integer".

b) "grep -v" outputs lines which do not match the given pattern. Remembering "^" for beginning of line and "$" for end of line, write shell commands to check whether the file "abc" contains just one integer per line (and nothing else). Output "yes" (only integer lines) or "no" (at least one inappropriate line).

c) Write a complete shell script which takes exactly one file name as command-line argument and outputs the sum of all of the numbers in that file, if the file contains only integers and one per line; or outputs a suitable error message if the file contains anything else. You also have to check the argument count and output an appropriate usage message, and otherwise behave like a proper complete shell script. (You can, however, assume that the addition never overflows what expr can do.)

[Sample solutions]


2. [6 marks]
Here is a portion of a unix filesystem. Like the "ls -l" output, we list whether the file (inode) is a plain file, directory, etc, with '-' for plain file or 'd' for directory. In the case of directories, the table also states the data content of the directory itself (a list of pairs of: inode number, name).

2d. 2, .. 2, usr 7, etc 4, bin 6
3- 
4d. 4, .. 2, passwd 3
5d. 5, .. 7, fsys.fig 10, src 11
6d. 6, .. 2, date 9
7d. 7, .. 2, bin 8, ajr 5, you 12
8d. 8, .. 7
9- 
10- 
11d. 11, .. 5, hello.c 13
12d. 12, .. 7, a1 14
13- 
14- 
15  
16  
17  

a) Suppose I do "mkdir /usr/ajr/newdir". Write in all changes/additions to the above list. (There are some free inodes at the end for you to use for new files.)

b) If instead I were cd'd to /usr/ajr and typed simply "mkdir newdir". would the result differ, and if so, how?

c) Suppose I do "ln -s /usr/you/a1 /usr/ajr/othera1", creating a "symbolic link". Write in all changes/additions to the above list.

d) If instead I were cd'd to /usr/you and typed "ln -s a1 /usr/ajr/othera1", would the result differ, and if so, how?

[Sample solutions]


3. [6 marks]
a) Write a C function (not an entire program) which takes an array of integers and the size of that array, and reverses them. E.g. if the array is initially [0,1,2,3], the array will end up being [3,2,1,0].

b) Write a C main() function which reads up to 20 integers into an array from the standard input with scanf(), calls the above function to reverse the array, and then outputs them all (one per line). You can assume that the input consists entirely of well-formed integers. If there are more than 20 integers in the input, your program just reads and processes the first 20.

[Sample solutions]