0

Can someone please help me find a method to this?


The following recursive method addeven (...) is supposed to add the even indexed values of an array, i.e., a[0] + a[2] + a[4] + … There are bugs in the method. Trace the method using test data, find the bugs, and correct them. Be thorough. The method may work on some data sets. (Hint: The error involves one line of code>)

public static int addeven(int a[], int size)

{

if (size == 2)

return a[1];

return a[size - 1] + addeven(a, size - 2) ;

}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by singuland
0

I know can u guide me though
im having hard time understanding it cuz ihave no book
am isuppose to write a method for it or just correct it ??

thanks :)

0

Just correct it. Walk through it step by step and see where it's wrong. Make a few test lists to work on - make sure you have the edge cases covered, try it with lists of zero, one, and two elements, as well as some longer ones with both even and odd numbers of elements. Walk through the code for each list, and see what it does. Where it does the wrong thing, fix it.

It's not hard. I'm not going to do it for you. You get no more help here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.