This exercise provides practice with pseudocode and passing arrays as
funtion arguments in C++. The objective of this exercise is to
develop code that adds up the elements of an array.
The answer file is lab10.
Exercise 1. Pseudocode
In the 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. C++ 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.
Design the subprogram so its return value is
the sum. In the main program write out the answer.
Call your new code sumarray.cc. Be sure to modify the
prompts and loops in the main program so you read only n elements
and not n+1. And don't forget the return
statement in the subprogram sum.
Copy your code to the answer file.