| Name: _____________________ | Class: CET 375 |
| SSN/ID: _____________________ | Section & Group: ____________ |
The C++ programming language is based on the C programming language
and contains all of the features provided in C. Some of these
features have been replaced by modern counterparts that are more
consistent with the object-oriented approach to programming. There
are certain aspects of C, however, that are frequently omitted from
first programming courses but with which C++ programmers should be
familiar (because many of them will be employed as C programmers or at
least expected to be familiar with C). This is especially true of
data structures. The data structures provided in C, although few in
number, are usually implemented very efficiently. Also, they and
other C features are often used to implement the new C++ data types.
One of these important data structures is the array, and the objective of this lab exercise is to review C-style arrays. We will look first at one-dimensional arrays, at how they are declared and processed. Multi-dimensional arrays are considered at the end of the lab exercise and in Project 5B. Additional information about arrays can be found in Section 2.3 of the text C++: An Introduction to Data Structures.
In general, please see this site, http://www.swcp.com/~dodrill/controlled/extras/arrays.html, for an excellent introduction to arrays and their relationships to pointers. In particular, the name of an array is a constant pointer and is different from a regular pointer. E.g., suppose you have an array named array_name.
In C, array_name really does not refer to the whole array but rather the value of the name is a pointer constant that is the address of the first element. The type of this pointer constant is the type of the array elements, element_type. If the type of the elements is int, then the type of array_name is "constant pointer to int."
The last paragraph does not mean that arrays and pointers are the same. One exhibit in this section shows that an array has very different characteristics from a pointer. The most obvious difference is the result of applying the sizeof() operator to an array and to a pointer; an array is not a scalar, but a pointer is. When an array name appears in an expression, the compiler generates a pointer constant. A compiler uses the pointer constant since automatic and static arrays are fixed in memory at compile-time. In other words, such "pointers" aren't stored; they are just used internally by the compiler.
When it comes to arrays, pointer substitution does not occur in two places:
A. One-dimensional arrays
#include <iostream>
using namespace std;
int main() {
}
typedef array-element-type type-name[array-size];
|
ahead of main() to define two data types:
type-name array-name = {list-of-values};
|
For example, using something like IntegerArray prime = {0,0,0};
cout << animal << "***\n";
Execute the program and describe what happens; i.e., what is
output:
B. Arrays and addresses
Reminder: To get hex addresses displayed, you may
have to cast them to void * (to display the
address in Hexadecimal):
or cast them to unsigned (to display the address
in Decimal):
|
Your memory map might look something like this:
| Variable Name | arr[0] OR *(arr) |
arr[1] OR *(arr + 1) |
arr[2] OR *(arr + 2) |
|---|---|---|---|
| Address | \x...50 | \x...54 | \x...58 |
| Value | 1 | 1 | 1 |
How many bytes of memory were allocated to each array? ____________________
Use the sizeof operator to verify your answers.
What is the address of arr[2]? ____________________
Of arr[3]? ____________________
You may use for loops if you wish; that is, display
&arr[i] and arr+i and vari i from 0
to 7. What output is produced?
This example should show that an array name like
arr is the address of its first element
arr[0], arr+1 is the address of arr[1], and, in
general, arr+i is the address of arr[i]
arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7]
and also add statements to display the values of:
*arr,*(arr+1),*(arr+2),*(arr+3),*(arr+4),*(arr+5),*(arr+6),*(arr+7)
(You may use for loops.) What output is produced?
C. Out-of-range indices
Find out by adding the following assignment statements
before the statements in #11 used to display the elements
of the three arrays:
This overwrites the values stored at the memory addresses (arr
- 2) and (arr + 9) (which were being used by the
arrays first and last).
Now run the program again. List the elements of the three arrays:
arr[-2] = -99;
arr[9] = 99;
Now, display the values of all the array elements
arr[-8],arr[-7],...,arr[15] and explain why we got the
output we did for the elements of first and
last.
first: ____________________________________________________
arr: ____________________________________________________
last: ____________________________________________________
D. Multi-dimensional arrays
A two-dimensional array like:
having 3 rows and 4 columns (a 3x4 matrix) can be declared and
iitialized by:
_ _
| 11 22 33 44 |
mat = | 55 66 77 88 |
| -1 -2 -3 -4 |
‾‾ ‾‾
(It could all be on one line.) mat[i][j] is the element in
the i-th row and j-th column of mat,
numbering from 0. For example, mat[1][3] is 88.
int mat[3][4] = {{11,22,33,44},
{55,66,77,88},
{-1,-2,-3,-4}};
In addition, you can allocate this array dynamically using pointers. For example, to declare a dynamic two-dimensional array A, you would do:
int **A;
A = new (int *) [rows];
for (int i = 0; i < rows; i++) {
A[i] = new int [cols];
}
for (int i = 0; i < rows; i++) {
delete [] A[i];
}
delete [] A;
This would like this in memory:
The elements of mat can be displayed by nested for loops
like:
You might need to add #include <iomanip> in order to
use the input/output manipulation codes.
for (int i=0; i <= 2; i++) {
for (int j=0; j <= 3; j++) {
cout << setw(5) << mat[i][j];
cout << endl;
}
In addition, you can compute the offset to an element explicitly using:
mat[cols*i + j] == mat[i][j]Experiment with declarations and output statements like these and display some addresses to answer the following questions:
*(mat + 0) _________________ *(mat + 1) _________________ *(mat + 2) _________________
**(mat + 0) _________________ **(mat + 1) _________________ **(mat + 2) _________________
*(*mat + 0) _________________ *(*mat + 1) _________________ *(*mat + 2) _________________
*(*(mat + 1) + 0) _________________ *(*(mat + 1) + 1) _________________
*(*(mat + 1) + 2) _________________
*(mat + i)
**(mat + i)
*(*mat + i)
*(*(mat + i) + j)
Hand In: This lab handout with the answers filled in
and a listing of the program developed while working through the lab
exercise (use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename>. Or, you can use the a2ps command: a2ps <filename>).