Definition
- An array is a collection of similar type of data elements stored in adjacent memory locations and are referred to by a single array-name.
- Array is a data structure storing a group of elements, all of which are of the same data type.
- Array is a powerful data storage method that lets us to group a number of same-type data items under the same group name.
Feature/Characteristic
- In the case of C, we have to declare and define array before it can be used i.e. Like other variables, arrays must be declared at the beginning of a function.
- Individual items, or elements, in an array are identified using a subscript after the array name.
- Generally array’s allocates the memory for all its elements in one block.
- Declaration and definition of an array tell whole thing to the compiler i.e. the name of the array, the type of each element, and the size or number of elements.
- All the elements of an array share the same name, and they are distinguished from one another with the help of an index present in a subscript.
- Random access to every array element is possible using a numeric index (subscript) with array name.
- Array is the most common data structure used to store collections of elements.
- It is a simple data structure, used for decades, and is extremely useful.
- The amount of storage for a declared array has to be specified at compile time before execution. This is the reason that an array has a fixed size/static data structure.
- The same data type of an array applies uniformly to all the array elements. for this reason, an array is called a homogeneous data structure.
- Arrays are among the oldest and most important data structures.
- They are also used to implement many other data structures, such as lists and strings.
- Arrays can hold primitives as well as references both.
- The array is the most efficient data structure for storing and accessing a sequence of objects.
- The elements of the array share the same variable name but each element has its own unique index number (also known as a subscript).
- The unique element no.(always started from zero) in an array, play a major role for calling each element individually.
- Any particular element of an array can be modified without disturbing another element.
Syntax
- The declaration of an array is just like any simple or normal variable declaration with additional size part, indicating the number of elements of the array.
- Arrays must be declared before they can be used. Optionally, array elements can be initialized when the array is declared.
- The declaration specifies the base type of the array, its name, and its size or dimension.
- We can define an array as:-
Syntax : DataType ArrayName [ArraySize];
Example : int x [50];
-
- where ‘int’ is the data type and ‘x’ is the name of array/defined as an array of constant-size 50 to store fifty integer data-type values.
- Here, each value of the array is called an array-element and an integer value in big bracket is called the subscript is used to denote individual elements of the array.
- Index value of an element in an array is the reference number given to each element at the time of memory allocation.The index value in an array is also called as subscript or indices.
- Similarly,
char data [30];
float arr[500];
-
- The subscript used for the individual element of an array is represented by integer quantity such as x[3], x[2], x[0] etc.
- The use of a symbolic constant makes it easier to modify a program that uses an array. for example –
#define SIZE 50
-
- We can access elements of an array by unique indices.
Advantage
- They are simple data structure.
- They store large no. of data items at one place.
- They are faster and can be utilized anywhere than other data structure.
- They are used by almost every program.
- All the elements of an array share the same name and they are distinguished from one another with the element no.
- Arrays are convenient to declare and provide the easy syntax to access any element by its index number.
- Once the array is set up, access to any element is convenient and fast.
Disadvantage
- Since array is a fixed size/static data structure, therefore, the size of memory can not be increased or reduced after compilation.We cannot alter the size of the array once array is declared.Most often this size is specified at compile time.
- Memory allocated during Compile time i.e. Once memory is allocated at compile time it cannot be changed during Run-time.
- All the array elements of an array is allocated continuously/contiguously/adjacently in the memory, not in scattered form.
- They store data of similar data types together i.e. we can not store different data type data items in an array.
- The amount of storage required for holding element of array depend on it’s size or type.
- Inserting new elements at the front is potentially expensive because existing elements need to be shifted over to make room.
- Deleting an element from an array is not possible.
Use/Application of Arrays
- Arrays are used to implement mathematical vectors and matrices, as well as other kinds of rectangular tables.
- Arrays are also used to implement other data structures, such as lists/linked lists, heaps, hash tables, deques, queues and stacks.
- Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.
- Computer programming tasks that involve repetitive data processing lend themselves to array storage.
- Arrays are used to store list of values.
- Arrays are used to perform Matrix operations.
- Arrays are used to implement search algorithms.
- Arrays are used to implement sorting algorithms such as Bubble sort, Merge sort etc.
- Arrays are also used to implement CPU Scheduling Algorithms.
- One dimensional array is used in Stack & Queue data structure implementation.
Types of Array
There are two types of array in C.They are –
- One/Single Dimensional Array
- Multi Dimensional Array
- Two Dimensional Array
- Three Dimensional Array
- four Dimensional Array
- …..
- n – Dimensional Array
1. One/Single Dimensional Array –
-
- Single dimensional array is also called as one dimensional arrays/Linear Array/1- D Array.
- Single dimensional arrays are used to store a large no. of similar type of data items in a single row having multiple columns.
- In single dimensional array, data is stored in linear form.
- An array having only one subscript variable is called One-Dimensional array.
- We can define one dimensional array as:-
Syntax : DataType ArrayName [ArraySize];
Example : int y [50];
where ‘int’ is the data type and ‘y’ is the name of array/defined as an array of constant-size 50 to store fifty integer data-type values. - One Dimensional Array Initialization :
- It is possible to initialize an array at the time of declaration.
- The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed within the braces and separated by commas.
- Syntax & Example of array initialization is as follows: –
Syntax : data type array-name [ size ] = {val 1, val 2, …….val n};
Example : int n[10] = {10, 42, 73, 45, 50, 60, 07,82, 54,100};
int n[ ] = {10, – 42, 73, 45, 50, 60, 07,- 82, 54,100};
float temp[05] = { 41.2, 25.7, 40.8, 34.0, 20.6};
-
-
- To access the elements of single dimensional array, we use array name followed by index value of that element by enclosing it in square braces.
- The index value of single dimensional array always starts with zero (0) for first element and incremented by one for each next element.
-
2. Multi Dimensional Array –
-
- An array having more than one subscript variable is called Multi-dimensional array.
- Multi Dimensional Array is also called as Matrix.
- Multi dimensional array is also called as array of arrays.
- Simply, an array is created with more than one dimension (size) is called as multi dimensional array.
- Multi dimensional array can be of two dimensional array or three dimensional array or four dimensional array and so on.
- Two Dimensional Array –
- It is the most popular and commonly used multi dimensional array .
- It is also called the 2-D array that are used to store data in the form of table.
- We can also use 2-D array to create mathematical matrices.
- To access elements in two-dimensional array, we use array name followed by row index value and then column index value of the element that to be accessed. Here the row and column index values must be enclosed in separate square braces.
- We can define two dimensional array as:-
Syntax : DataType ArrayName [RowSize] [ColumnSize];
Example : int y [2] [3];
where ‘int’ is the data type and ‘y’ is the name of two dimensional array having row size two and column size three to store six integer data-type values. - Two Dimensional Array Initialization : –
- It is possible to initialize two dimensional array at the time of declaration.
- The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed within the braces and separated by commas.
- Syntax of array initialization is as follows: –
Syntax : DataType ArrayName [Rowsize][Columnsize] = {{r1c1value, r1c2value, …},{r2c1value, r2c2 value,…},{r3c1value, r3c2 value,…}…} ; - Example of array initialization is as follows: –
int y [2] [3] = { {10, 20, 30}, {40, 50, 60} };
int y [2] [3] = {
{10, 20, 30},
{40, 50, 60}
};
-
-
-
- To assign individual value in a two dimensional array, we hava –
-
-
int y [0 ] [2 ] = 15;
0 Comments