Week 10 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:
Example Exam Q1: Return the Maximum Value in an Array

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

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

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

// Return the largest value in a given array.
int array_max(int size, int array[size]) {
    // Put your code here
    return 42;
}
You are to implement the array_max function which should return the largest value in the array.

The file array_max.c contains a main function which reads values into an array and calls array_max.

Here is how array_max.c should behave after you add the correct code to the function array_max:

dcc array_max.c -o array_max
./array_max
Enter array size: 5
Enter array values: 1 2 3 4 5
Maximum value is 5.
./array_max
Enter array size: 6
Enter array values: -1 -3 -9 -17 -2 -8
Maximum value is -1.

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

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

weekly test question:
Example Exam Q4: Find the Middle Element of a Linked List

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

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

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

// Return middle element of a linked list
// if list contains [6,7,8,9,10]  8 is returned
// if a list has even number of elements, first of middle two elements returned
// if list contains [1,2,3,4] 2 is returned
// list can not be empty
int get_middle(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;

}
get_middle is given one argument, head, which is the pointer to the first node in a linked list.

Add code to get_middle so that its returns the middle value of the list. If the list an even number of elements the first of the 2 elements in the middle of the list should be returned.

For example if the linked list contains these 8 elements:

1, 7, 8, 9, 13, 19, 21, 42

get_middle should return 9 because 9 and 13 are the middle two elements/

And or example if the linked list contains these 8 elements:

1, 2, 8, 1,  42

get_middle should return 8 because it is the middle element.

get_middle can assume the list is not empty.

Testing

list_get_middle.c also contains a main function which allows you to test your get_middle function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls list_get_middle(head)
  • prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_get_middle function will be called directly in marking. The main function is only to let you test your list_get_middle function

Here is how you use main function allows you to test list_get_middle:

dcc list_get_middle.c -o list_get_middle
./list_get_middle 1 2 4 8 16 32 64 128 256
16
./list_get_middle 2 4 6 5 8 9
6
./list_get_middle 13 15 17 19 18
17
./list_get_middle 42 4
42
./list_get_middle 42
42

Assumptions/Restrictions/Clarifications.

get_middle should return a single integer.

get_middle can assume the list has at least one element.

get_middle should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

get_middle should not use arrays.

get_middle should not call malloc.

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

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

Do not change the supplied 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 list_get_middle
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test10_list_get_middle list_get_middle.c

weekly test question:
Example Exam Q6: Delete all the elements of a Linked List that have the highest value

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

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

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

//
// Delete the node(s) in the list that contain the highest value
// The deleted node(s) are freed.
// The head of the list is returned.
//
struct node *delete_highest(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return NULL;

}
Note list_delete_highest.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
delete_highest is given one argument, head. head is the pointer to the first node in a linked list.

Add code to delete_highest so that it deletes the all nodes in the linked list whose data field are equal to the highest data value in the list.

delete_highest should return a pointer to the new list.

If the list is now empty delete_highest should return NULL.

delete_highest should call free to free the memory of any node it deletes.

For example if the linked list contains these 8 elements:

16, 7, 8, 19, 13, 19, 2, 12

delete_highest should return a pointer to a list with these elements:

16, 7, 8, 13, 2, 12

Testing

list_delete_highest.c also contains a main function which allows you to test your delete_highest function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls delete_highest(head)
  • prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your delete_highest function will be called directly in marking. The main function is only to let you test your delete_highest function

cp -n /web/cs1511/20T1/activities/list_delete_highest/list_delete_highest.c .
dcc list_delete_highest.c -o list_delete_highest
./list_delete_highest 16 7 8 19 13 19 2 12
[16, 7, 8, 13, 2, 12]
./list_delete_highest 200 150 27 200 200
[150, 27]
./list_delete_highest 4 6 2 4 6
[4, 2, 4]
./list_delete_highest 42
[]
./list_delete_highest
[]

Assumptions/Restrictions/Clarifications.

delete_highest should call free to free the memory for any nodes it deletes

delete_first should not change the data fields of list nodes.

delete_highest should not use arrays.

delete_highest should not call malloc.

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

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

Do not change the supplied 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 list_delete_highest
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test10_list_delete_highest list_delete_highest.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 11 Sunday 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.