Arrays
Arrays
Array
Consecutive group of memory locations Same name and type (int, char, etc.) Static entity (same size throughout program) Index value is written inside a pair of square beackets []
Array is used to process a large amount of data of same type.
Arrays
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
Array elements like other variables
Assignment, printing for an integer array c
c[ 0 ] = 3; cout << c[ 0 ];
Can perform operations inside subscript
c[ 5 2 ] same as c[3]
Types
Arrays are usually divided in to two types
One Dimensional Two Dimensional
Introduction to Programming
Arrays (One Dimensional)
Name of array c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] -45 6 0 72 1543 -89 0 62 -3 1 6453 78
Position number of the element within array c
Declaring Arrays
When declaring arrays, specify
Name Type of array
Any data type
Number of elements type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats
Declaring multiple arrays of same type
Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
Examples Using Arrays
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Initializing an array with a declaration. #include <iostream> #include <iomanip> using namespace std; void main() { // use initializer list to initialize array n int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout << "Element \t Value" << endl;
// output contents of array n in tabular format for ( int i = 0; i < 10; i++ ) cout << i << \t << n[ i ] << endl;
} // end main
Element 0 1 2 3 4 5 6 7 8 9
Value 32 27 64 18 95 14 90 70 60 37
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
Arrays
#include <iostream> #include <iomanip> using namespace std; void main() { const int arraySize = 10; int s[ arraySize ]; // array s has 10 elements // set the values
for ( int i = 0; i < arraySize; i++ ) s[ i ] = 2 + 2 * i;
for ( int j = 0; j < arraySize; j++ ) cout << Value at index << j << is << s[ j ] << endl; }
Arrays
Using const
void main() { const int x; x = 7;
// Error: x must be initialized
// Error: cannot modify a const variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// // Compute the sum of the elements of the array. #include <iostream>
using namespace std;
void main() { const int arraySize = 10;
int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
// sum contents of array a for ( int i = 0; i < arraySize; i++ ) total += a[ i ];
cout << "Total of array element values is " << total << endl;
} // end main
String
Array of characters char test[6] = {h, e, l, l, o}; char test[6] = hello;
Introduction to Programming
Example Program
Write a program that prints array elements in reverse order.
#include<iostream.h> void main() { int abc[5], i; for (i=0;i<=4;i++)
Cin>>abc[i];
cout<< Array in reverse order<<endl; for (i=4;i>=0;i--)
Cout<<abc[i]<<endl;
}
Introduction to Programming
Multi-dimensional Arrays
Very useful and practical, e.g.
Matrices Images int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
Multiple loops to access individual elements
Introduction to Programming