OPERATIONS ON ARRAYS
There are a number of operations that can be preformed
on arrays. These operations include:
 Traversing an array
 Inserting an element in an array
 Searching an element in an array
 Deleting an element from an array
 Merging two arrays
 Sorting an array in ascending or descending order
Traversing an Array
 Traversing an array means accessing each and every
element of the array for a specific purpose
 Traversing the data elements of an array A can include
printing every element.
 counting the total number of elements, or performing
any process on these elements. Since, array is a linear
data structure (because all its elements form a
sequence), traversing its elements is very simple
 and straightforward. The algorithm for array traversal
is given.
 Step 1: [INITIALIZATION] SET I = lower_bound
 Step 2: Repeat Steps 3 to 4 while I <= upper_bound
 Step 3: Apply Process to A[I]
 Step 4: SET I = I + 1
 [END OF LOOP]
 Step 5: EXIT
 Figure 3.12 Algorithm for array traversal
 In Step 1, we initialize the index to the lower bound of the array.
 In Step 2, a while loop is executed.
 Step 3 processes the individual array element as specified by the array name and
index value. Step 4 increments the index value so that the next array element could
be processed. The
 while loop in Step 2 is executed until all the elements in the array are processed, i.e.,
until I is
 less than or equal to the upper bound of the array.
Inserting an Element in an Array
 If an element has to be inserted at the end of
an existing array, then the task of insertion is
quite simple.
 For example, if an array is declared to contain 10
elements, but currently it has only 8 elements, then
obviously there is space to accommodate two more
elements.
 But if it already has 10 elements, then we will not be
able to add another element to it.
Deleting an Element from an Array
 Deleting an element from an array means removing a data
element from an already existing array.
 If the element has to be deleted from the end of the existing
array, then the task of deletion is quite simple. We just have to
subtract 1 from the upper_bound.
 For example, if we have an array that is declared as int
marks[60];
 The array is declared to store the marks of all the students in
the class. Now, suppose there are 54 students and the student
with roll number 54 leaves the course. The score of this student
was stored in marks[54]. We just have to decrement the
upper_bound. Subtracting 1 from the upper_bound will
indicate that there are 53 valid data in the array.
DELETION EXAMPLE
Two dimensional(2D) Array
 An array of arrays is known as 2D array. The two
dimensional (2D) array in C programming is also
known as matrix. A matrix can be represented as a
table of rows and columns.
Initialization of 2D Array
 There are two ways to initialize a two Dimensional
arrays during declaration.
 int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
OR
 int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
 Initialization of a 3d array
int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23,
2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
Declaration
 when we initialize a normal array (or you can say one dimensional array)
during declaration, we need not to specify the size of it.
 However that’s not the case with 2D array, you must always specify the
second dimension even if you are specifying elements during the
declaration.
 Let’s understand this with the help of few examples –
 /* Valid declaration*/
 int abc[2][2] = {1, 2, 3 ,4 }
 /* Valid declaration*/
 int abc[][2] = {1, 2, 3 ,4 }
 /* Invalid declaration – you must specify second dimension*/
 int abc[][] = {1, 2, 3 ,4 }
 /* Invalid because of the same reason mentioned above*/
 int abc[2][] = {1, 2, 3 ,4 }
How to store user input data into 2D array
 The array arr[n1][n2] can have n1*n2 elements.
 The array that we have in the example below is having the dimensions 5 and 4. These dimensions are
known as subscripts.
 So this array has first subscript value as 5 and second subscript value as 4.So the array abc[5][4] can have
5*4 = 20 elements.
 To store the elements entered by user we are using two for loops, one of them is a nested loop.
 The outer loop runs from 0 to the (first subscript -1) and the inner for loops runs from 0 to the (second
subscript -1).
 This way the the order in which user enters the elements would be abc[0][0], abc[0][1], abc[0][2]…so on.
Program for reading 5 rows and 4 columns
#include<stdio.h>
int main()
{
/* 2D array declaration*/
int abc[5][4];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<5; i++)
{
for(j=0;j<4;j++)
{
printf("Enter value for abc[%d][%d]:", i, j);
scanf("%d", &abc[i][j]);
}
} return 0; }
// C program to find the sum of two matrices of order 2*2
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrixn");
for (int row = 0; row < 2; ++row)
for (int col = 0; col < 2; ++col)
{
printf("Enter a%d%d: ", row + 1, col + 1);
scanf("%f", &a[row][col]);
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for (int row = 0; row < 2; ++row)
for (int col = 0; col < 2; ++col)
{
printf("Enter b%d%d: ", row + 1, col + 1);
scanf("%f", &b[row][col]);
}
// adding corresponding elements of two arrays
for (int row = 0; row < 2; ++row)
for (int col = 0; col < 2; ++col)
{
result[row][col] = a[row][col] + b[row][col];
}
// Displaying the sum
printf("nSum Of Matrix:");
for (int row = 0; row < 2; ++row)
for (int col = 0; col < 2; ++col)
{
printf("%.1ft", result[row][col]);
if (col == 1)
printf("n");
}
return 0;
}
Merging Two Arrays
 Merging two arrays in a third array means first copying the contents of
the first array into the third array and then copying the contents of the
second array into the third array. Hence, the merged array contains the
contents of the first array followed by the contents of the second array.
 If the arrays are unsorted, then merging the arrays is very simple, as one just
needs to copy the contents of one array into another. But merging is not a
trivial task when the two arrays are sorted and the merged array also needs
to be sorted.
LIMITATIONS IN ARRAY
 Arrays are generally used when we want to store large amount of
similar type of data. But they have the following limitations:
 Arrays are of fixed size.
 Data elements are stored in contiguous memory locations which may not
be always available.
 Insertion and deletion of elements can be problematic because of
shifting of elements from their positions.

Array 31.8.2020 updated

  • 2.
    OPERATIONS ON ARRAYS Thereare a number of operations that can be preformed on arrays. These operations include:  Traversing an array  Inserting an element in an array  Searching an element in an array  Deleting an element from an array  Merging two arrays  Sorting an array in ascending or descending order
  • 3.
    Traversing an Array Traversing an array means accessing each and every element of the array for a specific purpose  Traversing the data elements of an array A can include printing every element.  counting the total number of elements, or performing any process on these elements. Since, array is a linear data structure (because all its elements form a sequence), traversing its elements is very simple  and straightforward. The algorithm for array traversal is given.
  • 4.
     Step 1:[INITIALIZATION] SET I = lower_bound  Step 2: Repeat Steps 3 to 4 while I <= upper_bound  Step 3: Apply Process to A[I]  Step 4: SET I = I + 1  [END OF LOOP]  Step 5: EXIT  Figure 3.12 Algorithm for array traversal  In Step 1, we initialize the index to the lower bound of the array.  In Step 2, a while loop is executed.  Step 3 processes the individual array element as specified by the array name and index value. Step 4 increments the index value so that the next array element could be processed. The  while loop in Step 2 is executed until all the elements in the array are processed, i.e., until I is  less than or equal to the upper bound of the array.
  • 5.
    Inserting an Elementin an Array  If an element has to be inserted at the end of an existing array, then the task of insertion is quite simple.  For example, if an array is declared to contain 10 elements, but currently it has only 8 elements, then obviously there is space to accommodate two more elements.  But if it already has 10 elements, then we will not be able to add another element to it.
  • 7.
    Deleting an Elementfrom an Array  Deleting an element from an array means removing a data element from an already existing array.  If the element has to be deleted from the end of the existing array, then the task of deletion is quite simple. We just have to subtract 1 from the upper_bound.  For example, if we have an array that is declared as int marks[60];  The array is declared to store the marks of all the students in the class. Now, suppose there are 54 students and the student with roll number 54 leaves the course. The score of this student was stored in marks[54]. We just have to decrement the upper_bound. Subtracting 1 from the upper_bound will indicate that there are 53 valid data in the array.
  • 8.
  • 9.
    Two dimensional(2D) Array An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.
  • 10.
    Initialization of 2DArray  There are two ways to initialize a two Dimensional arrays during declaration.  int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} }; OR  int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};  Initialization of a 3d array int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
  • 11.
    Declaration  when weinitialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it.  However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration.  Let’s understand this with the help of few examples –  /* Valid declaration*/  int abc[2][2] = {1, 2, 3 ,4 }  /* Valid declaration*/  int abc[][2] = {1, 2, 3 ,4 }  /* Invalid declaration – you must specify second dimension*/  int abc[][] = {1, 2, 3 ,4 }  /* Invalid because of the same reason mentioned above*/  int abc[2][] = {1, 2, 3 ,4 }
  • 12.
    How to storeuser input data into 2D array  The array arr[n1][n2] can have n1*n2 elements.  The array that we have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts.  So this array has first subscript value as 5 and second subscript value as 4.So the array abc[5][4] can have 5*4 = 20 elements.  To store the elements entered by user we are using two for loops, one of them is a nested loop.  The outer loop runs from 0 to the (first subscript -1) and the inner for loops runs from 0 to the (second subscript -1).  This way the the order in which user enters the elements would be abc[0][0], abc[0][1], abc[0][2]…so on.
  • 13.
    Program for reading5 rows and 4 columns #include<stdio.h> int main() { /* 2D array declaration*/ int abc[5][4]; /*Counter variables for the loop*/ int i, j; for(i=0; i<5; i++) { for(j=0;j<4;j++) { printf("Enter value for abc[%d][%d]:", i, j); scanf("%d", &abc[i][j]); } } return 0; }
  • 14.
    // C programto find the sum of two matrices of order 2*2 #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrixn"); for (int row = 0; row < 2; ++row) for (int col = 0; col < 2; ++col) { printf("Enter a%d%d: ", row + 1, col + 1); scanf("%f", &a[row][col]); } // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for (int row = 0; row < 2; ++row) for (int col = 0; col < 2; ++col) { printf("Enter b%d%d: ", row + 1, col + 1); scanf("%f", &b[row][col]); }
  • 15.
    // adding correspondingelements of two arrays for (int row = 0; row < 2; ++row) for (int col = 0; col < 2; ++col) { result[row][col] = a[row][col] + b[row][col]; } // Displaying the sum printf("nSum Of Matrix:"); for (int row = 0; row < 2; ++row) for (int col = 0; col < 2; ++col) { printf("%.1ft", result[row][col]); if (col == 1) printf("n"); } return 0; }
  • 16.
    Merging Two Arrays Merging two arrays in a third array means first copying the contents of the first array into the third array and then copying the contents of the second array into the third array. Hence, the merged array contains the contents of the first array followed by the contents of the second array.  If the arrays are unsorted, then merging the arrays is very simple, as one just needs to copy the contents of one array into another. But merging is not a trivial task when the two arrays are sorted and the merged array also needs to be sorted.
  • 17.
    LIMITATIONS IN ARRAY Arrays are generally used when we want to store large amount of similar type of data. But they have the following limitations:  Arrays are of fixed size.  Data elements are stored in contiguous memory locations which may not be always available.  Insertion and deletion of elements can be problematic because of shifting of elements from their positions.