Arrays

You might also like

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

The University Of

Lahore

Name: Hamza Baig


PF-1
Reg no: BCS02183051
Date:1/2/2019

Union of two arrays:


#include<iostream>
using namespace std;
int main()
{
int a1[20],a2[20],u[40],i,j,k,n,m,flag;

cout<<"Enter elements of first array in ascending order:\n";


for(i=0;i<10;++i){
cin>>a1[i];
}
cout<<"\nEnter size of second array:";
cin>>m;
cout<<"Enter elements of second array in ascending order:\n";
for(i=0;i<10;++i){
cin>>a2[i];
}
k=0;
for(i=0;i<n;++i){
u[k]=a1[i];
k++;
}

for(i=0;i<m;++i){
flag=1;
for(j=0;j<n;++j){
if(a2[i]==a1[j]){
flag=0;
break;
}
}

if(flag){
u[k]=a2[i];
k++;
}
}

cout<<"\nUnion of two arrays is:\n";


for(i=0;i<k;++i){
cout<<u[i]<<" ";}}

Sorting array in ascending order


#include <iostream>
using namespace std;
int main()
{
int arr[10];
int size, i, j, temp;

cout<<"Enter elements in array: ";


for(i=0; i<10; i++)
{
cin>>arr[i];
}

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


{
for(j=i+1; j<10; j++)
{
//If there is a smaller element found on right of the array then swap it.
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

cout<<"Elements of array in sorted ascending order:"<<endl;


for(i=0; i<size; i++)
{
cout<<arr[i]<<endl;
}

return 0;
}
The University Of
Lahore

Name: Tanassar Hussain


PF-1
Reg no: BCS02183050
Date:1/2/2019

Union of two arrays:

You might also like