Oop PR4
Oop PR4
Oop PR4
Output:
9. Can you declare an array without assigning the size of an array?
Ans) No, we cannot declare an array without assigning the size of an array.
Exercise
1. Write a C++ program to find median of two sorted arrays of same size.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a1[100],a2[100],n1,n2,sum1=0,sum2=0,median1,median2,medianmax,sum,i;
cout<<endl<<"Output prepared by Aswhath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter the size of the 1st array: ";
cin>>n1;
cout<<endl<<"Enter elemnts of 1st array: ";
for(i=0;i<n1;i++)
{
cin>>a1[i];
sum1=sum1+a1[i];
}
cout<<"Enter the size of the 2nd array: ";
cin>>n2;
cout<<endl<<"Enter elemnts of 2nd array: ";
for(i=0;i<n2;i++)
{
cin>>a2[i];
sum2=sum2+a2[i];
}
cout<<endl<<"Elements of 1st array are: ";
for(i=0;i<n1;i++)
{
cout<<a1[i]<<"\t";
}
cout<<endl<<"Elements of 2nd array are: ";
for(i=0;i<n2;i++)
{
cout<<a2[i]<<"\t";
}
median1=sum1/n1;
median2=sum2/n2;
medianmax=(median1+median2)/2;
cout<<endl<<"Median of two sorted array is: "<<medianmax;
getch();
}
Output:
2. Write a C++ program to find the two repeating elements in a given array.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a1[50], no,i,j;
cout<<"Output prepared by Aswhath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter size of the array:";
cin>>no;
cout<<endl<<"Enter array elements:";
for(i=0;i<no;i++)
{
cin>>a1[i];
}
cout<<endl<<"Repeated elements in the array are:";
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if (a1[i]==a1[j])
{
cout<<a1[j]<<" \t";
}
}
}
getch();
}
Output:
3. Find the smallest and second smallest elements in an array.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],no,small,sm,i,j;
clrscr();
cout<<endl<<"Output prepared by Ashwath Bhekare (CO3IB) 22203B0015";
cout<<endl<<"Enter size of the array: ";
cin>>no;
cout<<endl<<"Enter array elements: ";
for(i=0;i<no;i++)
{
cin>>a[i];
}
cout<<endl<<"Array elements: ";
for(i=0;i<no;i++)
{
cout<<a[i]<<"\t";
}
small=a[0];
sm=small;
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
if(a[j]<a[i]&&a[j]!=small)
{
sm=a[j];
}
if(a[j]<small)
{
small=a[j];
}
}
}
cout<<endl<<"Smallest element: "<<small<<endl<<"Second Smallest
element:"<<sm;
getch();
}
Output: