Abhishek  Jaiswal

Abhishek Jaiswal

  • 113
  • 20k
  • 8.6m

Correction required(syntax error i guess)

Nov 21 2013 8:41 AM

Q: Algorithm 3.

(inserting into a linear array)INSERT(LA,N,K,ITEM)

Here LA is a linear array with N elements and K is a positive integer such that K<=N.This algorithm inserts an element ITEM into the Kth Position in LA.


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


#include<iostream.h>

void INSERT(int*LA, int N, int K, int ITEM)

{

int j=N;

while(j>=K)

{

LA[j+1]= LA[j];

j--;

}

LA[K]=ITEM;

N=N+1;

}

int main(){

int N=0;

int La[50]={0};

int k;

int ITEM;

cout<<"Enter that Item you want to insert"<<endl;

cin>>ITEM;

cout<<"Enter position where you want to insert item"<<endl;

cin>>k;

INSERT(La,N,k,ITEM);

for(int k =0;k<50;k++)

{

cout<<k<<"="<<La[k]<<endl;

}

}


Q: Algprithm 4.

(Deleting from a linear array) DELETE (LA,N,K,ITEM)

Here LA is a linear array with N elements and K is a 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


#include<iostream.h>

void DELETE(int*LA,int N, int K,int ITEM)

{

ITEM=LA[K];

for(int j=K;j<=N-1;j++)

{

LA[j]=LA[j+1];

}

N=N-1;

}

int main(){

int N=10;

int LA[10]={2,4,6,8,10,12,14,16,18,20};

int K;

int ITEM;

cout<<"Enter number"<<endl;

cin>>ITEM;

cout<<"Enter position"<<endl;

cin>>K;

DELETE(LA,N,K,ITEM);

for(int k=0;k<10;k++)

{

cout<<k<<"="<<LA[k]<<endl;

}

}


Q: Algorithm 5.

Linear search(DATA,UB,LB,ITEM,I,LOC)

step1. Set I = LB, LOC = 0

step2. Repeat step3 while i <= UB

step3. if ITEM = Data [i] then

{

set LOC :=I

}

set I := I +1

[End of loop]

step4. [successful?]

if Loc = 0 then

write ITEM is not in array

else

write Loc is the Loc of ITEM

step5. Exit


#include<iostream.h>

int main(){

int Data[5]={2,4,6,8,10};

int Item;

int i = 0;

int loc = 0;

while(i<=5)

{

if(Item=Data[i])

loc=i;

i=i+1;

}

if(loc=0)

cout<<"ITEM is not in array";

else

cout<<"LOC is the LOC of ITEM";

}