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

Array in DSA Final

Uploaded by

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

Array in DSA Final

Uploaded by

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

Array in DSA

Group members :-
1. Rup Kumar (230101120246)
2. Achhutha Reddy (230101120245)
3. Pawan Kalyan (230101120243)
4. Pavani (230101120241)

Guided by :-
Prof. Pragnya
Agenda
 I N T R O D U C T I O N O F A R R AY S

 T Y P E S O F A R R AY S

 ACCESSING ELEMENTS IN
A R R AY S
 INSERTING ELEMENTS

 DELETING ELEMENTS

 A D VA N TA G E S A N D L I M I TAT I O N S
O F A R R AY S
 R E A L - W O R L D A P P L I C AT I O N S O F
A R R AY S
 CONCLUSION
Introduction :
• Array are defined as collection of similar type of data items stored at
contiguous memory locations.

• Array is the simplest data structure where each data element can be randomly
accessed by using its index number.

• int numbers[5] = {1, 2, 3, 4, 5}


Types of Arrays Example of 1D Array :-
1.One-Dimensional Arrays
#include <stdio.h>
• A 1D array is a list of items stored in a single row,
like a row of boxes. int main() {
• Each item in the array has a specific position (called int scores[5] = {85, 90, 78, 92, 88}; // Declares and
an index), which helps us quickly locate or change initializes a 1D array
that item.
• All items in a 1D array must be of the same data type // Accessing and printing elements
(e.g., all integers, all characters, etc.). for (int i = 0; i < 5; i++) {
printf("Score of student %d is %d\n", i + 1, scores[i]);
}
Why Use a 1D Array? return 0;
• Organized Storage: You can store multiple related }
items, like student scores, in one place.
• Fast Access: By using the index, you can quickly get
any item from the array.
• Memory Efficient: It stores data in consecutive
memory locations, making it easy to access and
manage.
2. Two-Dimensional Arrays Example of 2D Array :-
#include <stdio.h>
 A 2D array is a collection of items arranged in rows
and columns. int main() {
 Think of it as a grid, where each cell in the grid has a
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} }; // Declares and
position based on its row and column. initializes a 2D array
 Each item in a 2D array must be of the same data type
(e.g., all integers or all characters). // Accessing and printing elements
for (int i= 0; i < 2; i++) { // Loop through rows
for (int j = 0; j < 3; j++) { // Loop through columns
Why Use a 2D Array? printf("%d ", matrix[i][j]); // Print each element in the
 Organized Data: You can organize data in a structured row
way, making it easier to work with two-dimensional }
layouts. printf("\n"); // Move to the next row
 Easy Access: You can quickly access any item by }
specifying its row and column. return 0;
 Memory Efficient: Like 1D arrays, 2D arrays also }
store data in consecutive memory, allowing fast
access.
Examples :-
Inserting Elements
#include <stdio.h>
• Inserting at the End: Adding an element to the last
int main() {
position. int arr[6] = {10, 20, 30, 40, 50};
int n = 5;
• Inserting at the Beginning or Middle: Adding an
int pos = 2;
element at a specific index, which requires shifting int value = 25;
other elements. for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
Insertion :- arr[pos] = value;
void insert(int arr[], int pos, int value, int size) { n++;
for(int i 1] = arr[i]; for (int i = 0; i < n; i++) {
} = size-1; i >= pos; i--) { printf("%d ", arr[i]);
arr[i+ }
arr[pos] = value; // Output: 10 20 25 30 40 50
}
return 0;
}
Examples :-
Deleting Elements
#include <stdio.h>
• Identify the Position: Find the index of the element you
int main() {
want to delete. int arr[5] = {10, 20, 30, 40, 50};
• Shift Elements: Move each element one position to the
int n = 5;
int pos = 2;
left, starting from the position of the deleted element. for (int i = pos; i < n - 1; i++) {
• Update the Size: Decrease the size of the array by 1. arr[i] = arr[i + 1];
}
n--;
Deletion :- for (int i = 0; i < n; i++) {
void delete(int arr[], int pos, int size) { printf("%d ", arr[i]);
for(int i = pos; i < size-1; i++) { }
arr[i] = arr[i+1]; // Output: 10 20 40 50
}
} return 0;
}
Examples :-
Array Traversal
#include <stdio.h>
• Start at the First Element: Begin at index 0.
int main() {
• Move One by One: Go to the next index until you reach int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
the end of the array.
printf("%d ", arr[i]);
• Perform an Action: For example, print each element or }
// Output: 10 20 30 40 50
add each element to a total.
return 0;
Traverse :- }
void traverse(int arr[], int size) {
printf("Array elements: ");
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
Advantages and Limitations of Arrays
Advantages of Arrays

• Easy Access: Direct access to elements using an index (O(1) time).

• Efficient Memory Use: Elements are stored in contiguous memory


locations.

• Easy Iteration: Easy to loop through all elements.

Limitations of Arrays

• Fixed Size: Cannot change the size once declared.

• Homogeneous Data: Can only store elements of the same data type.

• Memory Waste: Unused spaces in the array lead to wasted memory.


Real-World Applications of Arrays

 Data Storage: Arrays store data in programs, like student scores or


employee details.
 Image Processing: Images are represented as 2D arrays of pixels.

 Game Development: Game boards (like tic-tac-toe) are implemented


using 2D arrays.
 Matrices and Mathematical Computations: Arrays store matrices for
scientific and engineering calculations.
 Database Management: Arrays manage data in tables and organize
records.
Conclusion :-
we explored the fundamentals and practical uses of arrays in Data
Structures and Algorithms. Arrays provide a simple yet powerful way to
store and access data efficiently, making them essential in programming.
With their fixed size and contiguous memory layout, arrays enable fast
data access and are widely used in real-world applications, from image
processing to game development.
Thank you

You might also like