WORKSHEET–
1.1
Name : Tarun Bisht Branch/Sec : CSE/29_B
UID : 20BCS6130 Subject : DS lab
Question 1:
Write a program to perform Insertion and deletion operation where Let LA
is a Linear Array (unordered) with N elements and K is a positive integer
such that K<=N.
Code:
#include <iostream>
using namespace std;
void inputArray(int arr[], int elements)
{
cout << "Enter the value of elements" << endl;
for (int i = 0; i < elements; i++) {
cin >> arr[i];
}
}
void insertElement(int arr[], int elements, int x, int n)
{
cout << "Enter the value of the element you want to insert: "
<< endl;
cin >> n;
cout << "Enter the index: " << endl;
cin >> x;
for (int i = elements - 1; i >= x; i--) {
arr[i + 1] = arr[i];
}
arr[x] = n;
}
void deleteElement(int arr[], int elements, int n)
{
cout << "Enter the index of the element you want to delete: "
<< endl;
cin >> n;
for (int i = n; i < elements - 1; i++) {
arr[i] = arr[i + 1];
}
}
void display(int arr[], int elements)
{
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
}
int main()
{
int n, x, elements, end;
int arr[100];
cout << "Enter the number of elements you want to enter: " <<
endl;
cin >> elements;
inputArray(arr, elements);
cout << "Original array: ";
display(arr, elements);
cout << endl;
insertElement(arr, elements, x, n);
cout << "Array after the insertion of value: ";
elements = elements + 1;
display(arr, elements);
cout << endl;
deleteElement(arr, elements, n);
cout << "Array after the deletion of value: ";
elements = elements - 1;
display(arr, elements);
cout << endl;
return 0;
}
Output:
Question 2:
Write a program that performs a traversing operation where Let LA is a
Linear Array (unordered) with N elements.
Code:
#include<iostream>
using namespace std;
int arr_input(int arr[], int elements){
cout<<"Enter the value of elements"<<endl;
for (int i = 0; i < elements ; i++) {
cin>>arr[i];
}
}
int display(int arr[] , int elements){
for (int i = 0; i < elements; i++) {
cout<<arr[i]<<" ";
}
}
int main(){
int n,x,elements,end;
int arr[100];
cout<<"Enter the number of elements you want to enter: ";
cin>>elements;
arr_input(arr, elements);
cout<<"Elements in the array are: ";
display(arr,elements);
return 0;
}
Output: