PHYCS 3730/6720 Lab Exercise

Reading and references:

This exercise provides practice with pseudocode and passing arrays (lists) as funtion arguments in Python. The objective of this exercise is to develop code that adds up the elements of an array.

The answer file is Mylab08.txt.

Exercise 1. Pseudocode

In your answer file write a few lines of the pseudocode for an algorithm that adds up the elements of an array. For definiteness, call the array a and let n be the number of elements. The array index starts at 0 and goes to n-1. You will find that you need to define a new variable that holds the running total. Then you run through the array one element at a time, adding the element to the running total.

Exercise 2. Python code

In the array handout you will find code for reading a list of polynomial coefficients and evaluating a polynomial using Horner's algorithm. Modify this code so it reads array elements a[0] ... a[n-1] and calls a subprogram sum that adds up the array elements. The modification involves removing the poly subprogram and replacing it with a subprogram that just does the sum. Design the subprogram so its return value is the sum. This means that you should get the answer with a statement in the main program like this: total = sum(...) . In the main program, also write out the answer.

Call your new code sumarray.py. Be sure to modify the prompts and loops in the main program so you read only n elements and not n+1.

Copy your code to the answer file.