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

Data Structure Array

The document provides a comprehensive overview of arrays, including their definition, characteristics, and various operations such as initialization, modification, insertion, deletion, and searching. It emphasizes the importance of arrays as a linear data structure for storing homogeneous elements and allows for efficient data management. Additionally, the document includes practical examples in C and C++ to illustrate the concepts discussed.

Uploaded by

yudistiraradit95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Array

The document provides a comprehensive overview of arrays, including their definition, characteristics, and various operations such as initialization, modification, insertion, deletion, and searching. It emphasizes the importance of arrays as a linear data structure for storing homogeneous elements and allows for efficient data management. Additionally, the document includes practical examples in C and C++ to illustrate the concepts discussed.

Uploaded by

yudistiraradit95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Arrays

August 28, 2023

Dhimas Arief Dharmawan, PhD


dhimas.arief@upnyk.ac.id
Department of Informatics
UPN "Veteran" Yogyakarta
Indonesia
Table of Contents
Arrays

D. A. Dharmawan

Introduction to Arrays

Introduction to Arrays Operations of Arrays


Array Initialization and
Accessing Elements
Modifying Array Elements

Operations of Arrays Array Insertion and


Deletion

Array Initialization and Accessing Elements Searching in Arrays

Questions and
Modifying Array Elements Discussion

Array Insertion and Deletion Conclusion

Searching in Arrays

Questions and Discussion

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

address the scenario. Questions and


Discussion
▶ Present the scenario provided to you, along with your Conclusion

approach, in front of your classmates.

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

interpretation of the concept of an array. Deletion


Searching in Arrays

▶ Engage in group discussions to share and refine your Questions and


Discussion
individual definitions of an array. Conclusion
▶ Collaboratively compile a comprehensive definition of an array
and complete your answer with several characteristics of an
array.

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

elements of the same data type. Searching in Arrays

Questions and
▶ Each element is uniquely identified by its position or index Discussion

within the array. Conclusion

▶ An array provides a structured way to manage related data


items.

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

▶ Array Initialization Array Insertion and


Deletion
Searching in Arrays
▶ Accessing Array Elements
Questions and
▶ Modifying Array Elements Discussion

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

2 Modifying Array Elements


Array Insertion and
3 int main () { Deletion
4 // Declare and initialize an integer array Searching in Arrays

5 int numbers [5] = {1 , 2 , 3 , 4 , 5}; Questions and


Discussion
6
7 // Print the first element of the array Conclusion

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

4 // Declare and initialize an integer array 12 Modifying Array Elements


Array Insertion and
5 int numbers [5] = {1 , 2 , 3 , 4 , 5}; Deletion
6 Searching in Arrays

7 // Modify the third element Questions and


Discussion
8 numbers [2] = 10;
9 Conclusion

10 // Print the modified array


11 for ( int i = 0; i < 5; ++ i ) {
12 printf ( " % d " , numbers [ i ]) ; // Output : 1 2 10 4
5
13 }
14
15 return 0;
16 }

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

4 // Declare and initialize an integer array 13 Modifying Array Elements


Array Insertion and
5 int numbers [5] = {1 , 2 , 3 , 4 , 5}; Deletion
6 Searching in Arrays

7 // Modify the fourth element Questions and


Discussion
8 numbers [3] = 7;
9 Conclusion

10 // Display the modified array


11 for ( int i = 0; i < 5; ++ i ) {
12 std :: cout << numbers [ i ] << " " ; // Output : 1 2
3 7 5
13 }
14
15 return 0;
16 }

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

int, float). 14 Array Insertion and


Deletion

▶ 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

and insert it at the desired position.


▶ Deletion: Remove an element by shifting elements and
adjusting the array size.

Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
Insertion Example in C
Arrays

1 # include < stdio .h > D. A. Dharmawan

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

9 for ( int i = 0; i < size ; i ++) { Questions and


10 if ( array [ i ] == target ) { Discussion

11 printf ( " Element found at index % d \ n " , i ) ; Conclusion

// Output : Element found at index 2


12 found = 1;
13 break ;
14 }
15 }
16
17 if (! found ) {
18 printf ( " Element not found \ n " ) ;
19 }
20
Department of Informatics
21 return 0; UPN "Veteran" Yogyakarta
21 Indonesia
22 }
Linear Search Example in C++
Arrays
1 # include < iostream > 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 bool found = false ; Deletion
8 19 Searching in Arrays

9 for ( int i = 0; i < size ; i ++) { Questions and


10 if ( array [ i ] == target ) { Discussion

11 std :: cout << " Element found at index " << Conclusion

i << std :: endl ; // Output : Element found at index


2
12 found = true ;
13 break ;
14 }
15 }
16
17 if (! found ) {
18 std :: cout << " Element not found " << std :: endl ;
19 }
Department of Informatics
20 UPN "Veteran" Yogyakarta
21 Indonesia
21 return 0;
Questions and Discussion
Arrays

D. A. Dharmawan

Introduction to Arrays

Operations of Arrays
Array Initialization and
Accessing Elements
Modifying Array Elements
Array Insertion and
Deletion
Searching in Arrays

▶ Who has questions or would like to discuss? 20 Questions and


Discussion

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

▶ Modifying Array Elements


▶ Insertion and Deletion
▶ Searching in Arrays

Department of Informatics
UPN "Veteran" Yogyakarta
21 Indonesia
THANK YOU

You might also like