0% found this document useful (0 votes)
10 views11 pages

Exp 4 & Exp 6

Uploaded by

dhananjay mane
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)
10 views11 pages

Exp 4 & Exp 6

Uploaded by

dhananjay mane
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/ 11

EXP 4.

1 program to find median of two sorted arrays

#include <iostream.h>

#include<conio.h>

void main()

int a1[50], a2[50], a3[100], m, n, i, j, k = 0;

clrscr();

cout<<"\n Enter size of array Array 1: ";

cin>>m;

cout<<"\n Enter sorted elements of array 1: \n";

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

cin>>a1[i];

cout<<"\n Enter size of array 2: ";

cin>>n;

cout<<"\n Enter sorted elements of array 2: \n";

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

{
cin>>a2[i];

i = 0;

j = 0;

while (i < m && j < n)

if (a1[i] < a2[j])

a3[k] = a1[i];

i++;

else

a3[k] = a2[j];

j++;

} k+

+;

if (i >= m)

while (j < n)
{

a3[k] = a2[j];

j++;

k++;

if (j >= n)

while (i < m)

a3[k] = a1[i];

i++;

k++;

cout<<"\n After merging: \n";

for (i = 0; i < m + n; i++)

cout<<" "<<a3[i];

int len=m+n; int

mid = len/ 2;
float median;

if (len % 2)

median = a3[mid] ;

cout << "\nThe median is: " << median << endl;

else

median = (a3[mid]+a3[mid-1])/2.0;

cout << "\nThe median is: " << median << endl;

getch();

EXP 4.2 program to find two repeating elements in a given array

#include<iostream.h>

#include<conio.h>

void main()

int i,arr[20],j,no;

clrscr();

cout<<"Enter Size of array: ";


cin>>no;

cout<<"Enter any "<<no<<" num in array: ";

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

cin>>arr[i];

cout<<"Dublicate Values are: ";

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

for(j=i+1;j<no;j++)

if(arr[i]==arr[j])

cout<<"\n"<<arr[i];

getch();

Output

Enter Size of Array : 5

Enter any 5 elements in Array:

54523

Duplicate Elements are:


5

EXP 4.3 program to find smallest and second smallest element in a given array

#include <iostream.h>

int main()

int arr[50], n;

cout<<"\n Enter number of elements in array: ";

cin>>n;

cout<<"\n Enter array elements: ";

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

cin>>arr[i];

int small,next_small=INT_MAX;

small = arr[0];

for(int i=1;i<len;i++)

if(arr[i] < small)

next_small = small;

small = arr[i];

else if(arr[i] < next_small and arr[i] > small)

next_small = arr[i];

}
cout << "Smallest and the second smallest numbers are respectively "<< small
<< " and " << next_small <<endl;

return 0;

EXP 4.4 program to find missing number from array

#include <iostream.h>

int main()

int n=5,i;

int arr[4];

cout<<"Enter sequential array elements: ";

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

cin>>arr[i];

int temp;

temp = ((n+1)*(n+2))/2; //sum of n elements is n(n+1)/2,

for (i = 0; i<n; i++) //here one no. is missing so we use (n+1)(n+2)/2

temp -= arr[i];

int missingNo =temp;


cout<<"Missing Number is: "<<missingNo;

return 0;

EXP6.1 Write a c++ program to declare a class book having data members
book_name, author and price. Accept and display data for book having maximum
price.

#include<iostream.h>

#include<conio.h>

#include<string.h>

class Book

int no_of_pages;

char book_name[50];

public:

float price;

void getdata()

cout<<"Enter Book name:-> ";

cin>>book_name;

cout<<"Enter Book Price:->";

cin>>price;

cout<<"Enter No of pages:-> ";

cin>>no_of_pages;
}

void display()

cout<<"\nBook name:-> "<<book_name;

cout<<"\nBook Price:->"<<price; cout<<"\

nNo of pages:-> "<<no_of_pages;

};

void main()

Book b1,b2;

clrscr();

b1.getdata();

b2.getdata();

if(b1.price>b2.price)

b1.display();

else

b2.display();

getch();

}
EXP 6.2 Write a c++ program to declare a class staff having data members name,
salary, DA, HRA and calculate gross salary. Accept and display data for one staff.

Where DA=74.5% of basic, HRA=30% of basic, gross_salary= basic+DA+HRA

#include<iostream.h>

#include<conio.h>

class staff

float basic,gross_sal;

public:

void accept()

cout<<"Enter Basic Salary of Staff: ";

cin>>basic;

float display ();

};

float staff :: display()

{ float DA=74.5, HRA=30;

gross_sal=(basic*DA/100)+(basic*HRA/100)+basic;

return(gross_sal);

void main()

clrscr();
staff s1;

s1.accept();

cout<<"Gross Salary of Employee "<<s1.display();

getch();

You might also like