Programming Fundamentals
Download list_count_favourite.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity list_count_favourite
Your task is to add code to this function in list_count_favourite.c:
// Return the number of elements divisible by 17 in the linked list
int count_favourite(struct node *head) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
count_favourite is given one argument, head, which is the pointer to the
first node in a linked list.
Add code to count_favourite so that its returns the number of elements
divisible by 17 in the list.
For example if the linked list contains these 8 elements:
51, 7, 8, 9, 34, 19, 34, 42
count_favourite should return 3 because 51, 34 and 34 are divisible by 17.
Testing
list_count_favourite.c also contains a main function which allows you to
test your count_favourite 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_count_favourite(head) - prints the result.
Do not change this main function. If you want to change it, you have misread the question.
Your list_count_favourite function will be called directly in marking. The
main function is only to let you test your list_count_favourite function
Examples
Here is how you use main function allows you to test list_count_favourite:
dcc list_count_favourite.c -o list_count_favourite ./list_count_favourite 51 7 8 9 34 19 34 42 3 ./list_count_favourite 2 4 6 5 8 9 0 ./list_count_favourite 17 34 51 68 85 102 119 136 153 9 ./list_count_favourite 0
Assumptions/Restrictions/Clarifications
count_favouriteshould return a single integer.count_favouriteshould not change the linked list it is given.- Your function should not change the next or data fields of list nodes.
count_favouriteshould not use arrays.count_favouriteshould not call malloc.count_favouriteshould not callscanf(orgetcharorfgets).count_favouriteshould not print anything. It should not callprintf.- Do not change the supplied
mainfunction. It will not be tested or marked.