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 complete this test in CSE labs or elsewhere using your own machine.
- You may complete this test at any time before Week 11 Sunday 17:00.
- 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 COMP1511 final exams.
- You must treat the first hour as a real exam - no access to external help from other people or the internet.
- Once the first hour has finished, you should take note of how far you got, which parts you didn't understand, and ask questions to clarify your understanding with course staff or on the course forum.
- Failure to follow the above instructions will result in a lower quality learning experience and ultimately a lower mark in the final exam.
- Once the first hour has finished, you may use the internet, get help from other people, and still submit your solutions afterwards.
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.
You should not write any code. Test in Progress — working time You have just over minutes left in the test. Test Complete! Your time for this test has finished. You may submit your work. You may choose to keep working, but you should reflect on how you went in this hour, and discuss with your tutor if you have concerns.
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;
}
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_maxWhen 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;
}
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_middleWhen 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;
}
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 deletesdelete_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_highestWhen 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
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.