Modify the method that calculates the sum of the integers between 1 and N shown in this chapter. Have the new version match the following recursive definition: The sum of 1 to N is the sum of 1 to (N/2) plus the sum of (N/2 + 1) to N. Trace your solution using an N of 7.
What will be an ideal response?
// Computes the sum of the numbers between n1 and n2 (inc)
public int sum (int n1, int n2)
{
int result;
if (n2-n1 == 0)
result = n1;
else
{
int mid = (n1+n2)/2;
result = sum(n1, mid) + sum(mid+1, n2);
}
return result;
}
You might also like to view...
Select the group whose mission is to create guidelines and standards for web accessibility.
a. Internet Society b. Web Accessibility Initiative c. International Webmasters Association d. None of the above
What is the binary equivalent of the decimal number 240?
a. 11110000 b. 11111000 c. 11111100 d. 11111110
Which of the following is NOT true about writing comments in a Word Document?
A) Criticism needs to be constructive, with a suggestion for how to improve the document. B) Look for opportunities to compliment the author. C) Comments should be thorough no matter how long they may end up being. D) Comments should be helpful, kind, and unambiguous.
The ________ tab allows you to include a media object in your presentation
Fill in the blank(s) with correct word