0:00
/
0:00
Transcript

Day 002/365: Introduction to Data Structures - Understanding Arrays

Welcome to Day 02 of Project 365! Today, we’re exploring one of the most foundational concepts in programming: Arrays.

Welcome to Day 02 of Project 365! Today, we’re exploring one of the most foundational concepts in programming: Arrays. Understanding arrays is the first step toward mastering data structures, as they form the backbone of many algorithms and real-world applications.


What is an Array?

An array is a data structure that stores a collection of items at contiguous memory locations. These items are usually of the same type, and you can access them using their index.

Key Characteristics:

  • Fixed Size: Once declared, the size of an array cannot be changed.

  • Indexed Access: Elements are accessed using a zero-based index.

  • Homogeneous Elements: All elements must be of the same type.
    Example: In Python:

    arr = [10, 20, 30, 40, 50]
    print(arr[2]) # Output: 30

In C#:

int[] arr = {10, 20, 30, 40, 50};
Console.WriteLine(arr[2]);  // Output: 30

Types of Arrays

  1. Single-Dimensional Arrays:

    • A linear sequence of elements.

    • Example: [1, 2, 3, 4, 5]

  2. Multi-Dimensional Arrays:

    • Arrays containing arrays as elements, such as 2D or 3D arrays.

    • Example (2D array):

arr = [[1, 2], [3, 4]]
print(arr[1][0])  # Output: 3


int[,] arr = {{1, 2}, {3, 4}};
Console.WriteLine(arr[1, 0]);  // Output: 3
  1. Dynamic Arrays:

    • Resizable arrays such as Python lists or C#'s List<T>.


Basic Operations

1. Accessing Elements:

Retrieve elements using their index.

arr = [10, 20, 30, 40, 50]
print(arr[0])  # Output: 10


int[] arr = {10, 20, 30, 40, 50};
Console.WriteLine(arr[0]);  // Output: 10

2. Adding Elements:

In fixed-size arrays, elements cannot be added dynamically. In dynamic arrays, you can:

arr.append(60)  # Adds 60 to the end of the list


List<int> arr = new List<int> {10, 20, 30};
arr.Add(40);  // Adds 40 to the list

3. Removing Elements:

arr.pop()  # Removes the last element


arr.Remove(20);  // Removes the element 20

4. Iterating Through Arrays:

for num in arr:
    print(num)


foreach (int num in arr)
{
    Console.WriteLine(num);
}

Practical Applications of Arrays

  • Data Storage: Store lists of values, such as scores, prices, or student grades.

  • Algorithm Development: Arrays are integral to sorting and searching algorithms like binary search and quicksort.

  • Mathematical Operations: Representing matrices, vectors, and other mathematical entities.


Example Problems

Problem 1: Reverse an Array

Problem Statement:

Reverse the elements of an array.
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]


Solution in Python:

def reverse_array(arr):
    return arr[::-1]

print(reverse_array([1, 2, 3, 4, 5]))

Solution in C#:

int[] ReverseArray(int[] arr)
{
    Array.Reverse(arr);
    return arr;
}
int[] result = ReverseArray(new int[] {1, 2, 3, 4, 5});
Console.WriteLine(string.Join(", ", result));

Problem 2: Find the Maximum Element

Problem Statement:

Find the maximum element in an array.
Input: [10, 20, 30, 40, 50]
Output: 50
Solution in Python:

def find_max(arr):
    return max(arr)

print(find_max([10, 20, 30, 40, 50]))

Solution in C#:

int FindMax(int[] arr)
{
    return arr.Max();
}
Console.WriteLine(FindMax(new int[] {10, 20, 30, 40, 50}));

Actions

Today’s challenge: Practice working with arrays by solving these problems or creating your own. Share your solutions with our community!
Follow us for daily content. Let’s master coding together, one concept at a time!

Discussion about this video

User's avatar

Ready for more?