0% found this document useful (0 votes)
27 views

Topic 4 Array

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Topic 4 Array

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Topic 4: Array

OBJECTIVES
◦ Demonstrate the use of arrays
◦ Design program using the concept of pointer
◦ Describe the relationship between pointer and array.
What do you see ?

How are the items being group ?

What are the reasons ?


What is an array ?

Consist of contiguous group of memory locations that all have the same type.

To refer to a particular location or element in the array:-


specify the name of the array and the index number of the particular element in the array.

The lowest address corresponds to the first element

The highest address to the last element.

Instead of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1],
and ..., numbers[99] to represent individual variables
Explanation

Index number of the element Number[ 0 ] 55


within the array
Number[ 1 ] 6

Number[ 2 ] 18 Value
Name of an individual array Number[ 3 ] 10
element
Number[ 4 ] 23

Name of the array is Number 5 elements

How many elements in the array Number?


ARRAY DECLARATIONS
A typical declaration for an array in C++ is:

data_type arrayName [arraySize];

defines how many elements


specifies the type of the
data_type
elements (like int, float...) arraySize
the array can contain ;
enclosed in square brackets [ ]

the name of the


arrayName
declared array

• arraySize must be an integer constant greater than


zero
• data_type can be any valid C++ data type
ARRAY DECLARATIONS
For example ;
 To declare an array called politeknik of type integer:

int politeknik [33]; // politeknik is an array of 33 integer

 To declare a 10-element array called total of type float:

  float total[10]; // total is an array of 10 floating-point


INITIALIZING ARRAYS
In C++, an array may be initialize at the time of its declaration.
A variable can be initialized in its declaration. For example:

int value = 7;

An array can be initialized by a list of initial values for array elements and they are separated
with commas and enclosed within braces. For example:

int lines [7] = { 1, 2, 3, 4, 5, 6, 7};


INITIALIZING ARRAYS
There are a few ways to initialize an arrays.

int numbers[5] = {3,7,12,24,45}; int numbers[ ] = {3,7,12,24,45};

3 7 12 24 45 3 7 12 24 45

int numbers[5] = {3,7}; int lotsOfnumbers[1000] = {0};

3 7 0 0 0 0 0 …. 0 0

The rest are filled


with 0s All filled with 0s
INITIALIZING ARRAYS
In both cases, local and global, when we declare an array, we have the possibility to assign initial values to
each one of its elements by enclosing the values in braces { }. For example:

  int billy [5] = { 16, 2, 77, 40, 12071 };

This declaration would have created an array like this:


INITIALIZING ARRAYS
If the array size and an initializer list are specified in an array declaration, the number of initializers must be
less than or equal to the array size.

  int billy [5] = { 16, 2, 77, 40, 12071, 1500 };

causes a compilation error, because there are six initializers and


only five array elements.

If the size of array not being state, the compiler


determines the number of elements in the array by
counting the number of elements in the initializer list.
INITIALIZING ARRAYS
Another example:

  double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

This declaration would have created an array like this:


INITIALIZING ARRAYS
int numbers[5] = {3,7,12,24,45}; int numbers[ ] = {3,7,12,24,45};

3 7 12 24 45 3 7 12 24 45

int numbers[5] = {3,7}; int lotsOfnumbers[1000] = {0};

3 7 0 0 0 0 0 …. 0 0

Can you discuss the advantage &


disadvantage of the below initializing
(if any) ?
INITIALIZING ARRAYS
Here are same examples of array initialization:

i) int digit [9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };


ii) double points [8] = { 1.2, 1.4, 1.7, 2.6, 3.4, 2.6, 1.2, 3.1 };
iii) char letter[9] = { ‘a’, ’b’, ’c’, ’d’, ’e’, ’f’, ’g’, ’h’, ’i’ };

Can you draw the structure of array


value initialize above
INITIALIZING ARRAYS

What is the output ?


ACCESSING ARRAY ELEMENTS
An element is accessed by indexing the array name.
This is done by placing the index of the element within square brackets after the name of the array.
For example:
 

double salary = balance[0] ; balance[2] = balance[3] + 20


bonus_salary = salary + 500;

What is the value of What is the value of


bonus_salary ? balance[2] ?
ONE DIMENSIONAL ARRAY
In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it
was a normal variable, thus being able to both read and modify its value. The format is as simple as:

arrayname[index]

For example: an array called billy had 5 elements and each of those elements was of type int, the name which we can
use to refer to each element is the following:

For example :
To store the value 65 in the fourth element of billy ?

billy[3] = 65;
ONE DIMENSIONAL ARRAY
to pass the value of the third element of billy to a variable called a, we could write:

a = billy[2];

Notice that the third element of billy is specified billy[2], since the first
one is billy[0], the second one is billy[1], and therefore, the third one is
billy[2]. By this same reason, its last element is billy[4].

Therefore, if we write billy[5], we would be accessing the


sixth element of billy and therefore exceeding the size of the array.
ONE DIMENSIONAL ARRAY
In C++ it is syntactically correct to exceed the valid range of indices for an array.

This can create problems, since accessing out-of-range elements


do not cause compilation errors but can cause runtime errors.

At this point it is important to be able to clearly distinguish between the two uses that
brackets [ ] have related to arrays.

They perform two different tasks:


i. To specify the size of arrays when they are declared
ii. To specify indices for concrete array elements.
ONE DIMENSIONAL ARRAY
If you read carefully, you will see that a type specifies always precedes
a variable or array declaration, while it never precedes an access.

Some other valid operations with arrays:

billy[0] = a; billy[a] = 75; b = billy [a+2]; billy[billy[a]] = billy[2] + 5;


ONE DIMENSIONAL ARRAY

What is the output ?


Summary
Do you have any question ?
OBJECTIVES
◦ Declare two dimensional
◦ Initialize two dimensional array
◦ Illustrate the two dimensional array
◦ Access individual elements of two dimensional arrays
◦ Manipulate array elements
◦ Solve a given problem by writing algorithm and program, run, test
and debug using array
TWO DIMENSIONAL ARRAY
It is possible to have arrays of more than one dimension. Each dimension is represented as a subscript in
the array. (to represent tables of values consisting of information arranged in rows and columns)

A two-dimensional array has two subscripts; a three-dimensional array has three subscripts and so on.
int val = a[2][3]; int threedim[5][10][4];

Arrays can have any number of dimensions. A typical declaration for multidimensional array is :
we call it a
multidimensional array
data_type arrayname[size1][size2]...[sizeN];

For example :
int billy [3][4];
A good example of a two-dimensional array is a chess
board.
One dimension represents the eight rows; the other
dimension represents the eight columns.
INITIALIZING TWO DIMENSIONAL
ARRAY
A two-dimensional array can also be initialize.

Multidimensioned arrays may be initialized by specifying bracketed values for each row.

Following is an array with 3 rows and each row have 4 columns.

int a[3][4] = {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */

{8, 9, 10, 11} /* initializers for row indexed by 2 */

};

The following initialization is equivalent to previous example:


int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
TWO DIMENSIONAL ARRAY
Example :
int a [3][4] = { 1,2,3,4,11,12,13,14,21,22,23,24 };

Analysis:

a [0][0] = 1 a [0][1] = 2 a [0][2] = 3 a [0][3] = 4


a [1][0] = 11 a [1][1] = 12 a [1][2] = 13 a [1][3] = 14
a [2][0] = 21 a [2][1] = 22 a [2][2] = 23 a [2][3] = 24
ACCESSING TWO DIMENSIONAL
ARRAY
An element in 2-dimensional array is accessed by using the subscripts ie. row index and column index of
the array. For example:
int val = a[2][3];

The above statement will take 4th element from the 3rd row of the array.

You can verify it in the above


diagram. (refer previous slide)
ACCESSING TWO DIMENSIONAL
ARRAY

What is the output ?


ACCESSING TWO DIMENSIONAL
ARRAY

What is the output ?


Sample program code
EXAMPLE THAT USE ALL THE ABOVE MENTIONED THREE CONCEPTS WHICH IS:
DECLARATION, ASSIGNMENT AND ACCESSING ARRAYS

Program code Output


Sample program code
COPY ONE ARRAY INTO ANOTHER

Program code Output


EXERCISE
Write a C++ program to display the following output using array.

1. C[0]=0 C[1]=0 C[2]=0 C[3]=0 C[4]=0 C[5]=0


2. C[0]=25 C[1]=50 C[2]=2 C[3]=8 C[4]=30 C[5]=0

Base on your workbook of DFC2073 Lab Activities :


Complete Activity 7 & 8
Complete Lab Activity 4A in DFC2073 workbook

You might also like