write the comments with input, output, description, and test cases for a recursi
ID: 3546900 • Letter: W
Question
write the comments with input, output, description, and test cases for a recursive function that goes through a list and adds up all even numbers numbers (and only even numbers) in the list. So if the list was, [1 5 2 6 3 5 4] it would print out 12. Make up 3 more test list and test your function. The second parameter points to a location in the list. Can someone pleez help, I've been stuck since yesterday and no solution has worked so far. Please comment so i can understand logic and comment my code as well.
Explanation / Answer
int sums(int arr[], int numOfElem){
//set a stopping point (no more elements)
if(numOfElem == 0){
return 0;
}
//apply recursion through the other elements
//check for even
if(arr[numOfElem-1] % 2 == 0){
return arr[numOfElem-1] + sums(arr,numOfElem -1);
}
else
//since not even don't add the value
return sums(arr,numOfElem-1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.