Array

What is Array and its Types? Discuss Row and Column major order

Introduction

Array is a linear data structure in which the elements are stored in a proper sequence or linear relationship between the elements. All the elements are stored at the contiguous memory locations.(i.e one after the other)

Definition of Array

An Array is a collection of data elements of the same type, stored in contiguous memory locations, and accessed by their index or position. Arrays are commonly used for storing and manipulating large amounts of data efficiently.

Arrays are widely used in programming for efficient storage and manipulation of data. They allow us to store and process large amounts of data in a single variable, making our code more organized and easier to read. Arrays are commonly used in many programming languages, including C, C++, Java, Python, and JavaScript.

Array Declaration

Before using array in a program, it must be declared first like other variables.
To declare an array, we must provide the following information.

  • Data type of an array
  • The name of the array (rules are same as variable name)
  • The number of subscripts in an array
  • The maximum value of each subscript

So the general from or syntax of array declaration is :

Storage_sp type name [sub1] [sub2] ….;

Where storage_sp is storage class specifier which is optional
type is the data type (int, char or float)
name is the name of the array
sub1, sub2, …. are number of subscripts available in an array, which are used to specify the dimension of an array

Types of Arrays

Linear Array:- A linear array is a type of array data structure where the elements are stored in a contiguous block of memory and accessed using a single index. In other words, a linear array is a one-dimensional array. In a linear array, the elements are arranged in a single row, and each element is accessed using its position or index in the array.

Non Linear Array:- A non-linear array is a type of data structure where the elements are not stored in contiguous memory locations, and their positions cannot be accessed using a single index. Non-linear arrays are also known as multidimensional arrays or nested arrays.

Non Linear Array can categorized as:-

Two-dimensional array: This is a non-linear array with two dimensions, typically represented as a matrix. Elements in a two-dimensional array are accessed using two indices, one for the row and one for the column.

General syntax is

data type array name [row size] [Col. Size]

Example

int a [5] [10]

Three-dimensional array: This is a non-linear array with three dimensions, often used for representing 3D graphics and scientific data. Elements in a three-dimensional array are accessed using three indices, one for each dimension.

General syntax is

data type array name [space size] [row size] [Col. Size]

Example

Suppose there are 4 students and get different marks in three subjects in two house test then their marks are displayed by using 3-dimensional array as

St [1] [1] [3] = 26 St [1] [2] [3] = 36 students
St [1] [1] [1] = 20, St [1] St [1] [2] [1] = 26 St [1]

: :

St [4] [2] [1] = 36 St [4] [2] [2] = 29 St [4] [2] [1] = 16

N-Dimensional Array:- N-dimensional array is a data structure that can hold multiple values or elements, organized in a grid-like structure with n dimensions. In an n-dimensional array, each element is identified by a set of n indices, where each index corresponds to one of the dimensions of the array.

General syntax is

data type array name [S1] [S2]

Here Sn : nth size of array.

Discuss Row and Column major order with example.

In row-major order and column-major order refer to the two different ways of storing multi-dimensional arrays in memory.

In row-major order, the elements of a multi-dimensional array are stored in memory row-by-row. That is, all the elements in the first row are stored first, followed by all the elements in the second row, and so on. In a two-dimensional array, this means that the elements in each row are stored contiguously in memory.

Here is an example of a two-dimensional array in row-major order, in C language:

int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

In this example, the elements of the array are stored in memory as follows:

[1][2][3][4][5][6][7][8][9][10][11][12]

In column-major order, the elements of a multi-dimensional array are stored in memory column-by-column. That is, all the elements in the first column are stored first, followed by all the elements in the second column, and so on. In a two-dimensional array, this means that the elements in each column are stored contiguously in memory.

Here is an example of a two-dimensional array in column-major order, in C language:

int arr[3][4] = {
{1, 5, 9},
{2, 6, 10},
{3, 7, 11},
{4, 8, 12}
};

In this example, the elements of the array are stored in memory as follows:

[1][2][3][4][5][6][7][8][9][10][11][12]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *