ARRAYS

Arrays

  • An array is a data structure that stores a group of elements of the same data type (homogeneous).

  • Elements are stored in contiguous memory which is allocated statically.

  • Characteristics of arrays include:

    • Name: Identifier for the array.

    • Dimensionality: Can be 1D, 2D, 3D, etc.

    • Size: Number of elements it can hold.

    • Element Data Type: Type of data that the array holds.

Types of Arrays

1D Arrays

  • A 1D array is a linear list of elements.

  • Example: char str[5]; defines a 1D array of characters with 5 elements.

2D Arrays

  • A 2D array is a table-like structure with rows and columns.

  • Example: int matrix[3][4]; defines a 2D array with 3 rows and 4 columns.

3D Arrays

  • A 3D array has depth, forming a cube of elements.

  • Example: int cube[3][3][3]; defines a 3D array.

Declaration of Arrays in C

  • 1D Array:

<data type> <name> [ <size1> ]; // Example: int A[5];
  • 2D Array:

<data type> <name> [ <size1> ][<size2> ]; // Example: int B[3][4];
  • 3D Array:

<data type> <name> [ <size1> ][<size2>][<size3> ]; // Example: int C[2][3][4];

Visual Examples of Arrays

1D Array

  • Representation of a 1D array can be visualized as a list of elements, e.g., ['A', 'B', 'C'].

2D Array

  • Represented as a grid or table with specified rows and columns, e.g., [[1, 2], [3, 4]] represents a 2x2 array.

3D Array

  • Visualized akin to a cube containing an array of arrays, e.g., [[[1,2],[3,4]], [[5,6],[7,8]]] for a 2x2x2 array.

Accessing Array Elements

  • Array elements can be accessed using an index starting from 0.

  • Example for accessing an element in a 1D array:

int value = A[0]; // Access first element
  • Accessing elements in a 2D array:

int value = matrix[1][2]; // Access element at second row, third column

Algorithms with Arrays

Common Operations

  • Minimum: Finding the smallest value in the array.

  • Maximum: Finding the largest value.

  • Sum: Calculating the total of all elements.

  • Average: Total divided by the number of elements.

  • Count: Counting elements that meet specified conditions (e.g., positives).

  • Sort: Arranging elements in a specified order.

  • Search: Finding if a specified element exists.

Exercises

  • For practical learning, problems related to arrays are provided (e.g., finding the minimum/maximum value, sorting).

  • Example: Write a function that finds the minimum in a given 1D array.

Advanced Topics

Pointer and Array Equivalence

  • Usage of pointers with arrays allows for efficient memory management and manipulation.

  • A[i] is equivalent to *(A + i).

Passing Arrays as Parameters

  • Arrays can be passed to functions without needing to explicitly pass their size.

  • The name of the array acts as a pointer to the first element.

Higher Dimension Arrays

  • Arrays can extend beyond 2D to 3D, 4D, etc. Such arrays follow similar principles but become increasingly complex.


  • Further learning includes sorting algorithms and efficiency for algorithms using arrays, which will be expanded upon in higher-level computer science courses.