Professional Documents
Culture Documents
Computer Programming I
1. Arrays
2. Defining Arrays
4. Sorting Arrays
5. Multidimensional Array
6. Variable-length Arrays
1
Arrays
Arrays
Arrays are data structures consisting of related data items of the same
type, in contiguous memory locations.
2
Arrays
To print the sum of the values contained in the first three elements of
array c , wed write
printf ( " % d " , c [ 0 ] + c [ 1 ] + c [ 2 ] ) ;
3
Level of precedence
4
Defining Arrays
Defining Arrays
The following definition reserves 100 elements for integer array b and 27
elements to integer array x
int b [ 100 ] , x [ 27 ];
If there are fewer initializers than elements in the array, the remaining
elements are initialized to zero
int n [ 10 ] = { 0 }; // i n i t i a l i z e s entire array to zeros
7
Character Arrays
In arrays the & (usually used in scanf clause) is not necesary since an
array is passed by reference.
9
Character Arrays
Function printf, like scanf, does not check how large the character array
is. The characters of the string are printed until a terminating character
is encountered.
10
Passing Arrays to functions
Passing Arryas to functions
11
Passing Arryas to functions
An array name is really the address of the first element of the array, the
following example show the values of array , &array [0] and &array using
identifier %p
1 # include < stdio .h >
2 int main ( void ) {
3 char array [ 5 ]; // define an array of size 5
4 printf ( " array = % p \ n & array [0] = % p \ n & array = % p \ n " ,
array , & array [ 0 ] , & array ) ;
5 } // end main
12
Passing Arrays to functions
13
Sorting Arrays
Sorting Arryas
14
Sorting Arrays
16
Multidimensional Array
Multidimensional Arrays
17
Multidimensional array
The values are grouped by row in braces. If there are not enough
initializers for a given row, the remaining elements of that row are
initialized to 0 . Thus,
int b [ 2 ][ 2 ] = { { 1 , } ,{ 3 , 4 } };
18
Variable-length Arrays
Variable-length Arrays
19