KEMBAR78
Algorithm and Data Structure - Array and Struct | PDF
Algorithm and
Data Structure
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021
February 22, 2021
2
2 Array and Struct
3
What is Array?
Array also known as “larik”. Array is data collections
with each data element using same name and type
Each component/element of array can be accessed
and distinguished through index
4
Type of Array 1) One-Dimensional Array
2) Multi-Dimensional Array
5
One-Dimensional Array
A collection of identical elements arranged in a row. These
elements have the same data type, but content of those elements
may be different
6
Example
2 4 8 16 32 64 128 256 512 1024
0 1 2 3 4 5 6 7 8 9
Index
Data
dataType ArrayName[ElementNumber]={<Initialization>};
ArrayName [index];
Declaration
Access
7
Multi-Dimensional Array
Multi-dimensional array is data collections consisting of rows
and columns of elements of the same type
8
Example
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
(3,0) (3,1) (3,2)
(4,0) (4,1) (4,2)
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
(3,0) (3,1) (3,2) (3,3)
(0,0) (0,1) (0,2) (0,3) (0,4) (0,5)
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5)
2-D Array 5x3
2-D Array 4x4
2-D Array 5x6
9
Example (Cont.)
dataType ArrayName [Row][Column] =
{
{<RowInitialization1>},{< RowInitialization2>},{<RowInitializationN>}
{<RowInitialization1>},{< RowInitialization2>},{<RowInitializationN>}
};
ArrayName [Row][Column];
Declaration
Access
10
Record / Struct
Record is data collections that is not similar or
varied. In C, record also known as struct
The difference between record and array is that
record can be filled with data that have different data
type
11
Example
struct nama_type_record {tipe_data1 nm_var1; tipe_data2 nm_var2;
tipe_dataN nm_varN;} var_record;
General form of record
12
Example (Cont.)
struct mahasiswa { char nama[20]; char npm[10], int nilai;
} nilaiMhs;
nilaiMhs.nama;
nilaiMhs.npm;
nilaiMhs.nilai;
Declaration
Access
13
Record in Array
Record in array make it possible to store record data in an array
index.
That is, using this technique can store some data that has different
data types in an array
For example, some students can have a name and value.
14
Example
struct nama_type_record {tipe_data1 nm_var1; tipe_data2 nm_var2;
tipe_dataN nm_varN;} var_record[ArrayElement];
General form of record in array
15
Example (Cont.)
struct mahasiswa { char nama[20]; int nilai;
} nilaiMhs[3];
nilaiMhs[0].nama;
nilaiMhs[0].nilai;
Declaration
Access
16
Array in Record
Arrays in a record make it possible to store array data in a record.
That is, using this technique can store multiple indexes of array
data into a record that has a different data type.
For example, a student can have a name and several grades.
17
Example
struct nama_type_record {tipe_data1 nm_var1[ArrayElement];
tipe_data2 nm_var2[ArrayElement];
tipe_dataN nm_varN;[ArrayElement]}
var_record;
General form of array in record
18
Example (Cont.)
struct mahasiswa { char nama[20][3]; int nilai[3];
} nilaiMhs;
nilaiMhs.nama[0];
nilaiMhs.nilai[0];
Declaration
Access
19
Array in Array Record
Arrays in a array record make it possible to store array data in a
array record.
That is, using this technique can store multiple indexes of array
data into multiple record that has a different data type.
For example, each of student can have a name and several grades.
20
Example
General form of array in record array
struct nama_type_record {tipe_data1 nm_var1[ArrayElement];
tipe_data2 nm_var2[ArrayElement];
tipe_dataN nm_varN;[ArrayElement]}
var_record[ArrayElement];
21
Example (Cont.)
struct mahasiswa { char nama[20][3]; int nilai[3];
} nilaiMhs[3];
nilaiMhs[1].nama[0];
nilaiMhs[1].nilai[0];
Declaration
Access
Thank You, Next …
Modular Programming
February 22, 2021
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021

Algorithm and Data Structure - Array and Struct

  • 1.
    Algorithm and Data Structure AndiNurkholis, S.Kom, M.Kom Study Program of Informatics Faculty of Engineering and Computer Science SY. 2020-2021 February 22, 2021
  • 2.
  • 3.
    3 What is Array? Arrayalso known as “larik”. Array is data collections with each data element using same name and type Each component/element of array can be accessed and distinguished through index
  • 4.
    4 Type of Array1) One-Dimensional Array 2) Multi-Dimensional Array
  • 5.
    5 One-Dimensional Array A collectionof identical elements arranged in a row. These elements have the same data type, but content of those elements may be different
  • 6.
    6 Example 2 4 816 32 64 128 256 512 1024 0 1 2 3 4 5 6 7 8 9 Index Data dataType ArrayName[ElementNumber]={<Initialization>}; ArrayName [index]; Declaration Access
  • 7.
    7 Multi-Dimensional Array Multi-dimensional arrayis data collections consisting of rows and columns of elements of the same type
  • 8.
    8 Example (0,0) (0,1) (0,2) (1,0)(1,1) (1,2) (2,0) (2,1) (2,2) (3,0) (3,1) (3,2) (4,0) (4,1) (4,2) (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) (0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (4,0) (4,1) (4,2) (4,3) (4,4) (4,5) 2-D Array 5x3 2-D Array 4x4 2-D Array 5x6
  • 9.
    9 Example (Cont.) dataType ArrayName[Row][Column] = { {<RowInitialization1>},{< RowInitialization2>},{<RowInitializationN>} {<RowInitialization1>},{< RowInitialization2>},{<RowInitializationN>} }; ArrayName [Row][Column]; Declaration Access
  • 10.
    10 Record / Struct Recordis data collections that is not similar or varied. In C, record also known as struct The difference between record and array is that record can be filled with data that have different data type
  • 11.
    11 Example struct nama_type_record {tipe_data1nm_var1; tipe_data2 nm_var2; tipe_dataN nm_varN;} var_record; General form of record
  • 12.
    12 Example (Cont.) struct mahasiswa{ char nama[20]; char npm[10], int nilai; } nilaiMhs; nilaiMhs.nama; nilaiMhs.npm; nilaiMhs.nilai; Declaration Access
  • 13.
    13 Record in Array Recordin array make it possible to store record data in an array index. That is, using this technique can store some data that has different data types in an array For example, some students can have a name and value.
  • 14.
    14 Example struct nama_type_record {tipe_data1nm_var1; tipe_data2 nm_var2; tipe_dataN nm_varN;} var_record[ArrayElement]; General form of record in array
  • 15.
    15 Example (Cont.) struct mahasiswa{ char nama[20]; int nilai; } nilaiMhs[3]; nilaiMhs[0].nama; nilaiMhs[0].nilai; Declaration Access
  • 16.
    16 Array in Record Arraysin a record make it possible to store array data in a record. That is, using this technique can store multiple indexes of array data into a record that has a different data type. For example, a student can have a name and several grades.
  • 17.
    17 Example struct nama_type_record {tipe_data1nm_var1[ArrayElement]; tipe_data2 nm_var2[ArrayElement]; tipe_dataN nm_varN;[ArrayElement]} var_record; General form of array in record
  • 18.
    18 Example (Cont.) struct mahasiswa{ char nama[20][3]; int nilai[3]; } nilaiMhs; nilaiMhs.nama[0]; nilaiMhs.nilai[0]; Declaration Access
  • 19.
    19 Array in ArrayRecord Arrays in a array record make it possible to store array data in a array record. That is, using this technique can store multiple indexes of array data into multiple record that has a different data type. For example, each of student can have a name and several grades.
  • 20.
    20 Example General form ofarray in record array struct nama_type_record {tipe_data1 nm_var1[ArrayElement]; tipe_data2 nm_var2[ArrayElement]; tipe_dataN nm_varN;[ArrayElement]} var_record[ArrayElement];
  • 21.
    21 Example (Cont.) struct mahasiswa{ char nama[20][3]; int nilai[3]; } nilaiMhs[3]; nilaiMhs[1].nama[0]; nilaiMhs[1].nilai[0]; Declaration Access
  • 22.
    Thank You, Next… Modular Programming February 22, 2021 Andi Nurkholis, S.Kom, M.Kom Study Program of Informatics Faculty of Engineering and Computer Science SY. 2020-2021