|
1
|
|
|
2
|
- One Dimensional Arrays
- Multidimensional Arrays
|
|
3
|
- The simplest type of array in C is one which is declared and used in=
one
place. There are more complex uses of arrays. The following declares=
an
array called scores to hold 100 integers and sets the first and last
elements.
- C arrays are always indexed from 0. So the first int in scores array=
is
scores[0] and the last is scores[99].
- int scores[100];
- scores[0] =3D 13; // set first element
- scores[99] =3D 42; // set last element
|
|
4
|
|
|
5
|
- It's a very common error to try to refer to non-existent scores[100]
element. C does not do any run time or compile time bounds checking =
in
arrays. At run time the code will just access or mangle whatever mem=
ory
it happens to hit and crash or misbehave in some unpredictable way
thereafter. "Professional
programmer's language." The convention of numbering thin=
gs
0..(number of things - 1) pervades the language. To best integrate w=
ith
C and other C programmers, you should use that sort of numbering in =
your
own data structures as well.
|
|
6
|
- In C, as in Fortran or PL/I, it is possible to make arrays whose
elements are basic types. Thus we can make an array of 10 integers w=
ith
the declaration int x[10];
- The square brackets mean subscripting; parentheses are used only for
function references. Array indexes begin at zero, so the elements of=
x
are
- x[0], x[1], x[2], ..., x[9]
- If an array has n elements, the largest subscript is n-1.
|
|
7
|
- The for statement is a somewhat generalized while that lets us put t=
he
initialization and increment parts of a loop into a single statement
along with the test. The general form of the for is
- for( initialization; expression; increment )
- statement
- The meaning is exactly
- initialization;
- while( expression ) {
- statement
- increment;
- }
|
|
8
|
- #define SIZE 10;
- main ()
- { int i, array[SIZE];
- for (i =3D 0; i < SIZE; i++)
- {
- array[i] =3D 0;=
- }
- }
|
|
9
|
- This slightly more ornate example adds up the elements of an array:<=
/li>
- sum =3D 0;
- for( i=3D0; i<n; i++)
- sum =3D sum + array[i];
|
|
10
|
- Here are various examples for using arrays.
- int a[45]; // Ar=
ray of
45 elements
- a[0] =3D 23; // Sets f=
irst
element to 23;
- a[a[0]] =3D 56; // Sets the 24th element to 56
- a[23] +=3D 56; // Adds 56 to the 24th element
- int x[4]=3D {0,1,2,3}; /* makes x[i] =3D i */
- int y[ ]=3D {0,1,2,3}; /* makes y big enough for 4 values */
|
|
11
|
- Here is a program which reads a line, stores it in a buffer, and pri=
nts
its length (excluding the newline at the end).
- main( ) {
- int n, c;
- char line[100];
- n =3D 0;
- while( (c=3Dgetchar( )) !=3D ‘\n’ ) {
- if( n < 100 )
- line[n] =3D c;
- n++;
- }
- printf("length =3D %d\n", n);
- }
|
|
12
|
- There is no limit, in principle, to the number of indices which an a=
rray
can have. (Though there is a limit to the amount of memory available=
for
their storage.) An array of two dimensions could be declared as foll=
ows:
- float numbers[SIZE][SIZE];
- SIZE is some constant. (The sizes of the two dimensions do not have =
to
be the same.) This is called a two dimensional array because it has =
two
indices, or two labels in square brackets. It has (SIZE * SIZE) or
size-squared elements in it, which form an imaginary grid, like a ch=
ess
board, in which every square is a variable or storage area.
|
|
13
|
- ------------------------------------
- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ... (up to SIZE)
- ------------------------------------
- | 1 | | | | | | | | |
- ------------------------------------
- | 2 | | | | | | | | |
- ------------------------------------
- | 3 | | | | | | | | |
- ------------------------------------
- | 4 | | | | | | | | |
- ------------------------------------
- | 5 | | | | | | | | |
- ------------------------------------
- | 6 | | | | | | | | |
- ------------------------------------
- | 7 | | | | | | | | |
- ------------------------------------
- .
- .
- (up to SIZE)
|
|
14
|
- Every element in this grid needs two indices to pin-point it. The
elements are accessed by giving the coordinates of the element in the
grid. For instance to set the element 2,3 to the value 12, one would
write:
- array[2][3] =3D 12;
- The usual terminology for the two indices is that the first gives the
row number in the grid and that the second gives the column number in
the grid. (Rows go along, columns hold up the ceiling.) An array can=
not
be stored in the memory as a grid: computer memory is a one dimensio=
nal
thing. Arrays are therefore stored in rows. The following array:
- ------------
- | 1 | 2 | 3 |
- ------------
- | 4 | 5 | 6 |
- ------------
- | 7 | 8 | 9 |
- ------------
- would be stored:
- ------------------------------------
- | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
- ------------------------------------
- * ROW # 1 * ROW # 2 * ROW #3 *
|
|
15
|
- The following declares a two-dimensional 10 by 10 array of integers =
and
sets the first and last elements to be 13.
- int board [10][10];
- board[0][0] =3D 13;
- board[9][9] =3D 13;
|
|
16
|
- You might ask why we use a for since it’s so much like a while.
(You might also ask why we use a while because...) The for is usually
preferable because it keeps the code where it’s used and somet=
imes
eliminates the need for compound statements, as in this code that ze=
ros
a two-dimensional array:
- for( i=3D0; i<n; i++ )
- for( j=3D0; j<m; j++ )
- array[i][j] =3D 0;
|
|
17
|
- main ()
- { int i,j;
- float array[SIZE1][SI=
ZE2];
- for (i =3D 0; i < SIZE1; i++)
- {
- for (j =3D 0; j=
<
SIZE2; j++)
- {
-
array[i][j] =3D 0;
- }
- }
- }
|
|
18
|
- Multiple-dimension arrays are provided, though not much used above t=
wo
dimensions. The declaration and use look like
- int name[10] [20];
- n =3D name[i+j] [1] + name[k] [2];
- Subscripts can be arbitrary integer expressions. Multidimensional ar=
rays
are stored by row (opposite to Fortran), so the rightmost subscript
varies fastest; name has 10 rows and 20 columns.
|
|
19
|
- The internal braces are unnecessary, but help to distinguish the rows
from the columns. The same thing could be written:
- int array[3][3] =3D
- {
- 10,23,42,
- 1,654,0
- 40652,22,0
- };
|