COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

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:
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.

Return Largest Number from a Linked List

Download max_list.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/max_list/max_list.c .
Note max_list.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
Your task is to add code to this function:
// return the maximum value in a linked list
int max_list(struct node *head) {

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

}

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

Add code to max_list so that its returns the largest value in the linked list.

You can assume the list contains at least one element (is not empty).

For example if the linked list contains these 8 elements:

16, 7, 8, 12, 13, 19, 21, 12

max_list should return 21, because 21 is the largest value in the list

Testing

max_list.c also contains a main function which allows you to test your max_list 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 max_list(head)
  • prints the result.

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

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

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

cp -n /web/cs1511/18s1/activities/max_list/max_list.c .
dcc max_list.c -o max_list
./max_list 16 7 8 12 13 19 21 12
21
./max_list 2 4 6 2 4 6
6
./max_list 13 15 17 19 23 29 3 5 7 11 
29
./max_list 2 4 8 16 32 64 128 256
256
./max_list 42
42

Assumptions/Restrictions/Clarifications.

max_list should return a single integer.

max_list can assume this list will contain at least one integer.

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

max_list should not use arrays.

max_list should not call malloc.

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

You can assume the linked list only contains positive integers.

max_list 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 max_list

Autotest Results

99% of 659 students who have autotested max_list.c so far, passed all autotest tests.
  • 99% passed test 1
  • 99% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
  • 99% passed test 7
  • 99% passed test 8
  • 99% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test12_max_list max_list.c

Count Farnarkles (Linked List Version)

Download list_count_farnarkles.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/list_count_farnarkles/list_count_farnarkles.c .
Note list_count_farnarkles.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
Your task is to add code to this function:
// Return the number of farnarkles in the two lists, i.e. the number of
// values which occur at the same position in both linked lists.
int count_farnarkles(struct node *head1, struct node *head2) {

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

}

count_farnarkles is given two arguments, head1 and head2, which are pointers to the first node of linked lists.

Add code to count_farnarkles so that returns a count of how many places the two lists have the same value in the same position.

Note in week 5, you wrote an equivalent function for 2 arrays of fixed equal length. In this case, the lists may be any length and the two lengths may be unequal.

For example, if the two lists contain these values:

1, 4, 1, 5, 9, 2, 1, 8
1, 1, 8, 2, 9, 5

count_farnarkles should return 2 because both lists have the same value (1) at position 0 and the same value (9) at position 4.

Testing

list_count_farnarkles.c also repeats a main function which allows you to test your count_farnarkles function.

This main function:

  • uses a command line argument of "-" to separate the values for two linked lists.
  • converts the command-line arguments before the "-" to a linked list
  • assigns a pointer to the first node in the linked list to head1
  • converts the command-line arguments after the "-" to a linked list
  • assigns a pointer to the first node in the linked list to head2
  • calls count_farnarkles(head1, head2)
  • prints the result.

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

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

Here is how the main function allows you to test count_farnarkles:

cp -n /web/cs1511/18s1/activities/list_count_farnarkles/list_count_farnarkles.c .
dcc list_count_farnarkles.c -o list_count_farnarkles
./list_count_farnarkles 3 1 4 - 2 7 1 8 3
0
./list_count_farnarkles 1 2 3 4 - 2 1 3 8
1
./list_count_farnarkles 5 5 6 5 - 6 5 5 5
2
./list_count_farnarkles 3 5 7 - 3 5 19 7 23 29
2
./list_count_farnarkles 1 2 3 4 5 6 - 3 2 1
1
./list_count_farnarkles - 1 2 3 4
0
./list_count_farnarkles 4 3 2 1 -
0
./list_count_farnarkles -
0

Assumptions/Restrictions/Clarifications.

count_farnarkles should return a single integer.

The linked list may be unequal length.

The linked lists may be any length.

Either or both linked lists may be empty (contain no elements).

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

count_farnarkles should not use arrays.

count_farnarkles should not call malloc.

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

count_farnarkles 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_count_farnarkles

Autotest Results

99% of 644 students who have autotested list_count_farnarkles.c so far, passed all autotest tests.
  • 99% passed test 1
  • 99% passed test 10
  • 99% passed test 11
  • 99% passed test 12
  • 99% passed test 13
  • 99% passed test 14
  • 99% passed test 15
  • 99% passed test 16
  • 100% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
  • 99% passed test 7
  • 99% passed test 8
  • 99% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test12_list_count_farnarkles list_count_farnarkles.c

Remove Consecutive Repeated Values From List

Download list_delete_repeated.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/list_delete_repeated/list_delete_repeated.c .
Note list_delete_repeated.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
Your task is to add code to this function:
struct node *delete_repeated(struct node *head) {

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

}

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

Add code to delete_repeated so that it deletes any consecutive repeated values from the linked list.

If there are no consecutive repeated values, the list should not be changed.

delete_repeated should return a pointer to the new list.

delete_repeated should call free to free the memory of the node it deletes.

For example if the linked list contains these 9 elements:

16, 16, 7, 8, 12, 19, 19, 19, 7

delete_repeated should return a pointer to a list with 6 elements:

16, 7, 8, 12, 19, 7

Testing

list_delete_repeated.c also contains a main function which allows you to test your delete_repeated 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_repeated(head)
  • prints the result.

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

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

cp -n /web/cs1511/18s1/activities/list_delete_repeated/list_delete_repeated.c .
dcc list_delete_repeated.c -o list_delete_repeated
./list_delete_repeated 16 16 7 8 12 19 19 19 7
[16, 7, 8, 12, 19, 7]
./list_delete_repeated 42 42 42 42 42 42 42
[42]
./list_delete_repeated 42 42 42 77 42 42 42 42
[42, 77, 42]
 ./list_delete_repeated 1 0 1 0 1 0 1 0 1
[1, 0, 1, 0, 1, 0, 1, 0, 1]
 ./list_delete_repeated 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
[1, 2, 3, 4, 5]
./list_delete_repeated
[]

Assumptions/Restrictions/Clarifications.

If there are two repeated values, delete_repeated may delete either list node.

Similarly if there are n repeated values delete_repeated may delete any n - 1 of the list nodes.

delete_repeated should call free to free the memory for the node it deletes

delete_repeated should not change the data fields of list nodes.

delete_repeated should not use arrays.

delete_repeated should not call malloc.

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

delete_repeated 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_repeated

Autotest Results

79% of 604 students who have autotested list_delete_repeated.c so far, passed all autotest tests.
  • 95% passed test 1
  • 81% passed test 10
  • 87% passed test 2
  • 85% passed test 3
  • 82% passed test 4
  • 84% passed test 5
  • 81% passed test 6
  • 88% passed test 7
  • 82% passed test 8
  • 94% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test12_list_delete_repeated list_delete_repeated.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 Saturday 2 June 23:59:59 to complete this test.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface