KEMBAR78
Array Data Structure for programing language | PDF
Array Data Structure
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).
The above image can be looked as a top-level view of a staircase where you are at the
base of the staircase. Each element can be uniquely identified by their index in the array
(in a similar way as you could identify your friends by the step on which they were on in
the above example).
Arrays in C/C++
Last Updated: 28-04-2020
An array in C or C++ is a collection of items stored at contiguous memory locations and
elements can be accessed randomly using indices of an array. They are used to store
similar type of elements as in the data type must be the same for all elements. They can
be used to store collection of primitive data types such as int, float, double, char, etc of
any particular type. To add to it, an array in C or C++ can store derived data types such
as the structures, pointers etc. Given below is the picturesque representation of an
array.
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but
if we want to store a large number of instances, it becomes difficult to manage them
with normal variables. The idea of an array is to represent many instances in one
variable.
Array declaration in C/C++:
There are various ways in which we can declare an array. It can be done by specifying
its type and size, by initializing it or both.
1. Array declaration by specifying size
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
2. Array declaration by initializing elements
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
3. Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
0.
// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
Advantages of an Array in C/C++:
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of
code.
Disadvantages of an Array in C/C++:
1. Allows a fixed number of elements to be entered which is decided at the
time of declaration. Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are
needed to be managed in accordance with the new memory allocation.
Facts about Array in C/C++:
● Accessing Array Elements:
Array elements are accessed by using an integer index. Array index starts
with 0 and goes till size of array minus 1.
2D array
In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays. For example,
float​ x[​3​][​4​];
Here, ​x​ is a two-dimensional (2d) array. The array can hold 12 elements. You can think
the array as a table with 3 rows and each row has 4 columns.
Similarly, you can declare a three-dimensional (3d) array. For example,
float​ y[​2​][​4​][​3​];
Here, the array ​y​ can hold 24 elements.
Initializing a multidimensional array
Here is how you can initialize two-dimensional and three-dimensional arrays:
Initialization of a 2d array
// Different ways to initialize two-dimensional array
int​ c[​2​][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
int​ c[][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
int​ c[​2​][​3​] = {​1​, ​3​, ​0​, ​-1​, ​5​, ​9​};
Address calculation
1D array
Loc(a[i]) = BA + (i-lb)*c
Location of an array ‘a’ for the ith index where:
BA- base address
Lb- lower bound
C- integer size
2D array
RMO: Loc(a[i][j]) = BA + [(i-lb1)*nc + (j-lb2)]c
CMO: ​Loc(a[i][j]) = BA + [(i-lb1)+(j-lb2)nr]c
Where location of element in the position of i(row) and j(column) for an array ‘a’
Where:
BA- base address
Lb1- lower bound in row
Lb2- lower bound in column
Nc- number of column
Nr- number of row
Finding number of any element: last-first +1
Array Data Structure for programing language
Array Data Structure for programing language

Array Data Structure for programing language

  • 1.
    Array Data Structure Anarray is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The above image can be looked as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by their index in the array (in a similar way as you could identify your friends by the step on which they were on in the above example). Arrays in C/C++ Last Updated: 28-04-2020 An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C or C++ can store derived data types such as the structures, pointers etc. Given below is the picturesque representation of an array.
  • 2.
    Why do weneed arrays? We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable. Array declaration in C/C++: There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both.
  • 3.
    1. Array declarationby specifying size // Array declaration by specifying size int arr1[10]; // With recent C/C++ versions, we can also // declare an array of user specified size int n = 10; int arr2[n]; 2. Array declaration by initializing elements // Array declaration by initializing elements int arr[] = { 10, 20, 30, 40 } // Compiler creates an array of size 4. // above is same as "int arr[4] = {10, 20, 30, 40}" 3. Array declaration by specifying size and initializing elements // Array declaration by specifying size and initializing // elements int arr[6] = { 10, 20, 30, 40 } // Compiler creates an array of size 6, initializes first // 4 elements as specified by user and rest two elements as 0. // above is same as "int arr[] = {10, 20, 30, 40, 0, 0}" Advantages of an Array in C/C++: 1. Random access of elements using array index. 2. Use of less line of code as it creates a single array of multiple elements. 3. Easy access to all the elements. 4. Traversal through the array becomes easy using a single loop.
  • 4.
    5. Sorting becomeseasy as it can be accomplished by writing less line of code. Disadvantages of an Array in C/C++: 1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic. 2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Facts about Array in C/C++: ● Accessing Array Elements: Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.
  • 5.
    2D array In Cprogramming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float​ x[​3​][​4​]; Here, ​x​ is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as a table with 3 rows and each row has 4 columns. Similarly, you can declare a three-dimensional (3d) array. For example, float​ y[​2​][​4​][​3​]; Here, the array ​y​ can hold 24 elements. Initializing a multidimensional array Here is how you can initialize two-dimensional and three-dimensional arrays: Initialization of a 2d array // Different ways to initialize two-dimensional array int​ c[​2​][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}}; int​ c[][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
  • 6.
    int​ c[​2​][​3​] ={​1​, ​3​, ​0​, ​-1​, ​5​, ​9​}; Address calculation 1D array Loc(a[i]) = BA + (i-lb)*c Location of an array ‘a’ for the ith index where: BA- base address Lb- lower bound C- integer size
  • 9.
    2D array RMO: Loc(a[i][j])= BA + [(i-lb1)*nc + (j-lb2)]c CMO: ​Loc(a[i][j]) = BA + [(i-lb1)+(j-lb2)nr]c Where location of element in the position of i(row) and j(column) for an array ‘a’ Where: BA- base address Lb1- lower bound in row
  • 10.
    Lb2- lower boundin column Nc- number of column Nr- number of row Finding number of any element: last-first +1