Two-dimensional arrays: Difference between revisions

From Computer Science Wiki
No edit summary
No edit summary
Line 1: Line 1:
[[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]]
[[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]]


Like a 1D array, a 2D array is a collection of data cells, all of the same type, which can be given a single name. However, a 2D array is organized as a matrix with a number of rows and columns.<ref>http://www-ee.eng.hawaii.edu/~tep/EE160/Notes/Array/2darray.html</ref>
== Characteristics of a 2d array ==


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:


Daniel Shiffman<ref> https://processing.org/tutorials/2darray/</ref> writes: an array keeps track of multiple pieces of information in linear order, a one-dimensional list. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi-dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional list of everything you eat:
# 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.
# 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.
# 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).
# 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.
# 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.
# 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.
# 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.


(lettuce, tomatoes, steak, mashed potatoes, cake, ice cream)
== a video ==
 
Or you could have a two-dimensional list of three courses, each containing two things you eat:
 
(lettuce, tomatoes) and (steak, mashed potatoes) and (cake, ice cream)
 
== a good video ==
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
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



Revision as of 09:28, 9 May 2023

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.

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]