Week 08 Weekly Test Questions

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 access this language documentation while attempting this test:

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.


Set up for the test by creating a new directory called test08, changing to this directory, and fetching the provided code by running these commands:

mkdir test08
cd test08
1521 fetch test08

Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.

weekly test question:
Check a File for non-ASCII Bytes

We need to check whether files contain non-ASCII bytes.

Write a C program, non_ascii.c, which takes one argument, a filename.

It should print one line of output.

If the file contains a non-ASCII byte, non_ascii.c should print the location of the first non-ASCII byte. Use the same format as the example below.

If the file contains no non-ASCII byte non_ascii.c should print a message indicating this. Again use the same format as the example below.

Assume a byte is non-ASCII if it contains a value between 128..255 inclusive.

dcc non_ascii.c -o non_ascii
echo hello world >file1
./non_ascii file1
file1 is all ASCII
 echo -e 'hello\xBAworld' >file2
./non_ascii file2
file2: byte 5 is non-ASCII
./non_ascii non_ascii.c
non_ascii.c is all ASCII
 echo -e '\x80\x81' >file3
file2: byte 0 is non-ASCII

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest non_ascii
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test08_non_ascii non_ascii.c

weekly test question:
Compare the Bytes of Two Files

We need to check whether 2 files contain identical bytes

Write a C program, compare_file.c, which takes two arguments, both filenames.

compare_file.c should print one line of output.

If the two files are different compare_file.c should print the location of the first byte whether they differ. Use the same format as the example below.

If the one file is shorter than the other but the bytes it contains are identical to the other file, compare_file.c should print a message indicating this. Use the same format as the example below.

If the 2 files contain exactly the same bytes compare_file.c should print a message indicating this. Again use the same format as the example below.

dcc compare_file.c -o compare_file
echo hello world >file1
echo hello world >file2
./compare_file file1 file2
Files are identical
echo -n hello >file3
./compare_file file1 file3
EOF on file3
echo help me >file4
./compare_file file1 file4
Files differ at byte 3

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest compare_file
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test08_compare_file compare_file.c

weekly test question:
Delete non-ASCII characters from a file

We need to remove the non-ASCII bytes from files.

Write a C program, leave_only_ascii.c, which takes one argument, a filename.

leave_only_ascii.c should remove all non-ASCII bytes from the file.

After it is run the file should contain only a ASCII bytes.

It should print nothing on stdout. It should only change the file.

Assume a byte is non-ASCII if it contains a value between 128..255 inclusive.

dcc leave_only_ascii.c -o leave_only_ascii
echo -e 'hello\xBAworld' >file2
ls -l file2
-rw-r--r-- 1 z5555555 z5555555 12 Nov  8 08:18 file2
./non_ascii file2
file2: byte 5 is non-ASCII
./leave_only_ascii file2
ls -l file2
-rw-r--r-- 1 z5555555 z5555555 11 Nov  8 08:18 file2
./non_ascii file2
file2 is all ASCII

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest leave_only_ascii
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test08_leave_only_ascii leave_only_ascii.c

Submission

When you are finished each exercise 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 Wednesday 01 January 00:00 to complete this test.

Automarking will be run by the lecturer several days after the submission deadline for the test, using test cases that you haven't seen: different to the test cases autotest runs for you.

(Hint: do your own testing as well as running autotest)

Test Marks

After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface or by running this command on a CSE machine:

1521 classrun -sturec

The test exercises for each week are worth in total 1 marks.

The best 6 of your 8 test marks for weeks 3-10 will be summed to give you a mark out of 9.