DFC20113 - Programming Fundamentals: Understand The Use of Array
DFC20113 - Programming Fundamentals: Understand The Use of Array
DFC20113 - Programming Fundamentals: Understand The Use of Array
PROGRAMMING
FUNDAMENTALS
Understand the use of array
1
Course Learning Outcomes:
■ CLO1: Implement programming element and
articulate how they are used to achieve a working
program.
2
Learning Outcomes:
By the end of the class, student should be able to:
■ Define an Array
■ Describe the components of an array: index, elements, size
■ Explain the types of arrays
■ Declare, initialize and illustrate one dimensional array
■ Access individual element of one dimensional array
■ Declare, initialize and illustrate two dimensional array
■ Access individual element of two dimensional array
3
Situation
A lecturer are marking a quiz of 25 students for her
class. Help her to display the marks that she entered,
count the total and average marks of the class.
4
Solution without array
#include <iostream> cout<<"Enter quiz of student 6: ";
6
Solution using array
#include <iostream>
using namespace std;
int main()
{
float quiz[25]; //declaration of array
float sum=0, average=0; //initialize sum and average to 0
cout<<"This program is using array to calculate average of student's quiz."<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
for(int i=0; i<25; i++)
{
cout<<"Enter quiz of student " <<i+1<<" : ";
cin>>quiz[i];
sum+=quiz[i];
}
average=sum/10;
cout<<"Average of quiz is :"<<average<<endl;
return 0;
}
7
Output using array
8
Array Definition
■ Array is a collection of data elements of the same
type that are referenced by a common name.
9
Array Definition
■ Array’s element consist of memory allocation and
identified by index.
■ Array size : 9
0 1 2 3 4 5 6 7 8
10
Types of Arrays
■ Two types of arrays
One-dimensional array
Two-dimensional array
11
One Dimensional Arrays
■ Definition: A one dimensional array, also referred as 1D array, will have a
single row and can have any number of columns.
■ One dimensional arrays will have only one subscript. Subscript refers to
the dimension of the array.
■ When you declare a one dimensional array as x[7], it means that the array
has one row and seven columns.
12
Representation of a ONE
Dimensional Array
X[7] Row
1 2 3 4 5 6 7
Column
13
Example of declaring one
dimensional array
Syntax:
<Data type> <Variable_name>[Size of the array];
Example: int X[3];
int X [ 3 ] ;
15
Initializing One
Dimensional Array
– Eg: int nombor[3] = {3, 24, 31};
first index 0 1 2
nombor 3 24 31
16
Accessing Element of One
Dimensional Array
Element is accessed by its index
Array index refers to the location of the values in an array.
The first element will always have the array index as 0.
Syntax :
<Variable_name>[Array index];
Example:
Individual Element or using loop
marks[0]; for(i = 0; i < 3; i+
marks[1]; +)
marks[2]; marks[i];
17
Array element and index
X[0] X[1] X[2]
18
Accessing Element of One
Dimensional Array
Eg:
int my_array[5] = {11, 22, 33, 44, 55};
to store the value 75 in the third element of my_array, we could write the
following statement:
my_array[2] = 75;
to pass the value in 4th element of my_array and store the value into
temporary variable, temp_value:
int temp_value = my_array[3]; // also equals to 44
cout<<my_array[3];
cout<<temp_value;
19
Accessing Element of One
Dimensional Array
if the name of an array is name, then name[0] is the name of the element
that is in position 0, name[1] is the name of the element that is in position 1,
etc.
in general, the nth element is in position n-1. So if the array has n elements,
their names are name[0], name[1], name[2], …, name[n-1].
it is important to be able to clearly distinguish between the two uses that
brackets [ ] have related to arrays:
int name[5]; // declaration of a new array
name[2] = 75; /* access to an element of the array
and store number 75. */
20
Example
■ Program Student_Marks.cpp will illustrate how to
declare an array, initialize and access its elements.
21
// program Student_Marks.cpp
#include<iostream>
using namespace std;
void main()
{
int marks[ ]={95,85,75,80,65};
cout<<"marks[0] : "<<marks[0];
Output:
marks[0] : 95
cout<<"\nmarks[1] : "<<marks[1];
marks[1] : 85
cout<<"\nmarks[2] : "<<marks[2]; marks[2] : 75
cout<<"\nmarks[3] : "<<marks[3]; marks[3] : 80
cout<<"\nmarks[4] : "<<marks[4]; marks[4] : 65
}
22
EXAMPLE
■ Program Onedim_Int_Array.cpp illustrates how
to initialize integer array and display its contents
using for loop.
23
// program Onedim_Int_Array.cpp
#include<iostream>
using namespace std;
void main()
{
int y[4]={8,7,6,4};
for(int i=0;i<4;i++)
{
cout<<y[i]<<"\n";
}
}
24
Example
■ Program Onedim_Char_Array_Name.cpp
illustrates how to initialize a character array and
display its contents.
25
//program Onedim_Char_Array_Name.cpp
#include<iostream>
using namespace std;
void main()
{
char stud_name[ ]={‘M',‘A',‘F','I','A'};
for(int i=0;i<=4;i++)
{
cout<<stud_name[i];
}
}
26
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.
int marks[5];
for(int i=0;i<5;i++)
{
cout<<“Enter the marks: ";
cin>>marks[i];
}
27
Printing Data from an Array
■ You can use a for loop with a single cout
statement to print the values from an array.
28
Example
■ Program One_Int_Array.cpp illustrates how to accept
five marks from the user and prints the values on the
screen.
29
// Program One_Int_Array.cpp
#include <iostream>
using namespace std;
void main()
{
int marks[5];
//Accepting the marks
for(int i=0;i<5;i++){
cout<<"Enter mark :";
cin>>marks[i];
}
cout<<"\nThe marks you have enter is"<<endl;
//Displaying the array
for(int i=0;i<5;i++){
cout<<"Marks:"<<marks[i]<<endl;
}
}
30
■ Output :
31
In Class Exercise
1. Declare an array alpha of 15 elements of type int.
2. Access the value of tenth element of array alpha.
3. Set the value of fifth element of array alpha to 35.
4. Set the value of ninth element of array alpha to the
sum of fifth and sixth element of array alpha.
32
1. Declare an array alpha of 15 elements of type int.
int alpha [15];
33
■ What is the output?
#include<iostream>
using namespace std;
void main()
{
double num [ ]= {2.0, 4.0, 6.5, 8.7};
cout<<num[1+2];
cout<<num[1]+num[2];
}
34
Edit the following code, to fill in Output:
value into array //program output
1
#include <iostream> 2
using namespace std;
3
void main()
4
{
for(int i = 0; i < 10; i++) 5
{ 6
cout << i+1 << “\n”; 7
} 8
} 9
10
35
Answer
#include <iostream>
using namespace std;
void main()
{
int num[ ]={1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
{
cout << num[i]<< "\n";
}
}
36
EXERCISE:
1. Declare an array my_student of 10 element of type integer
2. Access the value of tenth element of array staff.
3. Variable name is matrix consist a 5 column with data type
of double.
4. Declare an array my_size of 5 elements of type float.
37
Exercise:
■ Determine what is the output for the segment
code below:
int a[4]={4,2,0,3};
38
Summary
■ An array is a structured data type with a fixed number of elements.
■ Every element of an array is of the same type and can be accessed
by their index.
■ Array index started with 0.
■ Array can be initialized during declaration.
■ A one-dimensional array has one subscript.
■ Element is accessed by its index.
39
REFERENCES
■ G. Marc. (2018) Professional C++ (4th Edition). New York,
United States. (ISBN: 9781119421306 )
■ S. Os . (2016) Programming: C ++ Programming:
Programming Language for Beginners: Learn in A Day!.
Morrisville, United States. ( ISBN: 9781329779013 )
■ A. Nikhil. (2017) Coding All-in-One For Dummies. New York,
United States. (ISBN: 9781119363026).
40
TWO-Dimensional Array
■ Two-dimensional arrays can be described as "arrays of
arrays".
■ For example, a two-dimensional array can be imagined as
a two-dimensional table made of elements of a same
uniform data type.
41
Presentation of Two-dimensional
array
42
■ Assume that there are 5 students in a class and each of them study
three different subjects, for example Mathematics, Physics and
Chemistry.
43
Example of declaring
Two-Dimensional array
Example
int marks_table [5][3];
Syntax
<Data type> <Variable name> [Row][Column];
44
Example of declaring
Two-Dimensional array
■ Table jimmy represents a bi-dimensional array of 3 by 5
elements of type int.
45
Initializing Two-Dimensional
Array
Eg:
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
Represent into table:
123
456
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
Represent into table :
123
450
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
Represent into table :
120
400
46
Accessing Element in Two-
Dimensional Array
■ Element is accessed by the index of its row and column.
■ Eg:
– To access the element in the 2nd row and at the 4th column of
this two-dimensional array, we can used the following code:
jimmy[1][3];
47
Example of Two Dimensional
Array
#include<iostream>
using namespace std;
void main()
{
int array2[ 2 ][ 3 ] = {{ 1, 2, 3} ,{4, 5,6 }};
48
Output
49
What is output?
#include<iostream>
using namespace std;
void main()
{
int marks_table[5][3] = { {83,99,74},
{88,90,72},{89,88,82},
{98,93,75},{78,60,65} };
cout<<marks_table[1][2];
}
50
Output
51
Example 1 of Two Dimensional
#include<iostream>
Array
#include<string>
void main()
//Second row
cout<<"Lastname\tFirstname\tLocation\n";
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout<<Data[i][j]<<"\t\t";
52
Example 2 of Two Dimensional
Array
#include <iostream>
using namespace std;
void main()
{
int array2[ 3 ][ 4 ];
//Accepting the marks
for (int row=0; row<2; row++) {
for(int col=0; col<3; col++){
cout<<"Enter mark ["<<(row)<<"][" <<col <<"]: ";
cin>>array2[row][col];
}
cout<<endl;
}
//display
for(int row=0; row<2; row++){
for(int col=0; col<3; col++)
cout<<array2[row][col] << " ";
cout<<endl;
}
}
53
Output of Example 2
54
In Class Exercise
Declare an array beta of 10 rows and 20 columns of
type int.
Examine the following:
double values[ ] [ ] = {
{1.2, 9.0, 3.2},
{9.2, 0.5, 1.5},
{7.3, 7.9, 4.8} } ;
What is the value of values[2][1]?
55
Which of the following statements constructs an array
with 5 rows of 7 columns?
long stuff[5][7];
long[5][7];
long stuff[7][5];
long [7][5];
56
Summary
■ An array is a structured data type with a fixed number of elements.
■ Every element of an array is of the same type and can be accessed by
their index.
■ Array index started with 0.
■ Array can be initialized during declaration.
■ In two-dimensional array, elements are arranged in table form.
■ To access element from two-dimensional array, pair of indices is needed
(index for row and index for column).
57
REFERENCES
■ G. Marc. (2018) Professional C++ (4th Edition). New York,
United States. (ISBN: 9781119421306 )
■ S. Os . (2016) Programming: C ++ Programming:
Programming Language for Beginners: Learn in A Day!.
Morrisville, United States. ( ISBN: 9781329779013 )
■ A. Nikhil. (2017) Coding All-in-One For Dummies. New York,
United States. (ISBN: 9781119363026).
58