Week 05 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.


weekly test question:
Return How Many Big (Positive and Negative) Numbers are in an Array

Download count_bigger.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/count_bigger/count_bigger.c .

Your task is to add code to this function in count_bigger.c:

// return the number of "bigger" values in an array (i.e. larger than 99
// or smaller than -99).
int count_bigger(int length, int array[]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}
count_bigger should return a single integer: the number of values in the array which are larger than 99 or smaller than -99.

For example if the array contains these 8 elements:

141, 5, 92, 6, 535, -89, -752, -3

Your function should return 3, because these 3 elements are bigger than 99 or smaller than -99:

141, 535, -752

Assumptions/Restrictions/Clarifications.

count_bigger should return a single integer.

count_bigger should not change the array it is given.

count_bigger should not call scanf (or getchar or fgets).

You can assume the array contains at least one integer.

count_bigger should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

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

1511 autotest count_bigger
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test05_count_bigger count_bigger.c

weekly test question:
Convert a given matrix to an identity matrix

Download identity_matrix.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/identity_matrix/identity_matrix.c .

Your task is to add code to this function in identity_matrix.c:

//Makes a square matrix into an identity matrix
void make_identity(int size, int matrix[size][size]) {
    //TODO: Complete this function
}

The above file identity_matrix.c contains a function make_identity(), which should change the matrix passed in to be the identity matrix of coresponding size.

The identity matrix is a square, 2D array with all the cells containing 0, excluding those on the diagonal from top left to bottom right, which contain a value of 1.

For example, the identity matrix of:

    0 1 2
    3 4 5
    6 7 8
is:
    1 0 0 
    0 1 0
    0 0 1

The provided function doesn't actually work. Your task is to complete this function.

You can assume the 2D array will always be square and only contain integers from 0 to 9.

The file also contains a main function and a print function which you can use to help test your make_identity() function. It has one simple test case.

This main function and the print function will not be marked -- you must write all of your code in the make_identity() function. You may modify the main function if you wish (e.g. to add further tests), but only the make_identity() function will be marked.

Once your program is working, the output from the provided test in the main function should be:

dcc -o identity_matrix identity_matrix.c 
./identity_matrix 
1 0 0
0 1 0
0 0 1

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

1511 autotest identity_matrix
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test05_identity_matrix identity_matrix.c

weekly test question:
Print the elements of an array until a pointer

Download array_print_pointer.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/activities/array_print_pointer/array_print_pointer.c .

Your task is to add code to this function in array_print_pointer.c:

// Assumes that the pointer is aimed at one of the array elements.
void array_print_pointer(int nums[LENGTH], int *last) {
    printf("I should output some elements of the array.\n");
}

The above file array_print_pointer.c contains a function array_print_pointer, which should print out the elements of an array from the first element up to and including the element referenced by the pointer "last", which is an input to the function.

Each element printed should be followed by a space.

For this exercise, your task is to complete this function.

Note: you must not modify the array within the array_print_pointer function. You should only read the values in the array, not change them.

Note: All the arrays you are given will be of a fixed length, 10, which is defined as a constant.

Note: You can assume that the pointer "last" given as input to the function will always contain the address of one of the elements of the list.

The file also contains a main function which you can use to help test your array_print_pointer function. It has an example test case.

This main function will not be marked -- you must write all of your code in the array_print_pointer function. You may modify the main function if you wish (e.g. to add further tests), but only the array_print_pointer function will be marked.

Once your program is working, the output from the provided test in the main function should be:

dcc -o array_print_pointer array_print_pointer.c 
./array_print_pointer 
1 2 3 4 5 6 

This diagram represents what the example above may look like in memory. In this diagram the computer placed the array starting at address 0xFFFF FF80, but this will almost certainly not be where it is placed when you run the program so do not use this address in your code.

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

1511 autotest array_print_pointer
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test05_array_print_pointer array_print_pointer.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 Week 6 Thursday 17: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:

1511 classrun -sturec

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

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