SlideShare a Scribd company logo
1 of 36
Download to read offline
1
Unit III
Array
2
Array
An array is defined as the collection of similar type of
data items stored at contiguous memory locations.
Arrays are the derived data type in C programming
language which can store the primitive type of data
such as int, char, double, float, etc. It also has the
capability to store the collection of derived data
types, such as pointers, structure, etc. The array is
the simplest data structure where each data element
can be randomly accessed by using its index number.
3
Properties of Array
The array contains the following properties.
● Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes.
●
Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
●
Elements of the array can be randomly accessed since we
can calculate the address of each element of the array
with the given base address and the size of the data
element.
4
Advantage of C Array
1) Code Optimization: Less code to the access the
data.
2) Ease of traversing: By using the for loop, we can
retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array,
we need a few lines of code only.
4) Random Access: We can access any element
randomly using the array.
5
Disadvantage of C Array
Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.
So, it doesn't grow the size dynamically like
LinkedList which we will learn later
6
Types of Array
Array
1D Array 2D Array Multi D Array
7
One Dimensional Array
A One-Dimensional Array is the simplest form of an
Array in which the elements are stored linearly and
can be accessed individually by specifying the index
value of each element stored in the array.
8
Declaration of C Array
data_type array_name[array_size];
For example: int num[5];
●
Memory representation:
●
9
Initialization of C Array
The simplest way to initialize an array is by using the
index of each element. We can initialize each element of
the array by using the index. Consider the following
example.
●
marks[0]=80;//initialization of array
●
marks[1]=60;
●
marks[2]=70;
●
marks[3]=85;
●
marks[4]=75;
10
Printing array elements
C array example
●
#include<stdio.h>
●
int main(){
●
int i=0;
●
int marks[5];//declaration of array
●
marks[0]=80;//initialization of array
●
marks[1]=60;
●
marks[2]=70;
●
marks[3]=85;
●
marks[4]=75;
●
//traversal of array
●
for(i=0;i<5;i++){
●
printf("%d n",marks[i]);
●
}//end of for loop
●
return 0;
●
}
11
C Array: Declaration with Initialization
We can initialize the c array at the time of
declaration. Let's see the code.
●
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the
size. So it may also be written as the following code.
●
int marks[]={20,30,40,50,60};
12
Accessing the Elements of an Array
#include<stdio.h>
●
int main(){
●
int i=0;
●
int marks[5]={20,30,40,50,60};//declaration and initialization of array
●
//traversal of array
●
for(i=0;i<5;i++){
●
printf("%d n",marks[i]);
●
}
●
return 0;
●
}
13
Storing Values in an array
14
Inputting Values in an Array
int i, marks[5];
●
●
for (i=0; i<5; i++)
●
{
●
scanf("%d", &marks[i]);
●
}
15
Simple Program to read and display n
numbers
16
Operations on Arrays
●
Traverse − print all the array elements one by one.
●
Insertion − Adds an element at the given index.
●
Deletion − Deletes an element at the given index.
●
Search − Searches an element using the given index
or by the value.
17
Traversal
●
Visiting every element
of an array once is
known as traversing the
array.
●
If we create an array of length 100
using a[100] in C language, we need
not use all the elements. It is
possible for a program to use just 60
elements out of these 100. (But we
cannot go beyond 100 elements).
18
Insertion
●
An element can be inserted in an array at a specific position. For this
operation to succeed, the array must have enough capacity. Suppose we
want to add an element 10 at index 2 in the below-illustrated array, then the
elements after index 1 must get shifted to their adjacent right to make way
for a new element.
●
●
●
●
●
●
●
When no position is specified, it’s best to insert the element at the end to
avoid shifting, and this is when we achieve the best runtime O(1)
19
Deletion
An element at a specified position can be deleted,
creating a void that needs to be fixed by shifting all
the elements to their adjacent left, as illustrated in
the figure .
We can also bring the last element of the array to fill
the void if the relative ordering is not important. :)
20
21
Searching
●
Searching can be done by traversing the array until the
element to be searched is found O(n) There is still a better
method. As you may remember, we talked about binary search
in some previous tutorials. Don't forget to look it up if you
missed it. We had analyzed both linear and binary search. This
search method is only applicable for sorted arrays. Therefore,
for sorted arrays, the time taken to search is much less than an
unsorted array. O(logn)
22
Sorting
Sorting means arranging an array in an orderly
fashion (ascending or descending). We have different
algorithms to sort arrays. We’ll see various sorting
techniques later in the course.
23
Program to insert an element in an array
24
Program to delete an element in an array
25
Program to search an element in an array
26
Two Dimensional Array in C
The two-dimensional array can be defined as an
array of arrays. The 2D array is organized as matrices
which can be represented as the collection of rows
and columns. However, 2D arrays are created to
implement a relational database lookalike data
structure. It provides ease of holding the bulk of data
at once which can be passed to any number of
functions wherever required.
27
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
●
data_type array_name[rows][columns];
Consider the following example.
●
int twodimen[4][3];
28
Initialization of 2D Array in C
●
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
●
int arr[4][3]={
{1,2,3},
{2,3,4},
{3,4,5},
{4,5,6}
};
●
int a[4][3] = {1,2,3,2,3,4,3,4,5,4,5,6};
29
Two-dimensional array example in C
30
C 2D array example: Storing elements in a matrix and printing it.
31
Adding and Subtracting Matrices
A matrix can only be added to (or
subtracted from) another matrix if the
two matrices have the same
dimensions .
32
Addition
33
Subtraction
34
Matrix multiplication in C
Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices
by the figure given below:
35
Transpose of Matrix
36
Sparse Matrix

More Related Content

Similar to Cunit3.pdf

Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanationTeresaJencyBala
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdfHEMAHEMS5
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...nsitlokeshjain
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programmingnmahi96
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsPrabu U
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptxdfsdg3
 

Similar to Cunit3.pdf (20)

Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays
ArraysArrays
Arrays
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
 
Array
ArrayArray
Array
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
 

More from zeenatparveen24

More from zeenatparveen24 (6)

8 Normalization (1).ppt
8 Normalization (1).ppt8 Normalization (1).ppt
8 Normalization (1).ppt
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
COCOMO.pptx
COCOMO.pptxCOCOMO.pptx
COCOMO.pptx
 
4 B-Coupling and Cohesion-1.pptx
4 B-Coupling and Cohesion-1.pptx4 B-Coupling and Cohesion-1.pptx
4 B-Coupling and Cohesion-1.pptx
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
cunit1.pptx
cunit1.pptxcunit1.pptx
cunit1.pptx
 

Recently uploaded

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

Cunit3.pdf

  • 2. 2 Array An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.
  • 3. 3 Properties of Array The array contains the following properties. ● Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. ● Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. ● Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
  • 4. 4 Advantage of C Array 1) Code Optimization: Less code to the access the data. 2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting: To sort the elements of the array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array.
  • 5. 5 Disadvantage of C Array Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later
  • 6. 6 Types of Array Array 1D Array 2D Array Multi D Array
  • 7. 7 One Dimensional Array A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.
  • 8. 8 Declaration of C Array data_type array_name[array_size]; For example: int num[5]; ● Memory representation: ●
  • 9. 9 Initialization of C Array The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example. ● marks[0]=80;//initialization of array ● marks[1]=60; ● marks[2]=70; ● marks[3]=85; ● marks[4]=75;
  • 10. 10 Printing array elements C array example ● #include<stdio.h> ● int main(){ ● int i=0; ● int marks[5];//declaration of array ● marks[0]=80;//initialization of array ● marks[1]=60; ● marks[2]=70; ● marks[3]=85; ● marks[4]=75; ● //traversal of array ● for(i=0;i<5;i++){ ● printf("%d n",marks[i]); ● }//end of for loop ● return 0; ● }
  • 11. 11 C Array: Declaration with Initialization We can initialize the c array at the time of declaration. Let's see the code. ● int marks[5]={20,30,40,50,60}; In such case, there is no requirement to define the size. So it may also be written as the following code. ● int marks[]={20,30,40,50,60};
  • 12. 12 Accessing the Elements of an Array #include<stdio.h> ● int main(){ ● int i=0; ● int marks[5]={20,30,40,50,60};//declaration and initialization of array ● //traversal of array ● for(i=0;i<5;i++){ ● printf("%d n",marks[i]); ● } ● return 0; ● }
  • 14. 14 Inputting Values in an Array int i, marks[5]; ● ● for (i=0; i<5; i++) ● { ● scanf("%d", &marks[i]); ● }
  • 15. 15 Simple Program to read and display n numbers
  • 16. 16 Operations on Arrays ● Traverse − print all the array elements one by one. ● Insertion − Adds an element at the given index. ● Deletion − Deletes an element at the given index. ● Search − Searches an element using the given index or by the value.
  • 17. 17 Traversal ● Visiting every element of an array once is known as traversing the array. ● If we create an array of length 100 using a[100] in C language, we need not use all the elements. It is possible for a program to use just 60 elements out of these 100. (But we cannot go beyond 100 elements).
  • 18. 18 Insertion ● An element can be inserted in an array at a specific position. For this operation to succeed, the array must have enough capacity. Suppose we want to add an element 10 at index 2 in the below-illustrated array, then the elements after index 1 must get shifted to their adjacent right to make way for a new element. ● ● ● ● ● ● ● When no position is specified, it’s best to insert the element at the end to avoid shifting, and this is when we achieve the best runtime O(1)
  • 19. 19 Deletion An element at a specified position can be deleted, creating a void that needs to be fixed by shifting all the elements to their adjacent left, as illustrated in the figure . We can also bring the last element of the array to fill the void if the relative ordering is not important. :)
  • 20. 20
  • 21. 21 Searching ● Searching can be done by traversing the array until the element to be searched is found O(n) There is still a better method. As you may remember, we talked about binary search in some previous tutorials. Don't forget to look it up if you missed it. We had analyzed both linear and binary search. This search method is only applicable for sorted arrays. Therefore, for sorted arrays, the time taken to search is much less than an unsorted array. O(logn)
  • 22. 22 Sorting Sorting means arranging an array in an orderly fashion (ascending or descending). We have different algorithms to sort arrays. We’ll see various sorting techniques later in the course.
  • 23. 23 Program to insert an element in an array
  • 24. 24 Program to delete an element in an array
  • 25. 25 Program to search an element in an array
  • 26. 26 Two Dimensional Array in C The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.
  • 27. 27 Declaration of two dimensional Array in C The syntax to declare the 2D array is given below. ● data_type array_name[rows][columns]; Consider the following example. ● int twodimen[4][3];
  • 28. 28 Initialization of 2D Array in C ● int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; ● int arr[4][3]={ {1,2,3}, {2,3,4}, {3,4,5}, {4,5,6} }; ● int a[4][3] = {1,2,3,2,3,4,3,4,5,4,5,6};
  • 30. 30 C 2D array example: Storing elements in a matrix and printing it.
  • 31. 31 Adding and Subtracting Matrices A matrix can only be added to (or subtracted from) another matrix if the two matrices have the same dimensions .
  • 34. 34 Matrix multiplication in C Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the figure given below: