Example:   Swap array elements.
void bad_swap(int, int); // prototypes
void a_swap(int[], int, int);
#define SIZE 100
int main() {
int x, y, // items to exchange
i, j, // indexes of array items to swap
A[SIZE]; // assume initialization
cin >> x >> y;
bad_swap(x, y);
cout << "x and y in main: " << x << " " << y << endl << endl;
cin >> i >> j;
if(i >= 0 && i < SIZE && j >= 0 && j < SIZE) { // are indexes in bounds?
a_swap(A, i, j);
cout << "A[" << i << "] and A[" << j << "] in main: "
<< A[i] << " " << A[y] << endl;
}
else {
cout << "Illegal array index; exiting program\n";
exit(1);
}
return 0;
}
// Function definition - does NOT work
void bad_swap(int a, int b) {
int temp;
temp = a; a = b; b = temp;
cout << "a and b in bad_swap: " << a << " " << b << endl;
}
// swap two array elements
void a_swap(int A[], int i, int j) {
int temp;
temp = A[i];
A[i] = A[j];
A[j] = temp;
cout << "A[" << i << "] and A[" << j << "] in a_swap: "
<< A[i] << " " << A[j] << endl;
}
|
Output if x and i are 6, y and j are 8, A[6] is 58, A[8] is -40: |
________________________________________
In order to modify the original variable, the function must have access to the address of the variable. As you have seen, this happens when we pass an array to a function. The array name acts like a pointer to (the address of) the first array element. This means that A is the same thing as &A[0], or   A == &A[0].   When we pass an array to a function, a copy of this address is made, and through this copy we have access to the array elements.
What about simple variables? Can we get access to the originals by passing (by value) their addresses? Yes! The correct version of swap() below illustrates this. Notice that a and b are input/output parameters since one value is held going in and that value is changed.
Example--corrected swap() with output parameters  Call with the addresses of two integers.
#include <iostream>
using namespace std;
void swap(int *, int *);
int main() {
int x = 3, y = 4;
swap(&x, &y);
cout << x << " " << y << endl;
return 0;
}
// swap two integers
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
cout << *a << " " << *b << endl;
}
|
What does each call so? See the comments! swap(&x, &y); // swap two integer variables swap(&A[x], &A[y]); // swap two array elements swap(&A[0], &A[SIZE-1]); // swap the first and last elements in A[] swap(A, &A[SIZE-1]); // swap the first and last elements in A[] (WHY?) |
<type> a_name [number_rows][number_cols]; |
Example
char grid[3][4];
grid[1][3] = 'R';
grid[2][0] = '$';
?
?
?
?
?
?
?
R
   0 1   2   3
$
?
?
?
Use the same approach as for 1-D arrays, but include sizes of all but the first dimension.
#define DAYS 31 // Sample function header void get_data(int x[][DAYS]) // Prototype void get_data(int [][DAYS]); |
Example:   Fill a 2-D array in row-major order
#define NROWS 3
#define NCOLS 4
// Read values in row-major order
char grid[NROWS][NCOLS];
int row, col;
for(row = 0; row < NROWS; row++)
for(col = 0; col < NCOLS; col++)
cin >> grid[row][col];
|
If input is: a b c d e f g h i j k l m n o p, we would get:
| a | b | c | d |
| e | f | g | h |
| i | j | k | l |
// Prototype void func(int[], char[][4]); // Call to func func(histo, grid); // Header for func void func(int histo[], char grid[][4]) |
// print A[0][0], A[1][1], A[2][2], ...
for(i = 0; i < N; i++)
cout << A[i][i] << endl;
|
2. Print the reverse diagonal,
// print A[0][N-1], A[1][N-2], ..., A[N-1][0]
for(i = 0; i < N; i++)
cout << A[i][N-(i+1)]) << endl;
|
3. Frame the array with asterisks:
// Frame top and bottom rows
for(col = 0; col < N; col++) {
A[0][col] = '*'; // top row
A[N-1][col] = '*'; // bottom row
}
// Frame sides, no overwrites
for(row = 1; row < N-1; row++) {
A[row][0] = '*'; // left side
A[row][N-1] ='*'; // right side
}
|
4. Print the elements in each of the odd-numbered columns, one column per line:
// Print odd-numbered columns int row, col, A[NROWS][NCOLS]; // assume initialization |
Try these at home. Drawing the diagrams will help.