7-Arrays
7-Arrays
1. Array Name .
2. Type of array .
3. size of the arrays .
Defining Arrays
3
int A[10];
• An array of ten integers .
Char str[20];
• An array of twenty characters .
int a[ 100 ], b[ 27 ] ;
• Defining multiple arrays of same type .
Defining Arrays - Examples-
5
temperature [ 4 ] 87.5
Index
Initializing Arrays
8
int N[ ] = { 1, 2, 3, 4, 5 };
If size omitted, the size is determined from the 5 initializers .
int N[5] = { 0 } ;
int B[20] = {2, 4, 8, 16, 32};
Unspecified elements are guaranteed to be zero .
If not enough initializers, rightmost elements become 0 .
int C[4] = {2, 4, 8, 16, 32};
Error — compiler detects too many initial values .
C++ arrays have no bounds checking .
int D[5] = {2*n, 4*n, 8*n, 16*n, 32*n};
Automatically only ; array initialized to expressions .
Accessing Array Elements
9
or
N = 3;
temperature [N] = 12.7;
Printing elements .
Performing operations :
Sum elements .
Find largest/ smallest element .
Search for an element .
Sort elements .
Manipulating Arrays
15
Reading Elements :