Operations on Arrays:-
·
Traversal
·
Insertion
·
Search
·
Deletion
·
Merging
·
Sorting
Traversal:- means accesing each element of the Array.(including printing
every elements, counting the number of elements).
#include<stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at
location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] =
%d\n", j, n[j] );
}
return 0;
}
Insertion:- it means adding a new data into an existing array. If you
forget to provide certain value in the array at a perticular location; use this
algorithm to insert new value in the array instead of creating a new array.
1)
Set i= n-1 ( upper bound)
2)
Start Loop: i<=pos
A[i+1]=A[i];
i--;
end of
loop.
3)
A[pos]=val;
4)
Set n=n+1;
5)
Exit.
|
n=length of the array.
pos= the position at which
the element has to be inserted.
val= the value that has to
be inserted.
Delition:to delete an existing data from an array.
Algorithm:
1) Set i=pos-1;
2) Start loop: i<=n; (condition)
Set
A[i]=A[i+1];
i=i+1;
end of loop
3) n=n-1;
4) exit
|
Merging:
Search: searching means to whether the number is present in the
array or not. And if present, then find its position.
Lenear search: check every element of the array with the value you want to
find.
Binary search:
Sorting:
No comments:
Post a Comment