Question: Question 7: We want to write a function that counts the number of elements in a list that are larger than the average. For example,

 Question 7: We want to write a function that counts the

Question 7: We want to write a function that counts the number of elements in a list that are larger than the average. For example, the function should return 2 for list [1, 2, 3, 4], since two elements are above 2.5 (the average), and it should return 1 for list [3, 1, 2, 10], since only the last element is above 4.0 An iterative version of this function is easy to code but also inefficient, as it will need to traverse the list twice: first to calculate the average, then to compare and count the list elements. However, we can write an accumulator recursive ersion of this functio n that will traverse the list only once! To do this, follow the steps below. Assume we use the usual implementation of a singly-linked list of node structures, where each node stores an integer (a) Write the code of the non-tail-recursive function below, that returns the sum of all integer elements in the list. (1 mark) int Sum( Node* node )\ (b) Write the code of the tail-recursive function below (using an accumulator) that returns the sum of all integer elements in the list. (1 mark) int TSum( Nodenode, (c) Modify the above tail recursive function (using another accumulator) so that it returns the average of all integer elements in the list. (2 marks) int TAvg( Node node, (d) Modify the above function so that it returns the number of elements in the list that are larger than the average. The figure below is a hint. (3 marks) int TCount( Node* node, I/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!