ARRAY Traversing ALGORITHM:
This can be accomplished by traversing A, that is, by accessing and processing each element of A exactly once.
ARRAY Insertion
Inserting an element at end of linear array can be done easily if the array is large enough to accommodate new item.
If the element is to be inserted in the middle of array then half members must be moved downward to new location to accommodate new element by keeping the order of other elements
ARRAY INSERTION ALGORITHM
This can be accomplished by traversing A, that is, by accessing and processing each element of A exactly once.
1. [Initialize counter] Set k: =LB.
2. Repeat steps 3 and 4 while k
<=UB.
3. [Visit Element] Apply PROCESS to LA
[k].
4. [Increase Counter] Set k: =k + 1.
[End of step 2 loop]
5. Exit.
Inserting an element at end of linear array can be done easily if the array is large enough to accommodate new item.
If the element is to be inserted in the middle of array then half members must be moved downward to new location to accommodate new element by keeping the order of other elements
ARRAY INSERTION ALGORITHM
Algorithm for Insertion: (Inserting
into Linear Array) INSERT (LA, N, K, ITEM)
1. [Initialize counter] Set J: = N.
2. Repeat Steps 3 and 4 while j >=
k;
3. [Move jth element downward.] Set LA [J + 1]: =LA
[J].
4. [Decrease counter] Set J: = J-1
[End of step 2 loop]
5. [Insert element] Set LA [K]:=ITEM.
6. [Reset N] Set N:=N+1
7. EXIT.
ALGORITHM FOR DELETION IN ARRAY
Algorithm for Deletion: (Deletion from
a Linear Array) DELETE (LA, N, K, ITEM)
Here LA is a Linear Array with N elements and K is the
positive integer such that K<=N. This algorithm deletes the Kth element from LA.
1. Set ITEM: = LA [k].
2. Repeat for J = K to N – 1.
[Move J + 1st element upward] Set LA [J]: = LA [J
+1].
[End of loop]
3. [Reset the number N of elements in
LA] Set N: = N-1
4. EXIT
9 comments:
If the algorithms come with examples I hope they will be easier to understand by beginners.
Insertion using for loop its very simple compare to while loop
plz give examplasss
Insertion:
#include
int main( )
{
int a[100], k, j, n, element;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (j = 0; j < n; j++)
scanf("%d", &a[j]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &k);
printf("Enter the value to insert\n");
scanf("%d", &element);
for (j = n - 1; j >= k - 1; i--)
a[j+1] = a[j];
a[k-1] = element;
printf("Resultant array is\n");
for (j = 0; j <= n; j++)
printf("%d\n", a[j]);
return 0;
}
Deletion:
#include
int main( )
{
int a[100], position, j, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( j = 0 ; j < n ; j++ )
scanf("%d", &a[j]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &k);
if ( k >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( j = k - 1 ; j < n - 1 ; j++ )
array[j] = array[j+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
#include
#include
using namespace std;
void traverse(int A[], int size)
{
for(int i=0 ; ilocation)
{
A[i+1]=A[i];
i=i-1;
}
A[location]=value;
size=size+1;
traverse(A, size);
}
void delete(int A[], int size, int value)
{
int del=0;
for(int i=0 ; i<size ; i++)
{
if(A[i]==value || del=1)
{
A[i]=A[i+1];
del = 1;
}
}
bool is = false;
}
int main()
{
int A[4]={1, 2, 5, 7};
cout<<"\ntraverse: \t";
traverse(A, 4);
cout<<"\nafter insertion: \t";
insert(A, 4, 1, 9);
cout<<"\nafter deletion: \t";
insert(A, 4, 2, 9);
cout<<"\nafter deletion: \t";
delete(A, 4, 9, 4);
traverse(A, 4);
}
i have an error
i.e: [ERROR] expected unqualified-id before 'delete'
in above program, can any one tell me what coding have i done wrong in this program?????????
Traversing in an array Java code pls
please provide me.....algorithm to delete every third element from a linear array
Post a Comment