Two-dimensional arrays

From Computer Science Wiki
Programming basics[1]

Characteristics of a 2d array[edit]

A two-dimensional (2D) array, also known as a matrix, is a data structure that organizes and stores elements in rows and columns. It can be visualized as a table, grid, or a rectangular arrangement of items. Here are some key characteristics of a 2D array:

  1. Structure: A 2D array is essentially an array of arrays, where each element of the outer array is another array representing a row in the matrix.
  2. Dimensions: A 2D array has two dimensions - rows and columns. The dimensions define the size of the array and are often denoted as (m x n), where 'm' represents the number of rows and 'n' represents the number of columns.
  3. Indexing: Elements in a 2D array can be accessed using a pair of indices, typically (i, j), where 'i' is the row index and 'j' is the column index. Indexing usually starts from 0, so the top-left element is at index (0, 0) and the bottom-right element is at index (m-1, n-1).
  4. Homogeneous Data Type: A 2D array generally stores elements of the same data type (e.g., integers, floating-point numbers, or strings). However, some languages or libraries may support heterogeneous arrays that can store different data types within the same array.
  5. Memory Allocation: In some programming languages, 2D arrays can be created with a fixed size, and the memory is allocated in a contiguous block. In other languages or implementations, they can be dynamically sized or created as nested lists or arrays, which can lead to non-contiguous memory allocation.
  6. Operations: Various operations can be performed on 2D arrays, including addition, subtraction, and multiplication (in the case of matrices), as well as traversal, searching, sorting, and manipulation of individual elements. Some languages and libraries offer built-in functions for working with 2D arrays, while others require manual implementation of these operations.
  7. Use Cases: 2D arrays are commonly used in various applications and domains, including computer graphics, image processing, scientific simulations, game boards, spreadsheets, and adjacency matrices in graph theory.

constructing a 2d array[edit]

Here are a few examples of algorithms that use two-dimensional arrays:

Summing the elements in a 2D array:'

function sumArray(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      sum += arr[i][j];
    }
  }
  return sum;
}


Finding the largest element in a 2D array:

function findLargestElement(arr) {
  let largest = arr[0][0];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > largest) {
        largest = arr[i][j];
      }
    }
  }
  return largest;
}


Transposing a 2D array (swapping rows and columns):

function transposeArray(arr) {
  let transposedArr = [];
  for (let i = 0; i < arr[0].length; i++) {
    transposedArr[i] = [];
    for (let j = 0; j < arr.length; j++) {
      transposedArr[i][j] = arr[j][i];
    }
  }
  return transposedArr;
}


Multiplying two 2D arrays:

function multiplyArrays(arr1, arr2) {
  let product = [];
  for (let i = 0; i < arr1.length; i++) {
    product[i] = [];
    for (let j = 0; j < arr2[0].length; j++) {
      let sum = 0;
      for (let k = 0; k < arr2.length; k++) {
        sum += arr1[i][k] * arr2[k][j];
      }
      product[i][j] = sum;
    }
  }
  return product;
}

Note that these are just examples, and there are many other algorithms that could use two-dimensional arrays. The specific implementation may vary depending on the problem being solved.

A video[edit]

This video has some references to Java, but it should help you understand the very basics of 2d arrays : https://www.youtube.com/watch?v=nySZfYSVspo

Standards[edit]

  • Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.


References[edit]