CSCI
515 C/C++
Programming Fall 2002
Dr.
Creider
CLab 8 Assignment (In lab
class)
Calculating two dimension
array subscripts using offsets
Write
a program to compare the subscripts used to access an element of a dynamically
allocated block of memory manipulated as a two dimension array with the actual
location represented as an offset. You
will not allocate any memory.
The virtual array could represent either a compile time array or a
dynamically allocated block of memory used as a two dimension array. In main, write code which has a nested loop
structure. The outer loop will be used
to enter pairs of values which represent row and column dimension sizes. Be sure to tell the user how to stop this
loop when he/she is ready to terminate this program. The inter loop will be used to enter pairs of values which
represent a row and column subscript.
Terminate this loop when the user enters a value of -1 for both the row
and column subscript.
Write
a function to take the dimension sizes and subscripts values entered by the
user and compute the actual subscripts to which the values entered refer and
the offset from the starting address of the block of memory.
Examples
Example 1: If you define an array to have 3 rows and 5
columns, and enter the subscripts of -1 for row and 8 for column, the actual
location of the element in the array is row 0 and column 3 with an offset of 3.
Example 2: If you define an array to have 5 rows and 2
columns and enter the subscripts of 2 for row and 5 for column, the actual
location of the element in the array is row 4 and column 1 with an offset of 9. If you entered the subscripts of 2 for row
and 0 for column, the actual location would be row 2 and column 0 with an
offset of 4.
Pass
to the function the two dimension sizes (number of rows and columns), the row
subscript and column subscripts which are the values entered by the user. The function will compute the offset and the
actual location (may be the same or different from the values entered by the
user) of the element in the array in the form of two subscripts. If the subscript values passed to the
function produce an offset which is less than the beginning of the array,
assign a value of -1 to the offset. If
the subscript values passed to the function produce an offset which is greater
than the end of the array, assign a value of -2 to the offset. Use the following formulas to compute the
offset and actual subscripts.
offset = row subscript entered *
column dimension size + column subscript entered
// if offset is before beginning of
array or after end of array do not compute the following
actual row subscript = offset / column dimension size
actual column subscript = offset %
column dimension size
Use
the parameter passing mode of “pass by reference” to change the value of the
last three arguments used in the call to this function. The function will have a total of 7
arguments.
When
the program returns to main, test the value of the offset. If the offset is a -1, output a message
explaining that the subscripts entered refer to an element before the beginning
of the array. If the offset is a -2
output a message that the subscripts refer to an element after the end of the
array. If the offset is positive
display the dimension sizes of the array, the input subscripts, the actual
subscripts and the offset computed by the function. Write the program so that you can test several different
dimension sizes with different pairs of row and column subscripts.
Name
the file you create, CLAB8. Do Not Use
Any Global Variables. Save both the
source (cpp file) and object code (exe) on your disk which is turned in at the
end of the lab.