Module - 2
Array Declaration
An array is a variable that can store multiple
values under a single name
An array in C is a fixed-size collection of similar
data items stored in contiguous memory
location
used to store the collection of primitive data
types such as int, char, float, etc
size and type of an array cannot be changed once
it is declared.
Arrays declaration and initialization
• Syntax
• dataType arrayName[arraySize];
– Int a[5];
– Float b[10];
– Char str[20];
– Double d[10];
– Int a[] = {1,2,3,4,5};
– Int a[5] = {1,2,3,4};
ND array initialization
Example
Accessing and updating array elements
Reading and Displaying matrix
Linear Search Algorithm
Algorithm Linear_Search( a,size,key)
}}
{ a: array of elements; size : size of
the array if(found==0)
key : element to search in the array print(“element not found”)
found=0; return -1
for(i=0;i<size;i++) • }
{
if(a[i]==key)
{
found=1;
print(“element Found”)
return i;
Binary Search
Algorithm Binary_search(a,l,h,key) else if ( key > a[m])
{
a: array of elements
l=m+1;
l: lower limit; h: upper limit else
key: key element to search
h=m-1
found=0;
while(l<=h) }
{
return -1;
m=(l+h)/2
if(key==a[m]) }
return m;
Selection sort algorithm
Algorithm Selection_sort(a,size) a[i] ↔a[m]
{ a: array elements
}
size : size of the array
for(i=0;i<n-1;i++)
• }
{
m=i
for(j=i+1;j<n;j++)
{
If(a[m]<a[j])
m=j
• }
Bubble sort Algorithm
Algorithm Bubble_Sort(a,size)
{ a: array elements
size : size of the array
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{ if(a[j] > a[j+1])
a[j]↔a[j+1]