0% found this document useful (0 votes)
23 views16 pages

Ch-3 Array

Arrays allow storing multiple values of the same type contiguously in memory. An array is defined with a name, data type, and size. Elements are accessed using indexes from 0 to the array size minus one. Multidimensional arrays generalize this to multiple indexes. Arrays are useful for storing tabular data like temperatures for different cities and seasons stored in a 2D array.

Uploaded by

ERMIAS Amanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

Ch-3 Array

Arrays allow storing multiple values of the same type contiguously in memory. An array is defined with a name, data type, and size. Elements are accessed using indexes from 0 to the array size minus one. Multidimensional arrays generalize this to multiple indexes. Arrays are useful for storing tabular data like temperatures for different cities and seasons stored in a 2D array.

Uploaded by

ERMIAS Amanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CHAPTER 3

ARRAYS
Arrays
• An array consists of a set of objects (called its elements), all of which
are of the same type and are arranged contiguously in memory.
• In general, only the array itself has a symbolic name, not its elements.
• Each element is identified by an index which denotes the position of
the element in the array.
• The number of elements in an array is called its dimension. The
dimension of an array is fixed and predetermined; it cannot be
changed during program execution.
• Arrays are suitable for representing composite data which consist of
many similar, individual items.
• E.g. a list of names, a table of world cities, etc…
Array [cont.…]
• An array variable is defined by specifying its dimension and the type
of its elements.
• For example, an array representing 10 height measurements (each
being an integer quantity) may be defined as:
• int heights[10];
• The individual elements of the array are accessed by indexing the
array. The first array element always has the index 0.
• Therefore, heights[0] and heights[9] denote, respectively, the first and
last element of heights.
• Each of heights elements can be treated as an integer variable. So, for
example, to set the third element to 177, we may write:
• heights[2] = 177;
• Attempting to access a non-existent array element (e.g., heights[-1] or
heights[10]) leads to a serious runtime error (called ‘index out of
bounds’ error).
Array Elements
• The items in an array are called elements (in contrast to the items in a
structure, which are called members).
• As we noted, all the elements in an array are of the same type; only
the values vary.
• Next figure shows the elements of the array age. (In the figure type
int is assumed to occupy two bytes, as in 16-bit systems.)
Basic operation
• Initializing Arrays
• By listing
• By loop
• Accessing Array Elements
• cout << “\nYou entered ” << age[j];
• A C++ string is simply an array of characters.
For example,
char str[] = "HELLO";
• defines str to be an array of six characters: five letters and a null character.
The terminating null character is inserted by the compiler.
• By contrast,
char str[] = {'H', 'E', 'L', 'L', 'O'};
• defines str to be an array of five characters.
Multidimensional Arrays
• An array may have more than one dimension (i.e., two, three, or
higher).
• The organization of the array in memory is still the same (a contiguous
sequence of elements), but the programmer’s perceived organization
of the elements is different.
• For example, suppose we wish to represent the average seasonal
temperature for three major Australian capital cities
• This may be represented by a two-dimensional array of integers:
Spring Summer Autumn Winter
int seasonTemp[3][4]; Sydney 26 34 22 17
Melbourne 24 32 19 13
Brisbane 28 38 25 20
CONT….

• The organization of this array in memory is as 12 consecutive integer


elements.
• The programmer, however, can imagine it as three rows of four
integer entries each
... 26 34 22 17 24 32 19 13 28 38 25 20 ...

First row Second row Third row


• As before, elements are accessed by indexing the array. A separate
index is needed for each dimension. For example, Sydney’s average
summer temperature (first row, second column) is given by
Spring Summer Autumn Winter
seasonTemp[0][1]. Sydney 26 34 22 17
Melbourne 24 32 19 13
Brisbane 28 38 25 20
CONT….
• The array may be initialized using a nested initializer:
int seasonTemp[3][4] = {
{26, 34, 22, 17},
{24, 32, 19, 13},
{28, 38, 25, 20} };
• Because this is mapped to a one-dimensional array of 12 elements in
memory, it is equivalent to:
int seasonTemp[3][4] = {
26, 34, 22, 17, 24, 32, 19, 13, 28, 38, 25, 20};
CONT….

• The nested initializer is preferred because as well as being more


informative, it is more versatile.
• For example, it makes it possible to initialize only the first element of
each row and have the rest default to zero:
int seasonTemp[3][4] = {{26}, {24}, {28}};
26 0 0 0
24 0 0 0
28 0 0 0
CONT….
• We can also omit the first dimension (but not subsequent
dimensions) and let it be derived from the initializer:
int seasonTemp[][4] = {
{26, 34, 22, 17},
{24, 32, 19, 13},
{28, 38, 25, 20}
};
• Processing a multidimensional array is similar to a one-dimensional
array, but uses nested loops instead of a single loop.
• The array is defined with two size specifiers, each enclosed in
brackets:
• double sales[DISTRICTS][MONTHS];
END
Output:

1.1 2.2 3.3


Quiz answer G1
• In the array declaration 1.1 3.3 3.3
double score[5];
• identify the following:
a. The array name: score
b. The base type (data type): double
c. The declared size of the array: 5
d. The range of values an index accessing this array can have: 0 through 4
e. One of the indexed variables (or elements) of this array :
1.1 2.2 3.3
Any of score[0], score[1], score[2], score[3], score[4]
• Identify any errors in the following array declarations.
1.1 2.2 2.2
• a. int x[4] = { 8, 7, 6, 4, 3 }; //Incorrect: One too many initializers
• b. int x[] = { 8, 7, 6, 4 }; //Correct
• c. const int SIZE = 4;
int x[SIZE]; //Correct
• What is the output of the following code?
double a[3] = {1.1, 2.2, 3.3};
cout << a[0] << " " << a[1] << " " << a[2] << endl;
a[1] = a[2];
cout << a[0] << " " << a[1] << " " << a[2] << endl;
Quiz answer G2
• In the array declaration
int grade[5];
• identify the following:
a. The array name: grade
b. The base type (data type): int
c. The declared size of the array: 5
d. The range of values an index accessing this array can have: 0 through 4
e. One of the indexed variables (or elements) of this array
Any of grade[0], grade[1], grade[2], grade[3], grade[4]
• Identify any errors in the following array declarations.
• a. int x[4] = { 8, 7, 6, 4 };//correct
• b. int x[5] = { 9,22,36,34,12 }; //correct
• What is the output of the following code?
double a[3] = {1.1, 2.2, 3.3}; Output:
cout << a[0] << " " << a[1] << " " << a[2] << endl;
a[1] = a[2]; 1.1 2.2 3.3
cout << a[0] << " " << a[1] << " " << a[2] << endl;
1.1 3.3 3.3

You might also like