SlideShare a Scribd company logo
Array 31.8.2020 updated
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.
Array 31.8.2020 updated
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.

More Related Content

What's hot (20)

PDF
Arrays in C
Kamruddin Nur
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPT
Data Structure Midterm Lesson Arrays
Maulen Bale
 
PPT
Multidimensional array in C
Smit Parikh
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPTX
Arrays
Trupti Agrawal
 
PPT
C++ Arrays
أحمد محمد
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPT
Array in c
Ravi Gelani
 
PPTX
Arrays
Chirag vasava
 
PPT
Arrays and structures
Mohd Arif
 
PPT
1 D Arrays in C++
poonam.rwalia
 
PPTX
Arrays in Data Structure and Algorithm
KristinaBorooah
 
PPT
Arrays in C++
Janpreet Singh
 
PDF
Array
hjasjhd
 
PPTX
Programming in c arrays
Uma mohan
 
PPTX
Arrays in c
Jeeva Nanthini
 
PPT
Array
PRN USM
 
Arrays in C
Kamruddin Nur
 
C programming , array 2020
Osama Ghandour Geris
 
Unit 6. Arrays
Ashim Lamichhane
 
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Multidimensional array in C
Smit Parikh
 
Arrays & Strings
Munazza-Mah-Jabeen
 
C++ Arrays
أحمد محمد
 
Programming in c Arrays
janani thirupathi
 
Array in c
Ravi Gelani
 
Arrays and structures
Mohd Arif
 
1 D Arrays in C++
poonam.rwalia
 
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Arrays in C++
Janpreet Singh
 
Array
hjasjhd
 
Programming in c arrays
Uma mohan
 
Arrays in c
Jeeva Nanthini
 
Array
PRN USM
 

Similar to Array 31.8.2020 updated (20)

PDF
Introduction to Arrays in C
Thesis Scientist Private Limited
 
PPT
Chap 6 c++
Venkateswarlu Vuggam
 
PDF
Chap 6 c++
Venkateswarlu Vuggam
 
PDF
02 arrays
Rajan Gautam
 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PDF
Array in C full basic explanation
TeresaJencyBala
 
PDF
ARRAYS
muniryaseen
 
PPT
Arrays
SARITHA REDDY
 
PPT
Chapter 10.ppt
MithuBose3
 
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
PPTX
arrays.pptx
NehaJain919374
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PDF
Data structures arrays
maamir farooq
 
PDF
Array&amp;string
chanchal ghosh
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Array In C++ programming object oriented programming
Ahmad177077
 
PDF
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Introduction to Arrays in C
Thesis Scientist Private Limited
 
02 arrays
Rajan Gautam
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Array in C full basic explanation
TeresaJencyBala
 
ARRAYS
muniryaseen
 
Chapter 10.ppt
MithuBose3
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
arrays.pptx
NehaJain919374
 
Chapter 13.pptx
AnisZahirahAzman
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Data structures arrays
maamir farooq
 
Array&amp;string
chanchal ghosh
 
Data structure array
MajidHamidAli
 
Array In C++ programming object oriented programming
Ahmad177077
 
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Ad

More from vrgokila (6)

PPT
evaluation technique uni 2
vrgokila
 
PPT
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
vrgokila
 
PPT
Unit 2 hci
vrgokila
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
PDF
Unit 1 ocs752 introduction to c programming
vrgokila
 
PPT
Array
vrgokila
 
evaluation technique uni 2
vrgokila
 
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
vrgokila
 
Unit 2 hci
vrgokila
 
Basic c programs updated on 31.8.2020
vrgokila
 
Unit 1 ocs752 introduction to c programming
vrgokila
 
Array
vrgokila
 
Ad

Recently uploaded (20)

PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Hashing Introduction , hash functions and techniques
sailajam21
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 

Array 31.8.2020 updated

  • 2. 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
  • 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 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.
  • 7. 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.
  • 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 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}}};
  • 11. 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 }
  • 12. 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.
  • 13. 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; }
  • 14. // 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]); }
  • 15. // 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; }
  • 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.