Professional Documents
Culture Documents
✔ int a[10];
✔ float height[5];
✔ double price[10];
✔ char s[12];
Address of Array Elements
✔ Formula : address(k) = base(a) + w( k – lower_bound)
✔ k : index of the element for which the address is to be calculated.
✔ a : name of the array.
✔ base(a) : base address of array ‘a’.
✔ w : word size of a single element.
✔ lower_bound : index of the first element in the array [usually zero].
✔ Ex.
✔ Solution
✔ Given base(a) = 1200.
✔ Since it is an integer array, w = 4 bytes.
✔ Lower_bound = 0
✔ k=index of ‘40’ = 7
✔ address(40) = 1200 + 4 * (7-0) = 1228
Array Initialization
✔ int a[5] = {1, 2, 3, 4, 5};
✔ int a[10] = {1, 2, 3, 4, 5};
✔ float b[] = {12.64, 53.53, 7.4566};
✔ char c[10] = {‘a’,’p’,’p’,’l’,’e’};
✔ char d[]={‘o’,’r’,’a’,’n’,’g’,’e’};
✔ char city[]=”Coimbatore”;
Characteristics of Arrays
✔ The declaration of an array creates an array that would hold a maximum
of elements. In other words, it allocates memory space required for the
maximum number of specified data contiguously.
✔ All the elements of an array should be of the same data type.
✔ All the elements of an array share the same name but they are identified
by the subscript or index value. It is also called as the element number or
position of the element.
✔ Any element of an array can be modified without disturbing the other
elements.
✔ Any element of an array can be assigned to a variable of the same type.
✔ e.g. x = a[2];
}
int main() { cout<<"\nEnter the element to be inserted : ";
{ 0 1 2 3 4
int i;
for (i=0;i<n;i++) 10 20 30 40 50
if (a[i]==t)
break;
t = 30
for (i;i<n;i++)
10 == 30 => false
a[i] = a[i+1]; 20 == 30 => false
} 30 == 30 => true
int main() { cout<<"\nEnter the element to be
deleted : ";
int a[10],n,t,pos;
cin>>t;
cout<<"Enter the number of elements : ";
del(a,n,t);
cin>>n;
n--;
read(a,n);
print(a,n);
print(a,n);
4. Searching an element in an array
int search(int a[10], int n, int t)
{
int i;
for (i=0;i<n;i++)
if (a[i]==t)
break;
if (i<n)
return 1;
else
return 0;
}
int main() { cout<<"\nEnter the element to be
searched : ";
int a[10],n,found,t;
cin>>t;
cout<<"Enter the number of elements : ";
found=search(a,n,t);
cin>>n;
if (found==1)
read(a,n);
cout<<t<<" is found in the array";
print(a,n);
else