Lab 7 - Arrays

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 command:
cp /usr/users/mdanfor/public_html/cs221-f08/array_example.cpp .
Compile and run the program using the following commands:
g++ -o demo array_example.cpp
./demo
You should see how the variable i gets overwritten by using an invalid subscript 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

Name your code lab7.cpp

Create a program that will read data from lab7.dat into arrays. You can copy the file over to your current directory using the following command:

cp /usr/users/mdanfor/public_html/cs221-f08/lab7.dat .
Note that lab7.dat has the pattern of an integer followed by a double on each line. This means you will need two arrays, one for the integers and one for the doubles. Use a loop similar to that described in class on Monday to read data from the file and store the data in the arrays. You will need to alter the "fin >> values[size]" portion. Recall from the file I/O lecture that you can read a line at a time using chained input if you know the pattern of the line. So to read each line, you would replace "fin >> values[size]" with a chained input similar to the following (assuming the input file is called fileIn):
fileIn >> <int_variable> >> <double_variable> 
The <int_variable> will be an element in your integer array and the <double_variable> will be an element in your double array.

After reading the values in, use a for() loop to print the integers to the screen and then second for() loop to print the doubles to the screen. Your output should be similar to the following:

The integers are: 3 7 10 5 96 76
The doubles are: 4.5 9.823 2.4517 3.54 56.78 43.2
Email lab7.cpp to me