Data Structure Array
Data Structure Array
D. A. Dharmawan
Introduction to Arrays
Questions and
Modifying Array Elements Discussion
Searching in Arrays
Conclusion
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Why do We Need Arrays?
Arrays
D. A. Dharmawan
2 Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
Array Insertion and
Deletion
▶ Review the provided case study and describe your approach to Searching in Arrays
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Understanding Arrays
Arrays
D. A. Dharmawan
3 Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
▶ In light of the given case study, state your personal Modifying Array Elements
Array Insertion and
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Definition of Arrays
Arrays
D. A. Dharmawan
4 Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
▶ An array is a linear data structure that stores a collection of Array Insertion and
Deletion
Questions and
▶ Each element is uniquely identified by its position or index Discussion
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Characteristics of Arrays
Arrays
D. A. Dharmawan
5 Introduction to Arrays
Operations of Arrays
▶ Fixed Size: Arrays have a fixed size defined at the time of Array Initialization and
Accessing Elements
declaration. Modifying Array Elements
Array Insertion and
▶ Homogeneous Elements: All elements in an array are of the Deletion
Searching in Arrays
same data type. Questions and
Discussion
▶ Contiguous Memory Allocation: Array elements are stored in
Conclusion
adjacent memory locations.
▶ Random Access: Elements can be accessed directly using
their index.
▶ Efficiency: Arrays allow efficient element access and
modification.
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Operations of Arrays
Arrays
D. A. Dharmawan
Introduction to Arrays
6 Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
Array Insertion and
▶ Given your choice to employ arrays for addressing the Deletion
Searching in Arrays
presented scenarios, could you outline the comprehensive Questions and
steps involved in utilizing arrays effectively? Discussion
Conclusion
▶ What strategies can be employed to manipulate the data
stored within an array while addressing the scenarios at hand?
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Operations of Arrays
Arrays
D. A. Dharmawan
Introduction to Arrays
7 Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
Conclusion
▶ Insertion and Deletion
▶ Searching in Arrays
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Array Initialization and Accessing Elements
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Procedure for Array Initialization: 8 Array Initialization and
Accessing Elements
▶ Choose Data Type: Decide on the data type of the Modifying Array Elements
Array Insertion and
elements you want to store in the array (e.g., int, float, char). Deletion
Searching in Arrays
▶ Declare the Array: Use the chosen data type and provide a Questions and
name for the array. Optionally, specify the array size in square Discussion
Conclusion
brackets if it’s known in advance.
▶ Initialize the Array: Assign values to the array elements if
needed.
▶ Accessing Array Elements: Use the array name followed by
an index within square brackets to access specific elements.
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Example in C
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
9 Array Initialization and
Accessing Elements
1 # include < stdio .h >
Modifying Array Elements
2 Array Insertion and
Deletion
3 int main () {
Searching in Arrays
4 // Declare and initialize an integer array
Questions and
5 int numbers [5] = {1 , 2 , 3 , 4 , 5}; Discussion
6
Conclusion
7 // Print the first element of the array
8 printf ( " First element : % d \ n " , numbers [0]) ;
9
10 return 0;
11 }
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Example in C++
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
10 Array Initialization and
1 # include < iostream > Accessing Elements
8 std :: cout << " First element : " << numbers << std ::
endl ;
9
10 return 0;
11 }
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Modifying Array Elements
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Procedure for Modifying Array Elements: Array Initialization and
Accessing Elements
▶ Choose Data Type: Decide on the data type of the 11 Modifying Array Elements
Array Insertion and
elements you want to store in the array (e.g., int, float, char). Deletion
Searching in Arrays
▶ Declare the Array: Use the chosen data type and provide a Questions and
name for the array. Optionally, specify the array size in square Discussion
Conclusion
brackets if it’s known in advance.
▶ Initialize the Array: Assign values to the array elements if
needed.
▶ Modify Array Elements: Access the desired element using
its index and assign a new value to it.
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Example in C
Arrays
D. A. Dharmawan
Introduction to Arrays
1 # include < stdio .h >
2 Operations of Arrays
Array Initialization and
3 int main () { Accessing Elements
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Example in C++
Arrays
D. A. Dharmawan
Introduction to Arrays
1 # include < iostream >
2 Operations of Arrays
Array Initialization and
3 int main () { Accessing Elements
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Array Insertion and Deletion
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Array Initialization and
▶ Choose Data Type: Decide the data type for the array (e.g., Accessing Elements
Modifying Array Elements
▶ Declare and Initialize Array: Declare the array with a specified Searching in Arrays
Questions and
size and initialize its elements. Discussion
▶ Insertion: Shift elements to make space for the new value, Conclusion
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Insertion Example in C
Arrays
2 Introduction to Arrays
3 int main () { Operations of Arrays
4 int array [5] = {10 , 20 , 30 , 40 , 50}; Array Initialization and
Accessing Elements
5 int size = 5;
Modifying Array Elements
6 int index = 2; 15 Array Insertion and
Deletion
7 int newValue = 25;
Searching in Arrays
8
Questions and
9 for ( int i = size - 1; i >= index ; i - -) { Discussion
10 array [ i + 1] = array [ i ];
Conclusion
11 }
12 array [ index ] = newValue ;
13 size ++;
14
15 for ( int i = 0; i < size ; i ++) {
16 printf ( " % d " , array [ i ]) ; // Output : 10 20 25
30 40 50
17 }
18
19 return 0;
20 } Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Deletion Example in C++
Arrays
D. A. Dharmawan
1 # include < iostream >
Introduction to Arrays
2
Operations of Arrays
3 int main () { Array Initialization and
4 int array [6] = {10 , 20 , 25 , 30 , 40 , 50}; Accessing Elements
Modifying Array Elements
5 int size = 6;
16 Array Insertion and
6 int index = 2; Deletion
Searching in Arrays
7
8 for ( int i = index ; i < size - 1; i ++) { Questions and
Discussion
9 array [ i ] = array [ i + 1];
Conclusion
10 }
11 size - -;
12
13 for ( int i = 0; i < size ; i ++) {
14 std :: cout << array [ i ] << " " ; // Output : 10 20
30 40 50
15 }
16
17 return 0;
18 }
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Searching in Arrays
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
▶ Choose Data Type: Decide the data type for the array (e.g., Modifying Array Elements
Array Insertion and
int, float). Deletion
17 Searching in Arrays
▶ Declare and Initialize Array: Declare the array with values. Questions and
Discussion
▶ Linear Search: Iterate through the array to find the desired Conclusion
value.
▶ Binary Search (if sorted): Divide and conquer method to find
the value efficiently.
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Linear Search Example in C
Arrays
1 # include < stdio .h > D. A. Dharmawan
2
Introduction to Arrays
3 int main () {
4 int array [5] = {10 , 20 , 30 , 40 , 50}; Operations of Arrays
Array Initialization and
5 int size = 5; Accessing Elements
6 int target = 30; Modifying Array Elements
Array Insertion and
7 int found = 0; Deletion
8 18 Searching in Arrays
11 std :: cout << " Element found at index " << Conclusion
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
Array Insertion and
Deletion
Searching in Arrays
Conclusion
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Conclusion
Arrays
D. A. Dharmawan
Introduction to Arrays
Operations of Arrays
Array Initialization and
Accessing Elements
Today, we have covered the following topics: Modifying Array Elements
Array Insertion and
▶ Definition and characteristics of arrays. Deletion
Searching in Arrays
▶ Operation of arrays, including: Questions and
Discussion
▶ Array Initialization
Conclusion
▶ Accessing Array Elements
21
Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
THANK YOU