1.1 Data Structures

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Student Name:Naman Sharma UID: 20BCS6584

Branch: CSE AIML Section/Group: Aiml 3-A


Semester: 3rd Date of Performance:28/8/2021
Subject Name: Data Structures Subject Code:20CSP-236

1. Aim/Overview of the practical:


Write a program to implement following operations on a linear array:
1. Read n elements and display
2. Insert a new element in the middle of an array.
3. Delete the first element of an array.
4. Find the location of a last element.

Code:
#include<iostream>
using namespace
std;

int a[10];
int n, i, j, val, pos;

void create();
void display();
void insert();
void del();
void search();

int main()
{
int
choice;
do{
cout<<"***************MENU OF THE
PROGRAM******************"<<endl; cout<<"1.Create an array"<<endl;
cout<<"2.Display the array"<<endl;
cout<<"3.Insert an element"<<endl;
cout<<"4.Delete an
element"<<endl;
cout<<"5.Search an
element"<<endl;
cout<<"6.Exit"<<endl;

cout<<"Choose a option (1-6)"<<endl;


cin>>choice;

switch(choice)
{ case 1:
create();
cout<<endl;break
; case 2: display();
cout<<endl;break
; case 3: insert();
cout<<endl;break
; case 4: del();
cout<<endl;
break; case 5: search();
cout<<endl;break
; case 6: exit(0);

default: cout<<"Invalid
choice:"; break;

}while(choice!

=6); return 0;
}

void create(){
cout<<"Enter the no. of elements in the array:
"; cin>>n;
cout<<"Enter the elements of array: "<<endl;

for(i=0;i<n;i++){
cin>>a[i];
}
cout<<endl;
}

void display(){
cout<<"The array is following:"<<endl;

for(i=0;i<n;i++)
{ cout<<a[i]<<"
";
}
cout<<endl;
}
void insert(){
cout<<"Enter the position of new element: ";
cin>>pos;
cout<<"Enter the value of new element:
"; cin>>val;

for(i=n-1;i>=pos;i--)
{ a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
cout<<"Updated Array:
"; for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void del(){
cout<<"Enter the position of element to delete:";
cin>>pos;
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<"The deleted element is =
"<<val<<endl; cout<<"Updated Array: ";
for(i=0;i<n;i++)
{ cout<<a[i]<<"
";
}
cout<<endl
;
cout<<endl
;
}

void search(){
int elem;
cout <<"Enter the
element :"; cin >> elem;

for( i = 0; i < n ; i++)


{ if(a[i] == elem){
cout << "Element found at position: "<< i;
break;
}}
}
5. Output: Image of sample output to be attached here
ufiivERsiTY e

] Run >

"ROBLEMS 3 OU+"U+TERMINALDEBUG CONSOLE

*** **

Python 3.9.6 64-bit @ 30 Ln J15, Col 1 Spaces: 4 UTF-8 CRLF C+ + Q

X
X

} File Edit Selection View Go Run Terminal HeI|s #include<iostream > • Untitled-4 - Visual Studio Code ”””*”””**””**””MENU OF
THE PROGRAM”” * *”” ** ”””
"ROBLEMS 3 OU+"U+ TERMINAL DEBUG CONSOLE * ””” * ””
1.
C
Updated Array: 11 22 66 33 44 55
r
eate an array
2.Display the — EI X
array 3.Insert an
Code + X
element 4.Delete
an element
5.Search an
element
6. Exit
Choose a option (1—6)

Enter the position of element to


delete:4 The deleted element is = 44
Updated Array: 11 22 66 33 55

***************MENU OF THE
PROGRAM****************** 1.Create an array
2.Display the
array 3.Insert an
element 4.Delete
an element
5.Search an
element 6.Exit
Choose a option (1-
6) 5
Enter the element :66
Element found at
position: 2
Python 3.9.6 64-bit @ 3 0

Ln J 15, Col 1 Spaces: 4 UTF-8 CRLF C+ + Q Q


Learning outcomes (What I have learnt):

1.

2.

3.

4.

5.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like