Professional Documents
Culture Documents
Arrays
Arrays
• An array is a collection of variables of similar
type, placed contiguously in memory.
• Arrays are used to avoid the inconvenience of
giving each data element a unique variable
name.
• One data structure having more than one data
members of the same type
Array Definition
• Array declaration tells the compiler about
• the type of array.
• the name of array.
• the size of array.
type arrayName [ arraySize ];
<component type> <variable identifier>[<integer value>];
• Example:
float arr[ 20 ];
Size of Array = 12
32 27 64 0 0
Entering Data into an Array
• Data can be entered into individual elements of
an array using a loop
• Enter marks of five students into an array
int marks[5];
for ( int i = 0; i < 5; i++ )
{
cout << "Enter marks of " <<i+1 <<" student = ";
cin >> marks [i] ;
}
Reading Data from an Array
Reading data from the array “marks” and
calculating the average.
sum = 0;
for ( i = 0; i < 5; i++ )
sum += marks [i];
cout<<"Average is "<<sum/5<<endl;
Summing the Elements of an Array
const int Size = 10; //variable indicating
size of array
int main()
{
int a[Size] = {87,68,94,100,83,78,85,91,76,87};
int total = 0;
// sum contents of array a
for ( int i = 0; i < Size; i++ )
total += a[ i ];
return 0;
}
Multi Dimensional Arrays (Matrices)
• For example,
arr[i][j]
19
Multiple-Subscripted Arrays
• Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
Column subscript
Array
name Row subscript
20
• int a[3][4];
Column subscript
Array
name Row subscript
21
b[0][0] b[0][1]
1 2
3 4
b[1][0] b[1][1]
22
• Referencing elements
– Specify row, then column
cout<<b[ 0 ][ 1 ] ;
Initializing a 2D Array
• int matrix [10] [2] = { {59 , 78},
{14 , 17},
{68 , 28},
{32 , 45},
{ 5 , 14},
{12 , 15},
{ 6, 2},
{ 22, 1},
{14 , 16},
{ 2, 58} };
59 78 14 17
(0,0) (0,1) (0,2) (0,3)
68 28 32 45
(1,0) (1,1) (1,2) (1,3)
(row = 2, col = 0)
5 14 12 15
(2,0) (2,1) (2,2) (2,3)
6 2 22 1
(3,0) (3,1) (3,2) (3,3)
4 x 4 Matrix
Reading a 2D Array
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
Example
• Write a matrix with following entries
12
54 34
16 3
12 6
43 23
1 2
2D String
2D array of characters
1 /* Fig. 6.21: fig06_21.c 28
2 Initializing multidimensional arrays */
3 #include <stdio.h>
4
5 void printArray( const int a[][ 3 ] ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 /* initialize array1, array2, array3 */
11 int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
12 int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
13 int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
14
15 cout<<"Values in array1 by row are:\n" ;
16 printArray( array1 );
17
18 cout<<"Values in array2 by row are:\n" ;
19 printArray( array2 );
20
21 cout<<"Values in array3 by row are:\n" ;
22 printArray( array3 );
23
24 return 0; /* indicates successful termination */
25
26 } /* end main */
27
28 /* function to output array with two rows and three columns */
29
29 void printArray( const int a[][ 3 ] )
30 {
31 int i; /* counter */
32 int j; /* counter */
33
34 /* loop through rows */
35 for ( i = 0; i <= 1; i++ ) {
36
37 /* output column values */
38 for ( j = 0; j <= 2; j++ ) {
39 cout<<a[ i ][ j ] ;
40 } /* end inner for */
41
42 cout<<"\n" ; /* start new line of output */
43 } /* end outer for */
44
45 } /* end function printArray */
Program Output
35