Arrays - EECE 230 S. Karaki 4. Arrays 1 4. ARRAYS 4.1...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
EECE 230 – S. Karaki 4. Arrays 1 Name of array (Note that all elements of this array have the same name, C) Position number of element within array C 4. ARRAYS 4.1 Introduction Arrays are data structures consisting of several data items of the same type. 4.2 Arrays An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array, as shown in the figure below. C[0] -45 C[1] 6 C[2] 0 C[3] 72 C[4] 1543 C[5] -89 C[6] 0 C[7] 62 C[8] -3 C[9] 1 C[10] 6453 C[11] 78 The position number contained within square brackets is more formally called a subscript . A subscript must be an integer or an integer expression. For example, if we assume that variable a is equal to 5 and that variable b is equal to 6 , then the following statement adds 2 to array element C[11] C[a +b] + = 2; To divide the value of the seventh element of array C by 2 and assign the result to the variable x , we would write: x = C[6] / 2; The brackets used to enclose the subscript of an array are actually considered to be an operator in C++. Brackets have the same level of precedence as parentheses.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EECE 230 – S. Karaki 4. Arrays 2 4.3 Declaring Arrays To tell the compiler to reserve 12 elements for integer array C , the following declaration is used: int C[12] ; The following declaration reserves 100 elements for integer array b and 27 elements for integer array x . int b[100], x[27] ; 4.4 Example Using Arrays // initializing an array #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; int main( ) { int i, n[ 10 ]; for ( i = 0; i < 10; i++ ) // initialize array n[ i ] = 0; cout << "Element" << setw( 13 ) << "Value" << endl; for ( i = 0; i < 10; i++ ) // print array cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; return 0; } Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0
Background image of page 2
EECE 230 – S. Karaki 4. Arrays 3 The elements of an array can also be initialized in the array declaration. If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to zero. For example, the elements of the array n in the previous program could have been initialized to zero with the declaration: int n[10] = {0}; This declaration explicitly initializes the first element to zero and implicitly initializes the remaining nine elements to zero. Automatic arrays are not implicitly initialized to zero. // Initializing an array in the declaration #include <iostream> #include <iomanip> using namespace std; int main() { int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; cout << "Element" << setw( 13 ) << "Value" << endl; for ( int i = 0; i < 10; i++ ) cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; return 0; } Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37 The following array declaration would cause a syntax error because there are 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/26/2010 for the course EECE 230 taught by Professor Samikaraki during the Fall '07 term at American University of Beirut.

Page1 / 21

Arrays - EECE 230 S. Karaki 4. Arrays 1 4. ARRAYS 4.1...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online