You are on page 1of 20

STD XII – COMPUTER SCIENCE

RECORD PROGRAMS (2019- 20)

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;
}

void MERGE(int a[ ], int b[ ],int c[ ],int m,int n)


{
int k=0,i=m-1,j=n-1;
cout<<endl;
while(i>=0 && j>=0)
{
if(a[i]>=b[j])
c[k++]=a[i--];
else
c[k++]=b[j--];
}

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

ENTER ELEMENTS FOR ARRAY A :


3
1
4
6

ENTER SIZE FOR ARRAY B : 3

ENTER ELEMENTS FOR ARRAY B :


7
2
5

MERGED ARRAY ELEMENTS - DESCENDING ORDER :


7
6
5
4
3
2
1
3. Suppose an array P containing N integer values are arranged in ascending order using
bubble sort. Write a user defined function to search for a integer from P with the help of
binary search method. The function should return an integer 0 to show absence of the number
and integer 1 to show presence of the number in the array. The function should have 3
parameters i. an array P ii. The number DATA to be searched iii. Number of elements N

#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;
}
}
}

int BINARY_SEARCH(int p[ ],int n,int data)


{
int beg=0, end=n-1,mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(p[mid]==data)
return (1);
else if(data>p[mid])
beg=mid+1;
else
end=mid-1;
}
return 0;
}

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

Enter the values:


2
1
4
5
3
6
9
SORTED ELEMENTS IN ASCENDING ORDER
-----------------------------------
1 2 3 4 5 6
9

Enter data to be searched: 6


NUMBER IS PRESENT!!!
4. Write a function that accepts a 2D int array in the size of M rows, N columns and print the
alternate numbers in the given format with its row sum.
For example: If the original matrix is
1 2 4 1 3 5 Sum=9
6 7 8 9 10 output should be 7 9 Sum=16
11 12 13 14 15 11 13 15 Sum=39
16 17 18 19 20 17 19 Sum=36

#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];

cout<<"\nMax of "<<i<<"st Row:"<<max<<endl;


cout<<"\nMin of "<<i<<"st Row:"<<min<<endl;

// for each column


for(i=0;i<n;i++)
{
min=max=a[0][i];
for(j=0;j<m;j++)

if(a[j][i]>max)
max=a[j][i];
else if (a[j][i]<min)
min=a[j][i];

cout<<"\nMax of "<<i<<"st Column:"<<max<<endl;


cout<<"\nMin of "<<i<<"st Column:"<<min<<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];
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 DISPLAY(BOOK b[],int n)


{
cout<<"\nBook No"<<"\tTitle"<<"\tAuthor"<<"\t No of copies"<<"\tPrice"<<endl;
cout<<"\n------------------------------------------------------------------"<<endl;
for(int i=0;i<n;i++)
cout<<"\n\n"<<b[i].bno<<"\t"<<b[i].title<<"\t"<<b[i].author<< "\t\t"<<b[i].no<<"\t"
<<b[i].price<<endl;
}
void SEARCH(BOOK b[],char auth[20],int n)
{
int flag=0;
for(int i=0;i<n;i++)
if(strcmp(auth , b[i].author)==0)
{
cout<<"\n\t\tRECORD FOUND!!!\n";
cout<<"\nBook No"<<"\tTitle"<<"\tAuthor"<<"\t No of copies" <<"\tPrice"
<<endl;
cout<<"\n------------------------------------------------------------------"<<endl;
cout<<"\n\n"<<b[i].bno<<"\t"<<b[i].title<<"\t"<<b[i].author<< "\t\t"<<b[i].no <<"\t"
<<b[i].price<<endl;
flag=1;
}
if(flag==0)
cout<<"\n\n\n\t\tRECORD DOES NOT EXIST!!!\n"<<endl;
}

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:
-------

Enter the no of records to be stored: 2

Enter details for 2 records:

Enter the book no : 1

Title : Miles

Author : Brown

No of copies : 3

Price : 500

Enter the book no : 2

Title : Joy

Author : John

No of copies : 5
Price : 750
Book No Title Author No of copies Price
------------------------------------------------------------------

1 Miles Brown 3 500

2 Joy John 5 750

Enter the author name to be Searched: John

RECORD FOUND!!!

Book No Title Author No of copies Price


------------------------------------------------------------------
2 Joy John 5 750
7. WAP to find the sum and difference of two complex numbers using functions SUM( )
and DIFF( ) that accepts two complex numbers as arguments and return the result using
structure.(return type of the function is structure)

#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;
}

complex DIFF(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

BASIC SALARY: 8000

EMPLOYEE 2 DETAILS :

NAME: Akash

AGE: 40

BASIC SALARY: 30000


EMPLOYEE DETAILS
------------------------------

NAME AGE BASIC HRA DA GROSS SALARY PF TAX NET SALARY


--------------------------------------------------------------------------------------------------------
Arjun 19 8000 1200 1440 10640 960 212.8 9467.2002
Akash 40 30000 4500 5400 39900 3600 1995 34305
9. Define a class Travel in C++ with the following descriptions:
Private Members
TravelCode of type long
Place of type character array(string)
Season of type character array(string)
Total_fare of type float
Discount of type float
Public Members:
A constructor to assign initial values to TravelCode as 0, place as “NULL”,
Season as “General” , Total_fare = 0 , Discount = 0.
A function NewTravel() which allows user to enter TravelCode, Place, Season and
Total_fare. Also calculates the Discount as per the following conditions:
Season Discount (%)
Diwali 10
Holi 5
Christmas 15
Summer 20
General 0
Discount given on Total_fare
A function ShowTravel() to display all data members on screen.

#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;
}

void sum(int a[],int n,char ch='O')


{
long int s=0;
if(ch=='E')
{
for(int i=0;i<n;i++)
if(a[i]%2==0)
s+=a[i];
cout<<"sum of even numbers = "<<s<<endl;
}
else
{
for(int i=0;i<n;i++)
if(a[i]%2!=0)
s+=a[i];
cout<<"sum of odd numbers = "<<s<<endl;
}
}
void main()
{
clrscr();
int a[100],n,i;
cout<<"\nEnter the Number of Elements : ";
cin>>n;
cout<<"\nEnter "<<n<<" Numbers : ";
for(i=0;i<n;i++)
cin>>a[i];

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

You might also like