0% found this document useful (0 votes)
26 views9 pages

DS Exp 1 Diya1

The document describes a menu driven C++ program that implements operations on a linear array including inserting elements, deleting elements, finding locations of elements, and displaying elements. The program code with functions to perform each operation is provided.

Uploaded by

Saugat Dutta
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)
26 views9 pages

DS Exp 1 Diya1

The document describes a menu driven C++ program that implements operations on a linear array including inserting elements, deleting elements, finding locations of elements, and displaying elements. The program code with functions to perform each operation is provided.

Uploaded by

Saugat Dutta
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/ 9

EXPERIMENT NUMBER –1

NAME – Diya SUBJECT – DATA STRUCTURE

UID – 21BCS2632 SUBJECT CODE – 21CSH-211

CLASS AND GROUP – 607-B SEMESTER – 3rd

AIM OF THE EXPERIMENT: WRITE A MENU DRIVEN PROGRAM THAT IMPLEMENT


FOLLOWING OPERATIONS (using separate functions) ON A LINEAR ARRAY:
1) INSERT A NEW ELEMENT AT END AS WELL AS AT A GIVEN POSITION.
2) DELETE AN ELEMENT FROM A GIVEN WHOSE VALUE IS GIVEN OR WHOSE POSITION
IS GIVEN.
3) TO FIND THE LOCATION OF A GIVEN ELEMENT.
4) TO DISPLAY THE ELEMENTS OF THE LINEAR ARRAY.

PROGRAM CODE:
#include <iostream>
using namespace std;
int main()
{
bool exit = false;
char YesNo;
while (!exit)
{

int n;
cout<<"NAME:DIYA \n";
cout<<"UID: 21BCS2632 \n";
cout<< " \n \n1. Insert a new element at end as well as at a given position \n";
cout<< "2. Delete an element from a given whose value is given or whose position is given. \n";
cout<< "3. To find the location of a given element. \n";
cout<< "4. To display the elements of the linear array. \n \n";
cout<< "Select between 1 to 4: ";
cin >> n;

if (n == 1)
{
int ch;
cout << " \n Type 0 for inserting element at the end \n";
DIYA_21BCS2632
cout << "Type 1 for inserting element at specific position \n \n";
cin >> ch;

if (ch == 0)
{
int size;
int position, num, i;
cout << "Enter number of elements - " << endl;
cin >> size;
int a[size];
cout << "Enter the elements in the array - " << endl;
for (int k = 0; k < size; k++)
{
cin >> a[k];
}
cout << "Enter the element to insert - ";
cin >> num;
int l = size;
a[l] = num;
cout << "The new array is - " << endl;
for (int j = 0; j < size + 1; j++)
{
cout << a[j] << " ";
}
}

else if (ch == 1)
{
int size;
int position, number, i;
cout << "Enter number of elements - " << endl;
cin >> size;
int a[size];
cout << "Enter the elements in the array - " << endl;
for (int k = 0; k < size; k++)
{
cin >> a[k];
}
cout << "Enter the element to insert - ";
cin >> number;
cout << "Enter the position at which you want to insert the new element- ";
cin >> position;
if (position > size + 1)
{
cout << "Insertion is not possible";
}
else
DIYA_21BCS2632
{
for (i = size; i >= position; i--)
{
a[i] = a[i - 1];
}
a[i] = number;
}
cout << "The new array is - " << endl;
for (int j = 0; j < size + 1; j++)
{
cout << a[j] << " ";
}
}
else
{
cout << "Invalid Input";
}
}

else if (n == 2)
{
int size;
int position, number, i;
cout << "Enter number of elements - " << endl;
cin >> size;
int a[size];
cout << "Enter the elements in the array - " << endl;
for (int k = 0; k < size; k++)
{
cin >> a[k];
}
cout << "Enter the position - ";
cin >> position;
if (position >= size + 1)
{
cout << "Deletion not possible ! " << endl;
}
else
{
for (int c = position - 1; c <= size - 1; c++)
{
a[c] = a[c + 1];
}
}
cout << "The new array is - " << endl;
for (int k = 0; k < size - 1; k++)
{
DIYA_21BCS2632
cout << a[k] << " ";
}
}

else if (n == 3)
{
int size;
int position, number, i;
cout << "Enter number of elements - " << endl;
cin >> size;
int a[size], fact = 0;
cout << "Enter the elements in the array - " << endl;
for (int k = 0; k < size; k++)
{
cin >> a[k];
}
cout << "Enter the number you want to search - ";
cin >> number;
for (int i = 0; i < size; i++)
{
if (number == a[i])
{
fact = 1;
position = i + 1;
}
}
if (fact == 1)
{
cout << "The number is found ! " << endl;
cout << "It is at the position : " << position << endl;
}
else
{
cout << "The number is not in the array bro!" << endl;
}
}

else if (n == 4)
{
int size;
int position, number, i;
cout << "Enter number of elements - " << endl;
cin >> size;
int a[size], fact = 0;
cout << "Enter the elements in the array - " << endl;
for (int k = 0; k < size; k++)
{
DIYA_21BCS2632
cin >> a[k];
}

cout << "The new array is - " << endl;


for (int k = 0; k < size; k++)
{
cout << a[k] << " ";
}
}
else
{
cout << "Invalid Number";
}

cout << " \n \n Do you want to continue? (Y or N) \n";


cin >> YesNo;
if (YesNo == 'N' || YesNo == 'n')
{
exit = true;
}
}
system("pause");
return 0;
}

DIYA_21BCS2632
DIYA_21BCS2632
DIYA_21BCS2632
DIYA_21BCS2632
OUTPUT:

DIYA_21BCS2632

You might also like