|
|
Adam2016 wrote: |
---|
Below is the code he uses in the book with the only difference being the function name. in the example I'm assuming he uses an array = {1,2,3,4,5,6,7,8} emphasis on assuming as he doesn't even give you this information. He does however say that the end variable will be 8. so I indeed the code does work for when end is 8, it will give us the correct summation of 36! |
int summation_binaryRec(int* arr,int start,int end)
It is looking at the particular recursive routine int summation_binaryRec(int* arr,int start,int end) NOT at the other part of the code, which is just a test driver routine. |
|
|
if(n==1)
if(end-start == 1)
base conditionTo analyze Algorithm BinarySum, we consider, for simplicity, the case where n is a power of two. The general case of arbitrary n is considered in Exercise R-4.5. Figure 3.20 shows the recursion trace of an execution of function BinarySum(0,8). |
two things: 1. the parameters are clear: n integers, starting at index i, so the base condition is if(n==1) you have start, end instead which is commonly used on ranges [start, end), I expected a if(end-start == 1) base condition |
2. ⌈x⌉ means ceil(x) ⌊x⌋ means floor(x) and with those it'll work with any array size (well, except 0) |
to analyze Algorithm BinarySum, we consider, for simplicity, the case where n is a power of two. The general case of arbitrary n is considered in Exercise R-4.5. Figure 3.20 shows the recursion trace of an execution of function BinarySum(0,8). |
adam2016 wrote: |
---|
that brings me to the question why does n have to be a power of 2 for this algorithm to work? |
end / 2
.
|
|
|
|