0% found this document useful (0 votes)
40 views5 pages

Worksheet - 1.1: Name: Tarun Bisht Branch/Sec: CSE/29 - B UID: 20BCS6130 Subject: DS Lab

The document contains code to perform insertion and deletion operations on a linear array. It includes functions for inputting array elements, inserting an element at a given index, deleting an element at a given index, and displaying the array. The main function demonstrates calling these functions to insert and delete elements from a sample array and display the results. It also contains a second code example that traverses a linear array by inputting elements and displaying them.

Uploaded by

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

Worksheet - 1.1: Name: Tarun Bisht Branch/Sec: CSE/29 - B UID: 20BCS6130 Subject: DS Lab

The document contains code to perform insertion and deletion operations on a linear array. It includes functions for inputting array elements, inserting an element at a given index, deleting an element at a given index, and displaying the array. The main function demonstrates calling these functions to insert and delete elements from a sample array and display the results. It also contains a second code example that traverses a linear array by inputting elements and displaying them.

Uploaded by

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

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:

You might also like