Arrays: Datatype Array Name Size of The Array
Arrays: Datatype Array Name Size of The Array
int tests[5];
allocates the following memory capable of holding five int values that are
assigned default subscript values starting at 0
subscripts
0 1 2 3 4
First element Second element Third element Fourth element Fifth element
1
Array elements can be used as regular variables:
tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
Size Declarators
Array Initialization
Here val [0] =’A’ , val [1] = ‘Z’ , val [2] = ’&’ , and val [3] = ‘*’
2
Values are stored in the order in which they appear.
Initialization list cannot exceed the size of the array.
Off-By-One Errors
An off-by-one error happens when you use array subscripts that are off by
one.
If array is initialized with fewer initial values than the size declarator, the
remaining elements will be set to 0:
12 17 15 11
Must use either array size declarator or initialization list at array definition
3
Using a Loop to Step Through an Array
Input/Output
Inputting, outputting, initializing, processing an array is done through for loop –
which effectively means you can’t display the entire array at once by just it’s
variable name.
All the elements need to be processed one at a time
Array Assignment
Arrays DON’T support aggregate operations (like array assignment, display)
Data Elements inside an array need to be accessed via their subscript
operator.
4
An entire array cannot be assigned to another array (tests and val are
two arrays)
val = tests // does not work
for ( int i = 0; i < ARRAY_SIZE; i++)
val[i]= tests[i]= ;
When you use a value as an array subscript, C++ does not check it to make
sure it is a valid subscript.
In other words, you can use subscripts that are beyond the bounds of the
array
NOTE:
Arrays are in static mode when their size is declared; dynamic arrays without
predefined size are known as vectors.
Unlike arrays, vectors can grow or shrink in size dynamically via some specialized
functions.
You have to be careful so that the last index position does not exceed the
declared size of the array. C++ does NOT do any bounds checking – you will end
up getting a runtime error for accessing unallocated memory.
5
Summing and Averaging Array Elements
Use a simple loop to add together array elements:
int tnum;
double average, sum = 0;
for(tnum = 0; tnum < SIZE; tnum++)
sum += tests[tnum];
int count;
int highest;
highest = numbers[0];
When this code is finished, the highest variable will contains the highest
value in the numbers array.
Comparing Arrays
6
// Compare the two arrays.
while (arraysEqual && count < SIZE)
{
if (firstArray[count] != secondArray[count])
arraysEqual = false;
count++;
}
if (arraysEqual)
cout << "The arrays are equal.\n";
else
cout << "The arrays are not equal.\n";
//Prog 1 Logic
7
//Prog 1 – Uses two parallel arrays: one for hours and one for pay rate
8
Arrays as Function Arguments
To define a function that takes an array parameter, use empty [] for array
argument:
void showScores(int []); // function prototype
void showScores(int tests[]) // function header
When passing an array to a function, it is common to pass array size so that
function knows how many elements to process:
showScores(tests, ARRAY_SIZE);
9
void showScores(int [], int); // function prototype
10
Modifying Arrays in Functions
Two-Dimensional Arrays
Can define one array for multiple sets of data
11
2D Array Initialization
Two-dimensional arrays are initialized row-by-row
Can omit inner { }, some initial values in a row – array elements without
initial values will be set to 0 or NULL
84 78
92 97
Input File
12
Prog 3: Program reads data from a file into an array
13