17. Exercise Set 7

Exercise 7.1: Newton Square Root Loop

Given: a real value Y, and the formula for Newton's square root algorithm,
x(i) = (Y/x(i-1) + x(i-1))/2, where x(0)=Y/2;
Given: termination criterion: |Y - x(i)**2| < epsilon, a real value;
Compose: a for-initial loop that calculates successive approximations x(i) until the termination criterion is met, and returns the final value of the approximation as well as the number of approximations, x(i), i >0, that have been calculated.
Click here for a sample answer.

Exercise 7.2: First Maximum and Minimum Loop

Given: a one-dimensional array, A, of real values;
Compose: a for-initial loop that finds the first instance of the minimum and maximum values present in the array, and returns these values and their index positions.
Click here for a sample answer.

Exercise 7.3: Successive Relaxation Loop

Given: a two-dimensional array, A, of real values;
Given: a predefined termination test function converged(x,y,eps) ,which takes two conformable two_dimensional arrays, x, and y, and a real value eps, and returns a Boolean value if its metric is met with respect to its third argument;
Compose: a function containing a for_initial loop that uses the function from exercise 16 to successively relax A until convergence occurs, and returns the converged value of A and the number of relaxations that have occurred.
Hint: Since the convergence function requires two arrays, one could use a bottom-tested loop...
Click here for a sample answer.

Exercise 7.4: Conway's Game of Life

Given: a two-dimensional array, Board, of integer cell values, zero for a dead cell and positive nonzero for a live one;
Given: the boundaries cells around the edge of Board are always dead;
Given: the rules by which a non-boundary cell lives or dies:
     if three or fewer neighbor cells are alive, then cell dies;
     if five or more neighbors cells are alive then cell dies;
     if four neighbor cells are alive, then increment current cell value;
Compose: a program that plays evolves the Board array until some input number of steps are completed, and returns an array of all the values taken on by Board.
Click here for a sample answer.




Previous Section



Next Section



Table of Contents