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

DSA_lab

Tasks_lab3

SUBMITTED TO: Mam Rabia Arshad

SUBMITTED BY: Muhammad Bilal

Software Engineering Dept.


19-SE-18
19-se-18@students.uettaxila.edu.pk
Task No.1

#include<iostream>

using namespace std;

/* Print Function (Printing the Array) */


void printArray(int arr[],int size)
{
cout<<"\n Your New Array is: ";
for (int i = 0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl<<endl;
}
/* insert Function (Inserting the Element in Array) */
int insert(int arr[],int capacity,int size,int element,int index)
{
if(size>capacity)
return -1;
for(int i=size-1;i>=index;i--)
arr[i+1] = arr[i];
arr[index] = element;
return 1;
}

/* find_Delete Function (Finds the Element and Deletes it) */


void find_Delete(int arr[],int size,int element)
{
int location;bool found = false ;
for(int i=0;i<size;i++)
if(arr[i]==element)
{
location = i;
found = true;
}
if(!found)
{
cout<<"\n\t\t\t\t" <<element<<" Not Found...";
exit(0);
}
else
{
for(int i = location; i <= size; i++)
arr[i] = arr[i + 1];
}
}
int main()
{
/* Getting the Size and Elements of an Array */
Start:
int capacity,size;
cout<<"\n Enter the max Capacity of Array: ";
cin>>capacity;
cout<<"\n Enter the Size of Array: ";
cin>>size;
int arr[capacity];
cout<<"\n Enter "<<size<<" Array Elements: ";
for(int i = 0; i < size; i++)
cin>>arr[i];

/* Options -- Menu */
Menu:
cout<<"\n 1) Insertion";
cout<<"\n 2) Deletion";
int option;
cout<<"\n\n\t Select any Option --> ";
cin>>option;
switch (option)
{ /* Case 1 -- Insertion */
case 1:
int newElement,position,done;
cout<<"\n Insert a New Element: ";
cin>>newElement;
cout<<"\n Position of New Element: ";
cin>>position;
done = insert(arr,capacity,size,newElement,position);
if(done==-1)
{
cout<<"\n Array Size is Greater than its Capacity";
exit(0);
}
else
{
size++;
printArray(arr,size);
}
break;
/* Case 2 -- Deletion */
case 2:
if(size==0)
{
cout<<"\n\t\t\t\t Array is Empty -- Make sure that your Array is not
Empty"<<endl;
goto Start;
}
else
{
int delElement;
cout << "\n Enter the Element to be Deleted: ";
cin >> delElement;
find_Delete(arr,size,delElement);
size--;
printArray(arr,size);
}
break;
default:
cout<<"\n\t\t\t\t Invalid Choice -- Make Right Choice"<<endl;
goto Menu;
}
goto Menu;
return 0;
}
Output:
Exercise #1

#include<iostream>

using namespace std;

void printArray(int arr[],int size)


{
for (int i = 0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void mergeArrays(int A[],int B[],int m,int n)
{
for (int i = 0; i < m; i++)
{
if (A[i] > B[0])
{
swap(A[i], B[0]);
int first = B[0];
int k;
for (k = 1; k < n && B[k] < first; k++)
B[k - 1] = B[k];
B[k - 1] = first;
}
}
cout<<"\n A: ";
printArray(A,m);
cout << "\n B: ";
printArray(B,n);
}
int main()
{
int m;
cout<<"\n Enter the Size 1st of Array: ";
cin>>m;
int A[m];
cout<<"\n Enter "<<m<<" Elements: ";
for(int i = 0; i < m; i++)
cin>>A[i];

int n;
cout << "\n Enter the Size 2nd of Array: ";
cin >> n;
int B[n];
cout << "\n Enter " << n << " Elements: ";
for (int i = 0; i < n; i++)
cin >> B[i];
mergeArrays(A,B,m,n);
return 0;
}
Output:

You might also like