Lab 7 - Arrays

The purpose of this array is to learn array concepts such as valid indices and array processing loops.

Part I: Array "Index out of Bounds" demo

As discussed in class, C++ will happily let you use an invalid subscript to access memory that is not part of the array. The demo, array_example.cpp, shows how one can "clobber" other variables in the program by accessing an invalid subscript. Copy the file to your home directory using the following command (or the wget command):
cp /home/fac/melissa/public_html/cs221/demos/array_example.cpp .
OR
wget http://www.cs.csubak.edu/~melissa/cs221/demos/array_example.cpp
Note the period at the end of the cp command, this is critical to get cp to copy the file to your current directory.

Compile and run the program using the following commands:

g++ -o demo array_example.cpp
./demo
You should see how the variables i and min get overwritten by using invalid subscripts for the array. This is why you, as the programmer, must keep track of the size of the array when accessing elements using the subscript operator.

Part II: Assignment

For this lab, you will write a program that uses an array to calculate the product of a list of doubles.

Program Requirements: Create an array that can hold 5 doubles. Use a for() loop to prompt the user to enter in 5 doubles. Calculate the product of all 5 doubles using either a second for() loop or by calculating it while reading in the doubles. Print out each of the doubles in the array using a for() loop, then print out the product that you have calculated.

The output should resemble the following (the user input is in blue font face):

Enter double 1: 1
Enter double 2: 1.5
Enter double 3: 2
Enter double 4: 3.2
Enter double 5: 2.5

The product of 1 1.5 2 3.2 2.5 is 24.

Name your code lab7.cpp. Email your completed code to me.