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

HITEC UNIVERISTY, TAXILA CANTT

ENGINEERING DEPARTMENT OF ELECTRICAL


Computer Fundamentals
Assignment # 03

Submit To:
Ma’am Sadia Azam
Submitted by:
Fariha Saeed
Registration No:
17-EE-088
Section:
“A”

Date: 12-01-2018
Assignment # 04
Function
Task # 01:
Write a C++ program to merge two sorted array using functions. Your output
should be like this:

Output

Merged array in ascending order = 1, 2, 4, 5, 6, 8, 9, 10, 15

Program:
#include<iostream>
using namespace std;
void merge(int [], int [], int, int)

int main()
{ int arr1[100], arr2[100], arr3[100], a1, a2, i, j, k;
cout<<"\n How Many Elements You Want to Enter in First Array?: ";
cin>>a1;
cout<<"\n Enter Elements in a First Array : \n";
for (i = 0; i < a1; i++)
{ cout<<" ";
cin>>arr1[i]; }
cout<<"\n How Many Elements You Want to Enter in Second Array? : ";
cin>>a2;
cout<<"\n Enter Elements in a Second Array : \n";
for (i = 0; i < a2; i++)
{ cout<<" ";
cin>>arr1; }
merge(arr1, arr2, a1, a2);
cout<<"\n First Sorted Array : \n";
for (i = 0; i < a1; i++)
{ cout<<" ";
cout<<arr1[i];}
cout<<"\n\n Second Sorted Array : \n";
for (i = 0; i < a2; i++)
{ cout<<" ";
cout<<arr2[i];}
i = 0,j=0;
while (i < a1 && j < a2)
{ if (arr1[i] < arr2[j])
{ arr3[k] = arr1[i];
i++;}
else
{arr3[k] = arr2[j];
j++; }
k++; }
if (i >= a1)
{ while (j < a2)
{ arr3[k] = arr2[j];
j++;
k++; } }
if (j >= a2)
{ while (i < a1)
{ arr3[k] = arr1[i];
i++;
k++; } }
cout<<"\n\n Sorted Array After Merging : \n";
for (i = 0; i < a1 + a2; i++)
{ cout<<" ";
cout<<arr3[i]; }
return 0;}
void merge(int arr1[], int arr2[], int a1, int a2)
{ for (int i=a2-1; i>=0; i--)
{ int j, last = arr1[a1-1];
for (j=a1-1; j >= 0 && arr1[j] > arr2[i]; j--)
arr1[j+1] = arr1[j];
if (j != a1-1)
{ arr1[j+1] = arr2[i];
arr2[i] = last; } } }

Output:

Task # 02
Write a C++ Program to find maximum and minimum element in array.
Program:
#include<iostream>
using namespace std;

int main()
{
int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{ cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];

for(i=1;i<n;i++)
{
if(Arr[i]<small)
small=Arr[i];
if(Arr[i]>large)
large=Arr[i]; }

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;

return 0;
}
Output:

You might also like