0% found this document useful (0 votes)
17 views4 pages

DSA Lab#02

The document discusses operations on linear arrays in C++ including traversing, inserting, and deleting elements. It provides algorithms and example programs for each operation. The objective is to understand how to implement various operations on arrays. Key concepts covered include traversing an array, inserting an element into an array, and deleting an element from an array.

Uploaded by

muh.saad.0010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

DSA Lab#02

The document discusses operations on linear arrays in C++ including traversing, inserting, and deleting elements. It provides algorithms and example programs for each operation. The objective is to understand how to implement various operations on arrays. Key concepts covered include traversing an array, inserting an element into an array, and deleting an element from an array.

Uploaded by

muh.saad.0010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab#:02

Implementation of different operations on the Linear Arrays

Objective:-

The objective of this Lab is to understand the various operations on arrays in C++.

Software Tools:-

Dev C++
Theory:-

We have already studied different operations that can be performed on array data
structures. We would be using the knowledge we learned there to implement
different operation on arrays.

Traversing Linear Arrays:-


Let A be the collection of data elements stored in the memory of the computer.
Suppose we want to print the contents of each element of A or suppose we want to
count the number of elements of A with a given property. This can be accomplished
by traversing A that is by accessing and processing each element of A exactly once.
The following algorithm traverses a linear array. The simplicity of the algorithm
comes from the fact that LA is a linear structure. Other linear structures such as
linked list can also be easily traversed. On the other hand the traversal of non-linear
structures such as trees and graphs is considerably more complicated.

Algorithm:-
(Traversing a Linear Array) Here LA is a linear Array with lower Bound LB and upper
Bound UB. This algorithm traverses LA.
Applying an operation PROCESS to each element of LA.
1. [Initialize counter] Set K=LB.
2. Repeat Step 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

HIET
Example Program:-

#include <iostream>
using namespace std;
int main()
{
int arr[5]={3, 15, 60, 45, 37};
//traversing
for (int k = 0; k < 5; k++)
{
cout<<arr[k]<<endl;
}
}

Inserting and Deleting:-


Let A be a collection of data elements in the memory of computer. “Inserting” refers
to the operation of adding another element to the collection A and “deleting” refers
to the operation of removing one of the elements from A. Here we discuss the
inserting and deleting when A is a linear array.
Inserting an element at the “end” of the linear array can be easily done provided the
memory space allocated for the array is large enough to accommodate the additional
element. On the other hand suppose we need to insert an element in the middle of
the array. Then on average half of the elements must be moved downward to the
new location to accommodate the new element and keep the order of other
elements.

Similarly deleting the element at the “end” of an array presents no difficulties but
deleting the element somewhere in the middle of the array would require that each
subsequent element be moved one location upward in order to fill up the array.

HIET
Algorithm:-
(Inserting into 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=Length.


2. Repeat Step 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.

Example Program:-

#include <iostream>
using namespace std;
int main()
{
int array[100], k, j, n, val;
cout<<"Enter number of elements in array"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for (j = 0; j < n; j++)
{
cin>>array[j];
}
cout<<"Enter the location where you want to insert the new element"<<endl;
cin>>k;
cout<<"Enter the value of element to be inserted"<<endl;
cin>>val;
for (j = n - 1; j >= k - 1; j--)

HIET
{
array[j+1] = array[j];
}
array[k-1] = val;
cout<<"The New array after insertion: "<<endl;
for (j = 0; j <= n; j++)
{
cout<<array[j]<<endl;
}
return 0;
}

Algorithm:-
(Deletion from a Linear Array) DELETE(LA, N, K)

Here LA is a linear array with N elements and K is a positive integer such that pos≤N.
This algorithm deletes an element ITEM from the Kth position in LA.

1. [Initialize Counter] Set J=K.


2. Repeat Step 3 and 4 while J<N-1.
3. [Move Jth element upward] Set LA [J] =LA[J+1].
4. [Increase Counter] Set J=J+1.
5. End of Step 2 Loop.
6. [Reset N] Set N=N-1.
7. Exit.

Lab Task:-

Write C++ program to implement all the above described algorithms and display
the output for each.

HIET

You might also like