[go: up one dir, main page]

0% found this document useful (0 votes)
29 views7 pages

Group1 Arrays

Uploaded by

anyangwagilpeter
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)
29 views7 pages

Group1 Arrays

Uploaded by

anyangwagilpeter
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/ 7

Topic:Arrays in Data Structures

❖ Presented by: [Group 1]

❖ ---

Slide 2: What is an Array?


- Definition: An array is a collection of elements, all of which are of the same
data type, stored in contiguous memory locations.
- Arrays are one of the simplest and most widely used data structures.
- Key Properties:
- Fixed size.
- Elements are indexed starting from 0.

❖ Slide 3: Array Representation


- Array Declaration in C:
❖ c
❖ int arr[5]; // Declares an array of 5 integers

- Indexing: Arrays are zero-indexed, meaning the first element is at index 0, the
second at index 1, and so on.

❖ Example:
❖ c
❖ int arr[5] = {10, 20, 30, 40, 50};

- arr[0] = 10
- arr[1] = 20

❖ Types of Arrays
- 1D Array (One-Dimensional Array): A simple list of elements.
- Example:
❖ c
❖ int arr[3] = {1, 2, 3};

- 2D Array (Two-Dimensional Array): A matrix-like structure that holds rows


and columns of elements.
- Example:
❖ c
❖ int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

- Multidimensional Arrays: Arrays with more than two dimensions.

Array Operations
❖ Insertion:
- Adding an element at a specific index.
❖ c
❖ arr[2] = 25; // Inserts 25 at index 2

❖ Access:
- Accessing elements using index.
❖ c
❖ printf("%d", arr[2]); // Outputs 25
❖ Traversal:
- Visiting each element of the array sequentially.
❖ c
❖ for (int i = 0; i < 5; i++) {
❖ printf("%d ", arr[i]);
❖ }

❖ Update:
- Modifyingan existing element.
❖ c
❖ arr[3] = 45; // Updates the value at index 3

❖ Deletion
- Removing an element at a specific index (not supported directly, can shift
elements)

❖ Real-Life Examples of Arrays


❖ List of Students' Grades:
❖ int grades[5] = {85, 90, 78, 88, 92};

❖ Monthly Sales Data:


❖ float sales[12] = {1000.5, 1200.75, 900.25, ...};
❖ Searching: Finding a specific element within the array.
❖ Sorting: Arrang elements in a specific order (e.g., ascending, descending
❖ Game Board (2D Array): A 2D array can represent a tic-tac-toe board.
❖ char board[3][3] = {{'X', 'O', 'X'}, {'O', 'X', 'O'}, {' ', 'O', 'X'}};

Advantages of Arrays

❖ Efficient Access: Random access to any element using its index in constant time O(1).

❖ Simple to Implement: Easy to declare and use in most programming languages.


❖ Contiguous Memory: Elements are stored together, making it efficient to iterate
through.

Disadvantages of Arrays
❖ Fixed Size: Once declared, the size of an array cannot be changed dynamically.

❖ Costly Insertion/Deletion: Inserting or deleting elements (especially in the middle)


requires shifting elements, which can be slow.
❖ Wasted Memory: If the array is declared with a size larger than required, unused
memory remains allocated.

Array vs Other Data Structures

Arrays vs Linked Lists:

Arrays:

- Constant time access O(1).


- Insertion and deletion take O(n).

Linked Lists:

- Dynamic size, elements can be added or removed easily.


- Access time is linear O(n).

Applications of Arrays
Storing lists of numbers, characters, or other data types.

Implementing matrices for mathematical operations.


Creating lookup tables.

epresenting graphs.

Note: While arrays are a fundamental data structure, they may not be the most suitable choice for all
scenarios. Other data structures like linked lists, stacks, queues, and trees may be more appropriate
depending on the specific requirements.

Practical Examples

❖ Example 1: Accessing Elements

❖ #include <stdio.h>

❖ int main() {
❖ int arr[3] = {10, 20, 30};
❖ printf("Element at index 1: %d\n", arr[1]); // Outputs 20
❖ return 0;
❖ }

❖ Example 2: Array Traversal


❖ c
❖ #include <stdio.h>

❖ int main() {
❖ int arr[5] = {1, 2, 3, 4, 5};
❖ for (int i = 0; i < 5; i++) {
❖ printf("%d ", arr[i]);
❖ }
❖ return 0;
❖ }
❖ Example 3: Two-Dimensional Array
❖ c
❖ #include <stdio.h>

❖ int main() {
❖ int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
❖ for (int i = 0; i < 2; i++) {
❖ for (int j = 0; j < 3; j++) {
❖ printf("%d ", matrix[i][j]);
❖ }
❖ printf("\n");
❖ }
❖ return 0;
❖ }

❖ Slide 11: Exercises


❖ Create an Array of 10 Integers
- Write a C program that takes 10 integer inputs from the user and stores them
in an array.

❖ Sum of Array Elements


- Write a program to find the sum of all elements in a given array.

❖ 2D Array Matrix Multiplication:


- Create a program to multiply two 2D arrays (matrices).

❖ Conclusion
- Arrays are a foundational data structure that provides efficient random access
to elements.
- While they have limitations, such as fixed size, they are easy to use and crucial
for managing collections of elements in a program.
- Understanding arrays is essential before exploring more complex data
structures like linked lists, stacks, and queues.

You might also like