# Multi-D Arrays

Multi-D Arrays is the important topic of the Computer Programming & technology. Moreover, Freestudy9 has all kind of important topic and information about the subject.

An array is a fixed-size sequenced collection of elements of the same data type.

**Single Dimensional Array **

An array using only one subscript to represent the list of elements called single dimensional array.

Syntax : data_type array_name[size];

##### Example: int marks[5];

An individual array element can be used anywhere like a normal variable with a statement such as g = marks [60];

More generally if *i* is declared to be an integer variable, then the statement g=marks[i]; will take the value contained at i^{th} position in an array and assigns it to g.

We can store the value into array element by specifying the array element on the left-hand side of the equals sign like marks[60]=95; Also, The value 95 stored at the 60^{th} position in an array.

Also, The ability to represent a collection of related data items by a single array enables us to develop concise and efficient programs.

For example, we can very easily sequence through the elements in the array by varying the value of the variable that used as a subscript into the array.

for(i=0; i<66; i++); sum = sum + marks[i];

Above for loop will sequence through the first 66 elements of the marks array (elements 0 to 65) and will add the values of each mark into sum. Moreover, When for loop finished, the variable sum will then contain the total of first 66 values of the marks.

The declaration int values[5]; would reserve enough space for an array called values that could hold up to 5 integers. Also, Refer to the below-given picture to conceptualize the reserved storage space.

**Initialization of Single Dimensional array**

The general form of initialization of array is:

- data_type array_name[size]={list of values};

###### There are three ways to initialize single dimensional array,

- int number[3]={1, 5, 2};
- will initialize the 0
^{th}element of an array of 1, 1^{st}element to 5 and 2^{nd}element to 2.

- will initialize the 0

- int number[5] = {1, 7};
- will initialize the 0
^{th}element of an array of 1, 1^{st}element to 7 and rest all elements will initialized to 0.

- will initialize the 0

- int number[ ] ={1, 5, 6};
- first of all array size will fix to 3 then it will initialize 0
^{th}element to 1, 1^{st}element to 5 and 2^{nd}element to 6

- first of all array size will fix to 3 then it will initialize 0

**Two-dimensional Arrays**

- Two-dimensional arrays also called table or matrix.
- Also, Two-dimensional arrays have two subscripts.
- The first subscript denotes the number of rows and the second subscript denotes the number of columns.

Syntax: data_type array_name[row_size][column_size];

Example: int marks[10][20];

- Here m declared as a matrix having 10 rows (numbered from 0 to 9) and 20 columns (numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column m[9][19]

**Initialization of two dimensional array**

- int table[2][3] = {1,2,3,4,5,6};
- will initialize 1
^{st}row 1^{st}column element to 1, 1^{st}row 2^{nd}column to 2, 1^{st}row 3^{rd}column to 3, 2^{nd}row 3^{rd}column to 6 and so on.

- will initialize 1

- int table[2][3] = {{1,2,3},{4,5,6});
- here, 1
^{st}group is for 1^{st}row and 2^{nd}group is for 2^{nd}row. So 1^{st}row 1^{st}column element is 1, 2^{nd}row 1^{st}column element is 4, 2^{nd}row 3^{rd}column element is 6 so on.

- here, 1

- int table[2][3] ={{1,2},{4}}
- initializes as above but missing elements will initialized by 0.

**Related Terms**

Computer Programming and Utilization, Break Continue Goto, Array with Example, Engineering Study.

## Leave a Reply