A) Describe a non-recursive algorithm that takes a list of distinct integers a_1
ID: 3842728 • Letter: A
Question
A) Describe a non-recursive algorithm that takes a list of distinct integers a_1, a_2, ..., a_n and returns the number of even integers in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, .... Assume that a function to determine whether an integer is even or not is already built into your language. E.g. For the list 2, 3, 4, 5, 6, 7, your program should return 3, because there are three even numbers in that list. procedure Number of_Evens (a_1, a_2, ...a_n: integers) b) Describe a recursive algorithm that takes a list L of distinct integers and returns the number of even integers in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java. C++, ... You may assume that your language has a built in way to determine whether a number is prime or not, and has the following built in functions to manipulate lists. i) ''empty?'' which returns TRUE if a given list is empty, FALSE otherwise ii) "first" which returns the first element of a given nonempty list. iii) "rest" which returns a list containing all but the first element of a given nonempty list. Note that if the list has only one element, "rest" will return the empty list. procedure Number_of_Evens (L:list of integers)Explanation / Answer
Ans a)
int Number_of_Evens(List<Integer> a)
{
int count = 0;
Iterator<Integer> itr = a.iterator();
while(itr.hasNext())
{
int n = itr.next();
if(n % 2 == 0)
{
count++;
}
}
return count;
}
Note : I am not getting question 4 b) Is it regarding finding number of prime no or even no.
Here is the solution for finding recursively even numbers in list of integers.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.