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

Array inset ,dell ,search

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)
26 views7 pages

Array inset ,dell ,search

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
You are on page 1/ 7

Data Structure & Algorithm

Assignment:
Array (Insertion, Deletion & Searching)

Submitted by:
Junaid Iqbal

Roll No:
186

Semester:
BSCS 3rd (evening)

Session:
2020-2024

Submitted to:
Noreen Khalid

Submission date:
09-November-2021

1|Page
Table of Content

1. Insertion in Array 03
1.1 Code (Insertion in Array) 03
1.2 Output (Insertion in Array) 04
2. Deletion in Array 04
2.1 Code (Insertion in Array) 04
2.2 Output (Deletion in Array) 05
3. Searching in Array 06
3.1 Code (Insertion in Array) 06
3.2 Output (Searching in Array) 07

2|Page
Insertion in Array

Code (insertion in Array)


#include<iostream>
using namespace std;
int main()
{
int arr[50], i, elem, pos, size;
cout<<"Enter the Size for Array: ";
cin>>size;
cout<<"Enter "<<size<<" Array Elements: ";
for(i=0; i<size; i++)
cin>>arr[i];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
cout<<"At What Position ? ";
cin>>pos;
for(i=size; i>=pos; i--)
arr[i] = arr[i-1];
arr[i] = elem;
size++;
cout<<"\nThe New Array is:\n";
for(i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}

3|Page
Output (insertion in Array)

Deletion in Array

Code (Deletion in Array)


#include<iostream>
using namespace std;
int main()
{

4|Page
int arr[10], tot=10, i, elem, j, found=0;
cout<<"Enter 10 Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nEnter Element to Delete: ";
cin>>elem;
for(i=0; i<tot; i++)
{
if(arr[i]==elem)
{
for(j=i; j<(tot-1); j++)
arr[j] = arr[j+1];
found++;
i--;
tot--;
}
}
if(found==0)
cout<<"\nElement doesn't found in the Array!";
else
cout<<"\nElement Deleted Successfully!";
cout<<endl;
for(i=0; i<tot; i++)
cout<<arr[i]<<endl;
return 0;
}

Output (Deletion in Array)

5|Page
Searching in Array

Code (Searching in Array)

#include<iostream>
using namespace std;

int main()
{
int arr[100], i, num, n, cnt=0, pos;
cout<<"\n Enter Array Size : ";
cin>>n;
cout<<"\n Enter Array Elements : \n";
for(i=0; i<n; i++)
{
cout<<" ";
cin>>arr[i];
}
cout<<"\n Enter Element to be Searched : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
cnt=1;
pos=i+1;
break;
}

6|Page
}
if(cnt==0)
{
cout<<"\n Element Not Found..!!";
}
else
{
cout<<"\n Element "<<num<<" Found At Position "<<pos;
}
return 0;
}

Output (Searching in Array)

7|Page

You might also like