5 Arrays in C++( FoP)
5 Arrays in C++( FoP)
Programming
Arrays
Arrays
– Structures of related data items
– Static entity (same size throughout program)
Arrays
Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
To refer to an element
– Specify array name and position number (index)
– Format: arrayname[ position number ]
– First element at position 0
N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
Arrays
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
Initializing arrays
– For loop
• Set each element
– Initializer list
• Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements 0
– To set every element to same value
int n[ 5 ] = { 0 };
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
1
2 // Initializing an array.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
Declare a 10-element array of
12 int main() integers.
13 {
14 int n[ 10 ]; // n is an array ofInitialize array
10 integers to 0 using a
15 for loop. Note that the array
16 // initialize elements of array nhas
toelements
0 n[0] to n[9].
17 for ( int i = 0; i < 10; i++ )
18 n[ i ] = 0; // set element at location i to 0
19
20 cout << "Element" << setw( 13 ) << "Value" << endl;
21
22 // output contents of array n in tabular format
23 for ( int j = 0; j < 10; j++ )
24 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
25
26 return 0; // indicates successful termination
27
28 } // end main
Element Value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
1
2 // Initializing an array with a declaration.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main() Note the use of the initializer
13 { list.
14 // use initializer list to initialize array n
15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
16
17 cout << "Element" << setw( 13 ) << "Value" << endl;
18
19 // output contents of array n in tabular format
20 for ( int i = 0; i < 10; i++ )
21 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;
22
23 return 0; // indicates successful termination
24
25 } // end main
Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37
Examples Using Arrays
Array size
– Can be specified with constant variable (const)
• const int size = 20;
– Constants cannot be changed
– Constants must be initialized when declared
– Also called named constants or read-only variables
1
2 // Initialize array s to the even integers from 2 to 20.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11 Note use of const keyword.
12 int main()
Only const variables can
13 {
specify array sizes.
14 // constant variable can be used to specify array size
15 const int arraySize = 10;
The program becomes more
16
17 int s[ arraySize ]; // array s
scalable when we set the array
has 10 elements
18 size using a const variable.
19 for ( int i = 0; i < arraySize; i++ ) //We setcan change
the arraySize,
values
20 s[ i ] = 2 + 2 * i; and all the loops will still
21 work (otherwise, we’d have to
22 cout << "Element" << setw( 13 ) update
<< "Value" every loop in the
<< endl;
23 program).
24 // output contents of array s in tabular format
25 for ( int j = 0; j < arraySize; j++ )
26 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
27
28 return 0; // indicates successful termination
29
30 } // end main
Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
1
2 // Using a properly initialized constant variable.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7 Proper initialization of
8 int main() const variable.
9 {
10 const int x = 7; // initialized constant variable
11
12 cout << "The value of constant variable x is: "
13 << x << endl;
14
15 return 0; // indicates successful termination
16
17 } // end main
break/continue statements
Related Examples
Nested Loops
Related Examples
2D Arrays
Related Examples
'break’ statement
#include <iostream>
using namespace std; 1
int main() 2
{
for (int i = 1; i <= 5; i++)
{ // break condition
if (i == 3)
{
break; }
cout << i << endl;
}
return 0;
}
‘continue’ statement
#include <iostream>
1
using namespace std;
int main()
2
{ 4
for (int i = 1; i <= 5; i++) { // 5
condition to continue
if (i == 3)
{
continue; }
cout << i << endl; }
return 0;
}
Nested Loops
#include <iostream>
using namespace std;
int main()
{
* * *
int rows = 5; int columns = 3; * * *
for (int i = 1; i <= rows; ++i)
{
* * *
for (int j = 1; j <= columns; ++j)
Multiple subscripts
– a[ i ][ j ]
– Tables with rows and columns
– Specify row, then column
– “Array of arrays”
• a[0] is an array of 4 elements
• a[0][0] is the first element of that array
Column 0 Column 1 Column 2 Column 3
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
Multiple-Subscripted Arrays
To initialize
– Default of 0
– Initializers grouped by row in braces
1 2
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 3 4
Row 0 Row 1
1 0
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4
Multiple-Subscripted Arrays
– Outputs 0 3 4