University of New South Wales
COMP 1521 COMP1521 22T1 Week 10 Weekly Test Sample Answers
Week 10 Weekly Test Sample Answers
Test Conditions
These questions must be completed under self-administered exam-like cond
...
University of New South Wales
COMP 1521 COMP1521 22T1 Week 10 Weekly Test Sample Answers
Week 10 Weekly Test Sample Answers
Test Conditions
These questions must be completed under self-administered exam-like conditions. You must time the test yourself and ensure you
comply with the conditions below.
You may complete this test in CSE labs or elsewhere using your own machine.
You may complete this test at any time before Week 11 Friday 20º59º59.
Weekly tests are designed to act like a past paper - to give you an idea of how well you are progressing in the course, and what
you need to work on. Many of the questions in weekly tests are from past final exams.
Once the first hour has finished, you must submit all questions you've worked on.
You should then take note of how far you got, which parts you didn't understand.
You may choose then to keep working and submit test question anytime up to Week 11 Friday 20º59º59
However the maximum mark for any question you submit after the first hour will be 50%
You may access this language documentation while attempting this test:
C quick reference
MIPS quick reference
You may also access manual entries (the man command).
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.
Getting Started
Set up for the test by creating a new directory called test10 and changing to this directory.
$ mkdir test10
$ cd test10
There are some provided files for this test which you can fetch with this command:
$ 1521 fetch test10
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
þt{ ÓÇÓ ¼ÜÇÓL:
Finding files with unique inode numbers
Write a C program, unique_files.c, which takes a list of files as command-line arguments.
Your program should, for each file, check if the file's inode number has previously been encountered.
If the inode number has not been encountered, print the file's name.
If the inode number has been encountered, simply continue to the next file without printing anything.
$ dcc unique_files.c -o unique_files
$ ./unique_files
$ ./unique_files unique_files
unique_files
$ ./unique_files unique_files.c unique_files unique_files.c
unique_files.c
unique_files
$ rm -f ?; touch a b c
$ ln a d; ln a e; ln b f; ln c g; ln b h
$ ./unique_files a g d f h b c e
agf
HINT:
23/05/2022, 16:30 COMP1521 22T1 — Week 10 Weekly Test Sample Answers
https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/10/answers 2/7
The inode number for a file can be retrieved with the stat system call.
See: man 2 stat for more information.
NOTE:
Your solution must be writen in C only.
You are not permitted to run external programs.
You are not permitted to use system(), popen(), posix_spawn(), fork()/exec(), etc.
No error checking is necessary.
Your program can assume all given files exist.
When you think your program is working you can autotest to run some simple automated tests:
$ 1521 autotest unique_files
When you are finished working on this exercise you must submit your work by running give:
$ give cs1521 test10_unique_files unique_files.c
SOLUTION:
Sample solution for unique_files.c
[Show More]