Link copied to clipboard. Share away!

Dissmis

Published on 06 Mar 2020

Assume we need to write a program to find the average height of all the students in a class. We have to declare 65 different variables to store the height values of all these students which is cumbersome. Even to write the expression for finding the average, we have to list down all these variables (al +a2+ . . +a65)/65. Furthermore, if the same program has to be extended for finding the average height of all students in the university, the declaration and manipulation of 1000's of variables become unmanageable.

Here comes the concept of arrays wherein the same array name can be used for representing several different values, just by differentiating the index.

Example: if 'height' is the name of the array, height[0] can store the height value of the first student in the class, height[ 1 ] can store the height value of the second student in the class, height[2] can store the height value of the third student in the class and so on. Here we need not declare 65 distinct variables. Just declare the height array and the number of elements we want to store in it.

- An array is a group of related data items which are referred by a common name, all being same type and size.
- Each value in an array is called an
*array element.* - All the elements are of the same type but may contain different values
- The array, elements are placed in contiguous memory locations.
- Each array element is given a position number known as array index or subscript.
- An array index begins from 0 and the last index number is n-1.
- Since C has no built in function to check array bounds, trying to access an element outside this range is not an error. It only returns a garbage value.

- One-dimensional array
- Two-dimensional array
- Multi-dimensional array

**1) One-Dimensional Arrays:**

A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one-dimensional array. The array subscript or index begins from 0.

**Syntax:** data_type array_name[Number_of Elements] ;

**Example: **int a{20];

means an array with the name 'a' has to be created in which 20 integer values can be stored. When this statement is executed, 80 bytes of memory, will be allocated and that entire block of memory is assigned the name 'a'. All the 20 elements will be contiguous in the memory.

**Array initialization: **

Similar to variable initialization, array elements can also be initialized with values, at the time of declaration itself.

int a[5]={1,2,3,4,5};

float sample[3]= {3.0,9,12.5};

char c array[5]={'a', 'e' , 'i' , 'o', 'u'};

int a[]={1,2,3,4}; //array initialization without specifying the size

int number[5]={1,2}; //in this case, 0th element is 1, 1^{st} element is 2^{nd} and remaining are all 0.

**Accessing array elements:** Each array element can be referred to by specifying the array name followed by its index number within square brackets. In C, array __indexing__ always starts from zero. This means that the 1^{st} element is denoted by a[0].

- 2
^{nd}element=a[1] - 3
^{rd}element=a[2] - n
_{th}element=a[n-1]

**2) Two-dimensional arrays: **This array can be defined as an array of several one-dimensional arrays. It can be visualized in the form of a table with rows and columns. It is an array with two indexes. One index represents the row number and the other the column number.

**Syntax:** data_type array_nainefrow_sizellcolunin_sizel ;

**Example:** int matrix[2][2];

**Array initialization: **You can also initialize an array when you declare it (just like with variables).

int matrix[4][3]= { 34, 56, 67 }, { 12, 33, 23 }, { 14, 80, 85 }, { 13, 78, 90 }

int matrix[4][3] = { 34, 56, 67, 12, 33, 23, 14, 80, 85 , 13, 78, 90 } ;

int matrix[3][2] = {0}; // all elements are initialized to 0

int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ; //wrong (__while initializing a 2-D array it is necessary to mention the second (column) dimension, whereas the first dimension (row) is optional) __

int arr[ ][ ] = ( 12, 34, 23, 45, 56, 45 ) ; // wrong

**Accessing Array elements: **

Each array element can be referred to by specifying its name followed by its row index number and column index number within square brackets. Matrix[0] [0] can be used to access the 0th row, 0th column element of the array.

- matrix[2][1] represents an element in 2
^{nd}row and^{1st}column.

**3) Multi-dimensional arrays: **

** **A 2-D array can be defined as an array of several 1-D arrays. Likewise, a 3-D array can be defined as an array of several 2-D arrays. In general, an n-dimensional array can be defined as an array of several (n-1) dimensional arrays.

Example: sample[4][2][2] is a 3-D array with 4 two-D arrays. Each 2-D array has 2 rows and 2 columns.

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×