An array is a collection of elements of the same type stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple values as a single variable. One-dimensional arrays store elements in a list, while multi-dimensional arrays arrange elements in multiple dimensions. Elements are accessed using their position indices, which must be within the array bounds. Arrays can be initialized during declaration and values accessed using loops. Operations like input, output and searching are commonly performed on array elements.
What Is AnArray?
• An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
• Array Used to store a collection of elements (variables).
• That means that, for example, five values of type int can be
declared as an array without having to declare 5 different
variables (each with its own identifier). Instead, using an
array, the five int values are stored in contiguous memory
locations, and all five can be accessed using the same
identifier, with the proper index.
o int array [5];
8-2
Array int 0 1 2 3 4
3.
Basic Array Definition
Typeof
values in
list
BaseType Id [ SizeExp ] ;
Name
of list
Bracketed constant
expression
indicating number
of elements in list
double X [ 100 ] ;
// Subscripts are 0 through 99
4.
Declaring an array
•The statement
int num[5];
declares an array num of 5 components of
the type int
• The components are num[0], num[1],
num[2], num[3], and num[4]
5.
Array Storage inMemory
The definition
int tests[ISIZE]; // ISIZE is 5
allocates the following memory
8-5
Element 0 Element 1 Element 2 Element 3 Element 4
6.
Processing One-Dimensional Arrays
•Some basic operations performed on a
one-dimensional array are:
– Initialize
– Input data
– Output data stored in an array
– Find the largest and/or smallest element
• Each operation requires ability to step
through the elements of the array
• Easily accomplished by a loop
7.
Accessing Array Components(continued)
• Consider the declaration
int list[100]; //list is an array
//of the size 100
int i;
• This for loop steps through each element
of the array list starting at the first element
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
8.
Accessing Array Components(continued)
• If processing list requires inputting data
into list
– The statement in Line 2 takes the from of
an input statement, such as the cin
statement
for (i = 0; i < 100; i++) //Line 1
cin >> list[i];
9.
Array Initialization
• Aswith simple variables
– Arrays can be initialized while they are being declared
• When initializing arrays while declaring them
– Not necessary to specify the size of the array
• Size of array is determined by the number of initial values
in the braces
• For example:
double sales[] = {12.25, 32.50, 16.90, 23,
45.68};
10.
Partial Initialization (continued)
•The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
• The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
– The first two components are initialized to 4 and 7
respectively
– All other components are initialized to 0
11.
How To DeclareArrays
• To declare an array in C++, you should specify the
following things
– The data type of the values which will be stored in the array
– The name of the array (i.e. a C++ identifier that will be used to
access and update the array values)
– The dimensionality of the array:
• One dimensional (i.e. list of values ),
• Two-dimension array (a matrix or a table), etc.
– The size of each dimension
• Examples
– int x[10];int x[10]; // An integer array named x with size 10
– float GPA[30];float GPA[30]; // An array to store the GPA for 30 students
– int studentScores[30][5];int studentScores[30][5]; // A two-dimensional array to store the
scores of 5 exams for 30 students
12.
One-dimensional Arrays
• Weuse one-dimensional (ID) arrays to store and
access list of data values in an easy way by
giving these values a common name, e.g.
int x[4];int x[4]; // all values are named x
x[0] = 10;x[0] = 10; // the 1st
value is 10
x[1] = 5;x[1] = 5; // the 2nd
value is 5
x[2] = 20;x[2] = 20; // the 3rd
value is 20
x[3] = 30;x[3] = 30; // the 4th
value is 30
10
5
20
30
x[0]
x[1]
x[2]
x[3]
13.
Example: Read Valuesand Print
them in Reverse Order
#include <iomanip.h>
void main()
{
int x[5], index;
cout << “Please enter five integer values: “;
for( index=0; index<5; index++) cin >> x[index];
cout << “The values in reverse order are: “;
for (index=4; index>=0; index--)
cout << setw(3) << x[index];
cout << endl;
}
14.
Two-Dimensional Arrays
• Two-dimensionalarrays are stored in
row order
– The first row is stored first, followed by the
second row, followed by the third row and
so on
• When declaring a two-dimensional array
as a formal parameter
– can omit size of first dimension, but not the
second
• Number of columns must be specified
Multidimensional Arrays
• MultidimensionalArray: collection of a fixed
number of elements (called components)
arranged in n dimensions (n >= 1)
• Also called an n-dimensional array
• General syntax of declaring an n-
dimensional array is:
dataType arrayName[intExp1][intExp2]...[intExpn];
where intExp1, intExp2, … are constant
expressions yielding positive integer values
17.
Multidimensional Arrays (continued)
•The syntax for accessing a component of
an n-dimensional array is:
arrayName[indexExp1][indexExp2]...[indexExpn]
where indexExp1,indexExp2,..., and
indexExpn are expressions yielding
nonnegative integer values
• indexExpi gives the position of the array
component in the ith dimension
18.
Example• #include <iostream>
•using namespace std;
• int main()
• {
• // This array can store upto 12 elements (2x3x2)
• int test[2][3][2];
• cout << "Enter 12 values: n";
•
• // Inserting the values into the test array
• // using 3 nested for loops.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k )
• {
• cin >> test[i][j][k];
• }
• }
• }
• cout<<"nDisplaying Value stored:"<<endl;
• // Displaying the values with proper index.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k)
• {
• cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
• }
• }
• }
• return 0;
• }
8-18
19.
Summary
• An arrayis a structured data type with a fixed number of components
– Every component is of the same type
– Components are accessed using their relative positions in the array
• Elements of a one-dimensional array are arranged in the form of a list
• An array index can be any expression that evaluates to a non-negative
integer
• The value of the index must always be less than the size of the array
• The base address of an array is the address of the first array component
• In a function call statement, when passing an array as an actual
parameter, you use only its name
• As parameters to functions, arrays are passed by reference only
• A function cannot return a value of the type array
• In C++, C-strings are null terminated and are stored in character arrays