Anda di halaman 1dari 22

C++ Arrays

Array Basics
• An array is a block of contiguous
memory locations to hold values of
a single specified type.
• Arrays can hold values of ANY type
• In C++, arrays are 0-based. This
means that the first element has
index 0.
Declaring an Array
• To declare an array, use the following
structure
type VariableName [ SIZE ];
Example:
int myArray[5];
declares an array named myArray
that can hold 5 integers with indices
0, 1, 2, 3, and 4.
Declaring an Array
• Note: We are using static arrays.
IE the size must be set when the
array is declared, and can not
change during the course of the
program.
• Use constant variables for the
array’s size so that modifications to
the size can be made in one place.
Using an Array
• In C++, the name of the array
refers to the address of the first
element in the array.
• In order to access an element in
the array, follow the name of the
array with square brackets
containing the element’s index.
Using an Array
In our example, int myArray[5];
declares an array of 5 integers.

M32 M36 M40 M44 M48


myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
Using an Array
• Declaring an array does not
automatically initialize the values.
• We can initialize our array in the
declaration statement by assigning
it a list of values. The list must be
held in braces, {}. The values in
the list must be separated by
commas.
Using an Array
Example:
int myArray[5] =
{14,3,7,0,1};

Note: You can only initialize the


array with a list in the array
declaration.
Using an Array
• If we are initializing an array of
numeric values, any values left out
of the list will be set to 0.
Example: int Array[5] = {14};

14 0 0 0 0
Using an Array
• You can leave out the size of the
array when using a list to initialize
the array in the declaration. The
array’s size will be the number of
values in the initialization list.
Example:
int Array[] = {14, 3, 6, 9, 2};
14 3 6 9 2
Using an Array
• To read values into a non-character
array, use a counter controlled loop
to move through the array indices.
Example:
for( int i = 0; i < 5; ++i )
{
cout << “Enter next number”;
cin >> myArray[ i ];
}
Using an Array
• To output the values in a non-
character array, use a counter
controlled loop to move through
the array indices.
Example:
for ( int i = 0; i < 5; ++i)
{
cout << myArray[ i ] << “ “;
}
Using an Array
• Common Error: Overstepping the
bounds of an array.
• Your compiler will not produce an
error if an index is used outside of
the 0 to size – 1 range.
• Invalid indices are used as any
other, moving the specified offset
from the first element in the array.
Using an Array
• Overstepping an array’s bounds
can attempt to modify values in
memory locations being used by
other variables or programs.
Using an Array
Example:
int myArray[] = {1,2,3,4,5};
for ( int i = 0; i <=5; ++i )
{
myArray[i] *= 2;
}
Before 1 2 3 4 5

After 2 4 6 8 10 ???
Passing Arrays to Functions

• To specify an array argument,


place a set of empty brackets
beside the array’s data type in the
function’s parameter list (for
single-subscripted arrays).
Example:
void doubleValues( int [], int );
Passing Arrays to Functions

• When calling the function, use the


name of the array (no brackets) as
an argument of the function.
Example:
int myArray[8]={1,2,3,4,5,6,7,8};
doubleValues( Array, 8 );
Passing Arrays to Functions

• When passing an array argument


to a function we’re sending the
address of the first element in the
array, allowing the function direct
access to the array elements.
• Remember the Principle of Least
Privilege! You can make array and
reference parameters constant.
void printArray( const int [], int );
Searching Arrays
• Unsorted arrays – linear search.
• Linear Search Method–
Start at one end of the array.
If the current element contains the
search value, return its array index
If not, move to the next element.
Continue until the element is found
or there are no more elements to
examine.
Searching Arrays
• Sorted arrays – binary search.
• Binary Search –
If the middle element contains
the value you’re searching for
return its array index.
Else, if the middle element is
larger than the value you’re
searching for, search the left sub-
array.
Else search the right sub-array.
Sorting Arrays
Bubble Sort - Informal Algorithm
Start at the end of the array
swapping all pairs of unordered
elements until you reach the
beginning of the array.
Now the smallest element is at the
front.
Repeat the process until all of the
elements are in order.
Sorting Arrays
Selection Sort - Informal Algorithm
front = position 0
Find the smallest element and swap it
to the front. Increment the front
Repeat until front = size - 1

Anda mungkin juga menyukai