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

Array Insertion and Deletion Program

This document contains code to perform insertion and deletion operations on a linear array as well as code to traverse a linear array. For the insertion and deletion operations, the code prompts the user to enter array elements, performs the operations by shifting elements and changing the array size, and displays the original and modified arrays. For traversing, the code prompts the user to enter array elements, and then displays all elements. Both programs take in an array size and handle arrays of up to 100 elements.

Uploaded by

Rishabh Tomar
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)
99 views5 pages

Array Insertion and Deletion Program

This document contains code to perform insertion and deletion operations on a linear array as well as code to traverse a linear array. For the insertion and deletion operations, the code prompts the user to enter array elements, performs the operations by shifting elements and changing the array size, and displays the original and modified arrays. For traversing, the code prompts the user to enter array elements, and then displays all elements. Both programs take in an array size and handle arrays of up to 100 elements.

Uploaded by

Rishabh Tomar
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

WORKSHEET – 1

Name : Branch : CSE


UID : 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