SolutionsToExerciseCh8

# SolutionsToExerciseCh8 - RecursiveSumofArray (N1 GIVENS...

This preview shows pages 1–4. Sign up to view the full content.

3/3/2010 1 Write a recursive algorithm to find the sum of the values in array positions 0…(N 1): Recursive Sum of Array GIVENS: INTERMEDIATES: RESULT: HEADER: BODY: X (an array) N (the length of the array) S (sum of array of size N-1) Sum (sum of array of size N) Sum SumOfArray( X, N ) Index 0 S 0 N =1? 1 true false Index < N ? Sum Sum + A[Index] Index Index + 1 Sum Sum X[0] S SumOfArray(X,N-1) Sum S + X[N-1] false true N 1 ? Trace for this value of X: (p.1) 5 3 2 length 3 X statement XN S S u m initial values { 5, 3, 2 } 3 ? ? 1. test: N = 1? false 2. S SumOfArray(X,N-1) see page 2 2 3. Sum S + X[N-1] S SumOfArray(X,N-1) Sum SumOfArray(X,N) {5,3,2} 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3/3/2010 2 Trace page 2 statement XN S S u m initial values { 5, 3, 2 } 2 ? ? 1. test: N = 1? false 2. S SumOfArray(X,N-1) see page 3 3. Sum S + X[N-1] 3 S SumOfArray(X,N-1) Sum SumOfArray(X,N) {5,3,2} 1 Trace, page 3 statement S S u m initial values {532 1 ? ? { 5, 3, 2 } 1. test: N = 1? true 4. Sum X[0] 4
3/3/2010 3 Trace, page 3 statement XN S S u m initial values {532 1 ? ? { 5, 3, 2 } 1. test: N = 1?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/06/2011 for the course ITI 1120 taught by Professor Inkpen during the Fall '07 term at University of Ottawa.

### Page1 / 8

SolutionsToExerciseCh8 - RecursiveSumofArray (N1 GIVENS...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online