Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

DFC20113

PROGRAMMIN
G
FUNDAMENTA
LS

Chapter 4
ARRAY
PROGRAMME LEARNING OUTCOMES
(PLO)

 PLO 2: Practice technical skills by applying


appropriate methodologies, models and techniques
in IT fields

 PLO 3 : Display Information and Communication


Technology (ICT) skill in performing diagnostic
and documenting processes in ICT related fields.
COURSE LEARNING
OUTCOMES (CLO)
 Upon completion of this course, students should be
able to:

 CLO 1 – Implement programming element and


articulate how they are used to achieve working
program. (C3, PLO2)

 CLO 2- Show simple programs by developing code


to solve problems in a computer using C++
programming language. (P2,PLO3)
LEARNING OUTCOMES (LO)
4.1 Explain the use of arrays.
4.1.1 Define an array.
4.1.2 Describe the components of an array:
a. Index
b. Element
c. Size
4.1.3 Explain types of array:
a. one dimensional
b. two dimensional
4.1.4 Declare one dimensional array.
4.1.5 Initialize one dimensional array.
4.1.6 Illustrate the one dimensional array
4.1.7 Access individual element of one dimensional array.
Define an array.
An array is a group of
memory locations that they
all have the same name and
the same data type.
Components of an array:
 Index
 used to refer to elements in an array individually
 Element
 values stored inside an array
 Size
 number of elements in an array
Example:
int Num [4] = { 10, 20 ,30, 40} ;

size

elements

Num[0] Num[1] Num[2] Num[3]


Num 10 20 30 40
One-Dimensional Array
 Will have a single row and can have any number
of columns.

 Will have only one subscript. Subscript refers to


the dimension of the array.
Example : int x [4 ];
Representation of Array
EXERCISE
 Declare an array named “balance” that have 8
integer element.
 int balance[8];

 Declare an array named “temperature” that have


10 float element.

 float temperature [10];


Declare One Dimensional

Syntax :
<Element data type> <Variable name>[Array index] = Value;
 The first element will always have the array index as 0.
 Array index refers to the location of the values in an
array.
For example:
marks[0]=95;
marks[1]=85;
marks[2]=75;
Declare One Dimensional
 Example 1:

char Nama [4] = “ALI” ; or char Nama [4] = {‘A’, ‘L’, ‘I’} ;

‘ A’ ‘ L’ ‘I’ ‘ \0 ’
Char : Nama [0] Nama[1] Nama[2] Nama[3]

‘ \0 ’ is the null terminator.


Declare One Dimensional
 Example 2:

int Num [4] = { 10, 20 ,30, 40} ;

10 20 30 40

Num[0] Num[1] Num[2] Num[3]


Initializing Arrays

You can also initialise the array at the time of


declaration as shown in the following example:

int marks[] = {95,85,75};

When initialising an array, the values are


enclosed within curly brackets { }.
EXERCISE
 Declare an array named “balance” that have 8
integer element. Assume your own element.
 int balance[8]={1,2,3,4,5,6,7,8};

 Declare an array named “temperature” that have 3


float element. Assume your own element.

 float temperature [3]={12.34, 23.12, 27.00};


Accessing Array Elements
 Usingthe array index you can access the
array elements.
 Toprint the value of the second element in an
array, the code will be:

cout<<"marks[1] : " << marks[1]<<endl;


Example:
 Program illustrate how to declare an array, initialise it and
access its elements.

int main()
{

int marks[5]={95,85,75,80,65};
cout<<"marks[0] : " << marks[0]<<endl;
cout<<"marks[1] : " << marks[1]<<endl;
cout<<"marks[2] : " << marks[2]<<endl;
cout<<"marks[3] : " << marks[3]<<endl;
cout<<"marks[4] : " << marks[4]<<endl;

return 0;
system("PAUSE");
}
Entering Data into an Array
 When more number of values are to be
stored in an array, a for loop can be used.
 The sample code shows how to use a for
loop in an array.
for(int i=0;i<=5;i++)
{
cout<< "Enter mark["<<i<<"] :";
cin>>marks[i];
}
Reading Data from an Array
 You can use a for loop with a single println
statement to print the values from an array.

for(int i=0;i<=5;i++) //Displaying the array


{
cout<<"marks["<<i<<"] :" << marks[i]<<endl;
}
Example
 Program illustrates how to accept five marks from the user and prints the values on the screen.

int main()
{
int sum =0;
int marks[6];
for(int i=0;i<=5;i++) //Accepting the marks
{
cout<< "Enter mark["<<i<<"] :";
cin>>marks[i]; }

cout<< "Displaying the value inside an array"<<endl;

for(int i=0;i<=5;i++) //Displaying the array


{
cout<<"marks["<<i<<"] :" << marks[i]<<endl;}

cout<< "Displaying the sum of an array"<<endl;

for(int i=0;i<=5;i++) {
sum+=marks[i];
}
cout<< "The Sum of an array : "<< sum;

return 0;
system("PAUSE");
}
Two-Dimensional Array
LEARNING OUTCOMES (LO)
4.1 Explain the use of arrays.

4.1.8 Declare two dimensional array.


4.1.9 Initialize two dimensional array.
4.1.10 Illustrate the two dimensional array
4.1.11 Access individual element of two dimensional array.
4.1.12 Manipulate array elements.
4.1.13 Solve a given problem by writing program, run, test
and debug using array.
Two-Dimensional Arrays
 Arearrays with more than one dimension.
 Two-dimensional arrays represent data in terms of
rows and columns. It has two subscripts.
Representation – 2D Array
Example:
int marks_table[ ][ ] = { {83,99,74},
{88,90,72},
{89,88,82},
{98,93,75},
{78,60,65} };
cout<<marks_table[1][2];
}
Two- Dimensional Array Definition
 <Elementdata type> <Array Name>
[ <number of Rows>] [ <number of Columns] ;

Example : char Nom [ 3 ] [ 4 ] ;


Nom[0] [0] Nom[0] [1] Num[0] [2] Num[0] [3]

Num[1] [0] Num[1] [1] Num[1] [2] Num[1] [3]

Num[2] [0] Num[2] [1] Num[2] [2] Num[2] [3]


Entering Data into Array
 A nestedfor loop can be used to enter data in
a 2D array
For example:
for(int x=0;x<2;x++)
{
for(int y=0;y<3;y++)
{
cout<< "Enter mark_table ["<<x<<"]["<<y<<"] :";
cin>>marks_table[x][y];
}
}
Reading Data from an Array
 Example to read data from an array

for(int x=0;x<2;x++)
{
for(int y=0;y<3;y++)
{
cout<<"marks_table["<<x<<"][ "<<y<<"] :";
cout<< marks_table[x][y]<<endl;
}
}
Example:
int main()
{

int marks_table[2][3];

for(int x=0;x<2;x++){
for(int y=0;y<3;y++)
{cout<< "Enter mark_table ["<<x<<"]["<<y<<"] :";
cin>>marks_table[x][y]; }}

for(int x=0;x<2;x++){
for(int y=0;y<3;y++)
{cout<<"marks_table["<<x<<"][ "<<y<<"] :";
cout<< marks_table[x][y]<<endl;}}

return 0;
system("PAUSE");
}

You might also like