Professional Documents
Culture Documents
1. Given 2 integer arrays A[ ] and B[ ] of sizes M and N respectively. Write a function named
MIX( ) which will produce a third array named C[ ], in which the following sequence is
followed.
All even numbers of A from left to right are copied into C from left to right
All odd numbers of A from left to right are copied into C from right to left
All even numbers of B from left to right are copied into C from left to right
All odd numbers of B from left to right are copied into C from right to left
A, B and C are passed as arguments to MIX( ).
Eg: A is {3,2,1,7,6,3} and B is {9,3,5,6,2,8,10} c is {2,6,6,2,8,10,5,3,9,3,7,1,3}
#include<iostream.h>
#include<conio.h>
void MIX(int a[ ],int b[ ],int c[ ],int m,int n)
{ int i,j=m+n-1,k=0;
for(i=0;i<m;i++)
{ if(a[i]%2==0)
{ c[k]=a[i];
k++;
}
else
{ c[j]=a[i];
j--;
}
}
for(i=0;i<n;i++)
{
if(b[i]%2==0)
{
c[k]=b[i];
k++;
}
else
{ c[j]=b[i];
j--;
}
}
}
void main()
{ clrscr();
int m,n,i,j,a[50], b[50],c[100];
cout<<"\nEnter the limit for Array A?\n"<<endl;
cin>>m;
cout<<"\nEnter the limit for Array B?\n"<<endl;
cin>>n;
cout<<"\nEnter the Values for Array A:\n"<<endl;
for( i=0;i<m;i++)
cin>>a[i];
cout<<"\nEnter the Values for Array B:\n"<<endl;
for(i=0;i<n;i++)
cin>>b[i];
MIX(a,b,c,m,n);
cout<<"Array C values are:";
for(i=0;i<m+n;i++)
cout<<c[i]<<"\t";
getch();
}
output:
-------
Enter the limit for Array A?
3
Enter the limit for Array B?
4
Enter the Values for Array A:
1
2
3
Enter the Values for Array B:
4
5
6
7
Array C values are:2 4 6 7 5 3 1
2. Suppose A[ ], B[ ], C[ ] are integer array of integers of size M, N and M+N
respectively. The numbers in array A and B should be arranged in ascending order
using selection sort. Also write a function MERGE( )to produce the third array C by
merging arrays A and B in descending order using merge sort. Pass the array A, B and
C as arguments in the function.
#include<iostream.h>
#include<conio.h>
void SELECTION_SORT(int a[],int n)
{
int i,j,t,min;
for(i=0;i<=n;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
if( i!=min )
{
t=a[i];
a[i]=a[min];
a[min]=t;
}
while(i>=0)
c[k++]=a[i--];
while(j>=0)
c[k++] = b[j--];
cout<<"\n\nMERGED ARRAY ELEMENTS - DESCENDING ORDER :\n";
for(i=0;i<k;i++)
cout<<c[i]<<endl;
}
void main()
{ clrscr();
int a[20],b[20],c[40],i,m,n,j,t;
cout<<"\n\nENTER SIZE FOR ARRAY A :\t";
cin>>m;
cout<<"\n\nENTER ELEMENTS FOR ARRAY A :\n";
for(i=0;i<m;i++)
cin>>a[i];
cout<<"\n\nENTER SIZE FOR ARRAY B :\t";
cin>>n;
cout<<"\n\nENTER ELEMENTS FOR ARRAY B :\n";
for(i=0;i<n;i++)
cin>>b[i];
SELECTION_SORT(a,m);
SELECTION_SORT(b,n);
MERGE(a,b,c,m,n);
getch();
}
output:
-------
ENTER SIZE FOR ARRAY A : 4
#include<iostream.h>
#include<conio.h>
void BUBBLE_SORT(int a[],int n)
{
int i,j,t;
for(i=0 ; i<n-1 ; i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
void main()
{
clrscr();
int p[100],n,i,t,data;
cout<<"\nEnter the size \t";
cin>>n;
cout<<"\n\nEnter the values:\n";
for(i=0;i<n;i++)
cin>>p[i];
BUBBLE_SORT(p,n);
cout<<"\n\n\t\tSORTED ELEMENTS IN ASCENDING ORDER"<<endl;
cout<<"\t\t-----------------------------------"<<endl;
for(i=0;i<n;i++)
cout<<"\t\t\t\t"<<p[i]<<"\n";
cout<<"\n\nEnter data to be searched:\t";
cin>>data;
t=BINARY_SEARCH(p,n,data);
if(t==0)
cout<<"\nNUMBER IS NOT PRESENT!!!";
else
cout<<"\nNUMBER IS PRESENT!!!";
getch();
}
output:
-------
Enter the size 7
#include<iostream.h>
#include<conio.h>
void Display(int a[10][10],int m,int n)
{
int i,j,sum,k=0;
for(i=0;i<m;i++)
sum=0;
for(j=0;j<n;j++,k++)
if(k%2==0)
cout<<a[i][j]<<" ";
sum=sum+a[i][j];
else
cout<<" ";
cout<<"\tsum:"<<sum<<endl;
}
void main()
{
clrscr();
int a[10][10],m,n,i,j;
cout<<"Enter the size of matrix:";
cin>>m>>n;
cout<<"Enter the values:";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
Display(a,m,n);
getch();
}
output:
-------
Enter the size of matrix:4
5
Enter the values:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 3 5 sum:9
7 9 sum:16
11 13 15 sum:39
5. Write a program using function called FIND_MIN_MAX ( ) that accept a 2D int array and
its size as argument find and print the maximum and minimum of each row and each
column.
#include<iostream.h>
#include<conio.h>
void FIND_MIN_MAX(int a[10][10],int m,int n)
{
int i,j,min,max;
//for each row
for(i=0;i<m;i++)
min=max=a[i][0];
for(j=0;j<n;j++)
If(a[i][j]>max)
max=a[i][j];
else if (a[i][j]<min)
min=a[i][j];
if(a[j][i]>max)
max=a[j][i];
else if (a[j][i]<min)
min=a[j][i];
void main()
{
clrscr();
int a[10][10],m,n,i,j;
cout<<"Enter the size of matrix:";
cin>>m>>n;
cout<<"Enter the values:";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
FIND_MIN_MAX(a,m,n);
getch();
}
output:
-------
Enter the size of matrix:3 3
Enter the values:
10 2 13
41 15 26
7 80 39
Max of 0 st Row:13
Min of 0 st Row:2
Max of 1 st Row:41
Min of 1 st Row:15
Max of 2 st Row:80
Min of 2 st Row:7
Max of 0 st Column:41
Min of 0 st Column:7
Max of 1 st Column:80
Min of 1 st Column:2
Max of 2 st Column:39
Min of 2 st Column:13
6. A library maintains some records with the following record structure: book number, title,
author, no. of copies and price. Write a complete program using structure and do the
following
1. Input N records b. Display all the records
c. Given the author name, display the book no, title, author, no of copies and
price of all books.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
struct BOOK
{
int bno,no,price;
char title[20],author[20];
};
void INPUT(BOOK b[], int n)
{
cout<<"\nEnter details for "<<n<< " records:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"\nEnter the book no : ";
cin>>b[i].bno;
cout<<"\nTitle : ";
gets(b[i].title);
cout<<"\nAuthor : ";
gets(b[i].author);
cout<<"\nNo of copies : ";
cin>>b[i].no;
cout<<"\nPrice : ";
cin>>b[i].price;
}
}
void main()
{
clrscr();
BOOK b[100];
char auth[20];
int n;
cout<<"\nEnter the no of records to be stored: ";
cin>>n;
INPUT(b,n);
DISPLAY(b,n);
cout<<"\n\nEnter the author name to be Searched: ";
gets(auth);
SEARCH(b,auth,n);
getch();
}
Output:
-------
Title : Miles
Author : Brown
No of copies : 3
Price : 500
Title : Joy
Author : John
No of copies : 5
Price : 750
Book No Title Author No of copies Price
------------------------------------------------------------------
RECORD FOUND!!!
#include<iostream.h>
#include<conio.h>
struct complex
{
int real,imag;
};
complex SUM(complex c1,complex c2)
{ complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
void main()
{
clrscr();
complex z1,z2,z3,z4;
cout<<"\n\nEnter real and imaginary part of complex no. 1 : ";
cin>>z1.real>>z1.imag;
cout<<"\n\nEnter real and imaginary part of complex no. 2 : ";
cin>>z2.real>>z2.imag;
z3=SUM(z1,z2);
cout<<"\n\nThe sum of complex no.s : ";
cout<<z3.real<<" "<<z3.imag<<"i";
z4=DIFF(z1,z2);
cout<<"\n\nThe difference of complex no.s : ";
cout<<z4.real<<" "<<z4.imag<<"i";
getch();
}
output:
-------
Enter real and imaginary part of complex no. 1 : 2 5
Enter real and imaginary part of complex no. 2 : -1 -6
The sum of complex no.s : 1 -1i
The difference of complex no.s : 3 11i
8. Write a program using class and object to simulate the salary sheet representation of N
emploees. For each employee the data members are – name, age, basic, hra, da, pf, gsal,
netsal and tax. Calculate hra, da and pf as 15%, 18% and 12% on basic respectively.
Calculate gross salary gsal as basic+hra+da and netsalary as gsal-(pf+tax). Calculate the tax
as follows
Salary Tax
<=10000 2% of gross salary
>10000 & <=50000 5% of gross salary
>50000 10% of gross salary
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
class EmpSal
{
char name[30];
int age,basic;
float HRA,DA,PF;
float gsal,netsal,tax;
public:
void GetValue()
{ cout<<"\nNAME:\t";
gets(name);
cout<<"\nAGE:\t";
cin>>age;
cout<<"\nBASIC SALARY:\t";
cin>>basic;
}
void Compute()
{
HRA=0.15*basic;
DA=0.18*basic;
PF=.12*basic;
gsal=basic+HRA+DA;
if(basic<=10000)
tax=.02*gsal;
else if((basic>10000)&&(basic<=50000))
tax=.05*gsal;
else if(basic>50000)
tax=.1*gsal;
netsal=gsal-(PF+tax);
}
void PrnResult()
{
cout<<name<<" "<<age<<" "<<basic<<" "<<HRA<<" "<<DA<<" "
<<" "<<gsal<<" "<<PF<<" ";
cout<<setprecision(4)<<tax<<" ";
cout<<setprecision(4)<<netsal<<endl;
}
};
void main()
{
clrscr();
int n;
EmpSal s[20];
cout<<"\n\nENTER NUMBER OF EMPLOYEES:\t";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n"<<" EMPLOYEE "<<i+1<<" DETAILS : "<<"\n";
s[i].GetValue();
s[i].Compute();
}
clrscr();
cout<<"\n\t\t\tEMPLOYEE DETAILS\n";
cout<<"\t\t\t----------------\n\n\n";
cout<<"\nNAME AGE BASIC HRA DA GROSS SALARY PF TAX NET
SALARY\n";
cout<<"-------------------------------------------------------\n\n\n";
for(i=0;i<n;i++)
s[i].PrnResult();
getch();
}
output:
-------
ENTER NUMBER OF EMPLOYEES: 2
EMPLOYEE 1 DETAILS :
NAME: Arjun
AGE: 19
EMPLOYEE 2 DETAILS :
NAME: Akash
AGE: 40
#include <iostream.h>
#include<string.h>
#include <conio.h>
class Travel
{
long travelcode;
char place[30];
char season[30];
float total_fare,discount;
public:
Travel()
{
travelcode=0;
strcpy(place,NULL);
strcpy(season,"General");
total_fare=0;
discount=0;
}
void NewTravel()
{
cout<<"Enter travelcode,place,season,totalfare:";
cin>>travelcode>>place>>season>>total_fare;
if(strcmp(season,"Diwali")==0)
discount=(10.0/100.0)*total_fare;
else if(strcmp(season,"Holi")==0)
discount=(5.0/100.0)*total_fare;
else if(strcmp(season,"Christmas")==0)
discount=(15.0/100.0)*total_fare;
else if(strcmp(season,"Summer")==0)
discount=(20.0/100.0)*total_fare;
else
discount=0;
total_fare=total_fare-discount;
void ShowTravel()
cout<<"Travelcode\tPlace\tSeason\tTotalfare\n";
cout<<travelcode<<"\t\t"<<place<<"\t"<<season<<"\t"<<total_fare;
};
void main()
{
clrscr();
Travel t;
t.NewTravel();
t.ShowTravel();
getch();
}
output:
--------
Enter travelcode,place,season,totalfare:
101
Chennai
Diwali
3500
Travelcode Place Season Totalfare
101. Chennai Diwali 3150
10. Write definitions for two versions of an overloaded function. First version sum( ) takes an
argument as size and integer array and the function should print the sum of all the elements.
Second version sum() takes three arguments as int array, array size and a character which has
the default value as ‘O’. If the passed character is ‘E’ , it prints the sum of even elements
otherwise prints the sum of odd elements.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void sum(int n,int a[])
{
long int s=0;
for(int i=0;i<n;i++)
s+=a[i];
cout<<"sum of all the elements:"<<s<<endl;
}
sum(n,a);
sum(a,n,'E');
sum(a,n);
getch();
}
Output
Enter the number of elements : 5
Enter 5 numbers:
1 2 3 4 5
Sum of all the elements : 15
Sum of even numbers: 6
Sum of odd numbers: 9