Вы находитесь на странице: 1из 23

Q.

1 Write a program to print the ch=getche( );


following pattern: }
A cout<<”\n The inputted String is :\n”;
ABC for( i=0;i<len;i++)
ABCDE cout<<a[i];
ABCDEFG cout<<”\n\n***************** OUTPUT
ABCDEFGHI ***************\n”;
ABCDEFGHIJK cout<<”Total characters inputted : “<<len;
ABCDEFGHIJKLM cout<<”\nCharacter Frequency\n\n”;
ABCDEFGHIJKLMNO for( i=0 ;i<len; i++ )
ABCDEFGHIJKLMNOPQ {
Soln. occurred=0;
#include<conio.h> count=1;
#include<iostream.h> //check in the array wether the character has
void main( ) //already been counted from the current //position to
{ the first in reverse direction
int n, i , j , space; for(j=i-1 ; j>=0; j--)
char ch; {
clrscr( ) ; if(a[j]= =a[i] )
cout<<”Enter the value of n<=13 :\n\n”; {
cin>>n; occurred =1; //set occurred =1 if
cout<<”\n\n Desired pattern is ….\n\n”; //character is found
for(i=1;i<=n;i++) break;
{ }
for(space=1;space<=n-i ; space++) }
cout<<’ ’; if(! occurred ) //if the character is not found
ch=’A’; //earlier
for(j=1;j<=2*i-1;j++) {
{ total++;
cout<<ch; //count the characters in forward direction
ch++; for( j=i+1 ;j <len ; j+ +)
} {
cout<<endl; if ( a[j]= = a[i])
} {
getch( ); count++;
} total++;
}
Q.2. WAP to calculate frequency of all }
characters in a string . cout<<” “<<a[i]<<” “<<count<<”\n”;
Soln. }
if(total= =len )//if all characters have been
#include<conio.h> //counted
#include<iostream.h> break;
void main( ) }
{ getch( );
char a[80] , ch; }
int i, j , count ,occurred ,len=0, total=0;
output :
clrscr( ) ; Enter the stream of characters terminated by
cout<<”\nEnter the stream of characters terminated “ENTER” key :
by \”ENTER\”key :\n”; Computer science C++
ch=getche( ); Inputted stream of characters is
while( ch!=’\r’) Computer science C++
{ ***********OUTPUT***************
a[len++]=ch; Total characters inputted :20
Character Frequency
C 2 Q.4. WAP for matrix multiplication using
o 1 Functions .
m 1 Soln :
p 1 //Multiplication of two matrices
u 1
t 1 #include<conio.h>
e 3 #include<iostream.h>
r 1 #define SIZE 5
2 //definition for function enter( )
void enter(int x[SIZE][SIZE] ,int row , int colm)
S 1 {
c 2 for( int i = 0 ;i < row ; i++ )
i 1 for( int j = 0 ; j < colm ; j++ )
n 1 cin>>x[i][j];
+ 2 }
Q.3. Write a function named SUMFUN( ) ,with //definition of function display( )
arguments x and N , which returns the sum of the void display(int x[SIZE][SIZE] ,int row , int colm)
following series : {
x-x3/3+x5/5-x7/7+x9/9+……. int i , j ;
Write the main( ) function to access this function . for( int i = 0 ;i < row ; i++ )
Soln : {
for( int j = 0 ; j < colm ; j++ )
#include<conio.h> cout<<x[i][j]<<”\t”;
#include<iostream.h> cout<<endl;
#include<math.h> }
float SUMFUN(float x,int N) }
{
float sum =0.0,sign = 1,term; //definition for function mat_multiplication
int I;
for( i = 1 ; I <=2*N ; I + = 2) void mat_multiplication(int x[SIZE][SIZE] , int
{ y[SIZE][SIZE] , int z[SIZE][SIZE] ,int row1, int
term = sign * pow(x , i) / I ; colm1 , int colm2 )
sum + = term ; {
sign *= -1; int i , j , k;
} for( int i = 0 ;i < row1 ; i++ )
return sum ; {
} for( int j = 0 ; j < colm1 ; j++ )
{
void main( ) z[i][j]=0;
{ for( int k = 0 ; k < colm2 ; k++ )
clrscr( ); z[i][j] + = x[i][k] *y[k][j];
float result, x; }
int N; }
cout<<”Enter x and N \n ”; }
cin>>x>>N; void main( )
result = SUMFUN( x, N); {
cout<<”\n Result = “<<result<<endl; int a[SIZE][SIZE] , b[SIZE][SIZE]
getch( ); , c[SIZE][SIZE] , row1 , colm1 , row2 ,
colm2 ;
} clrscr( );
Output : cout<<”\n Enter the order of first Matrix <=
Enter x and N “<<SIZE<<”*”<<SIZE<<”\n”;
1 3 cin>>row1>>colm1;
Result = 0.866667 cout<<”\n Enter the order of Second Matrix
<= “<<SIZE<<”*”<<SIZE<<”\n”;
cin>>row2>>colm2; Q.5. Write a program to convert a 2-digit
if(colm1= = row2) octal number to its binary equivalent .
{ Soln :
cout<<”\mEnter the first Matrix of order “ << row1 #include<conio.h>
<< ” * ” << colm1 << endl; #include<iostream.h>
enter( a , row1,colm1); #include<math.h>
cout<<”\mEnter the second Matrix of order “ << void oct_to_bin( int octnum )
row2 << ” * ” << colm2 << endl; {
enter( b , row2,colm2); unsigned long bin_num[6];
clrscr( ); int i = 0 , j , dec_num=0 , p = 0 , r ;
cout<<”\n\n First Matrix is ….. \n”; // p for power and r for remainder
display( a,row1, colm1); //convert from octal to decimal first
cout<<”\n\n Second Matrix is ….. \n”; while( octnum)
display( b,row2, colm2); {
mat_multiplication(a , b , c , r = octnum % 10;
row1,colm1,colm2 ); //function call dec_num = dec_num + ( r * pow(8 ,p ) );
cout<<”\n\n Multiplication of two Matrices is \n\n p++;
”; octnum / = 10;
display( c,row1, colm2); }
} cout<<”\n Decimal equivalent is : ” << dec_num <<
else endl;
{ //now convert from decimal to binary
cout<<”\n\n Matrix Multiplication is not do
possible \n\n “ {
return; r=dec_num % 2;
} bin_num[i++] =r;
getch( ); dec_num / = 2 ;
} }while(dec_num);
Output : cout<<”\n Binary equivalent is : ”;
Enter the order of the first matrix<=5*5 for(j = i - 1 ; j > = 0 ; j--)
3 3 cout<<bin_num[j];
Enter the order of the first matrix<=5*5 }
3 3 void main( )
Enter the first matrix of order 3 * 3 {
5 2 8 int octnum ;
1 4 0 clrscr( );
7 1 9 cout<< “\n Enter any 2 digit octal number”;
Enter the second matrix of order 3 * 3 cin>>octnum;
6 9 3 oct_to_bin(octnum);//function call
8 2 6 getch( );
5 7 1 }
First matrix is
5 2 8 Output :
1 4 0 Enter any 2 digit octal number
7 1 9 77
Second matrix is Decimal equivalent is : 63
6 9 3 Binary equivalent is : 111111
8 2 6
5 7 1
Multiplication of two matrices is

86 105 35
38 17 27
95 128 36
Q . 6. Write a C++ program to calculate the area of break;
circle ,rectangle or triangle depending upon the case 3 : cout<<”\n Enter the base and
user’s choice using structures . height of right angled triangle :”;
Soln : cin>>tri.base>>tri.height;
tri.area = tri.base* tri.height/2.0;
#include<conio.h> cout<<”\n Area of triangle is : ”
#include<iostream.h> << tri.area << “sq. units\n”;
#include<math.h> break;
struct circle }
{ getch( );
float radius ; }
float area;
}circ; OUTPUT :
1. Calculate the area of circle
struct rectangle 2. Calculate the area of rectangle
{ 3. Calculate the area of right angled triangle
float length; Enter the choice :1
float breadth; Enter the radius :5
float area; Area of circle is :78.5 sq. units
}rect;
Q. 7. Write a C++ program to evaluate students
struct triangle performance using Structure :
{ struct student { int roll_no , marks[5] ;
float base; char name[30];
float height ; };
float area;
}tri; Soln :

void main( ) #include<iostream.h>


{ #include<conio.h>
int choice; #include<stdio.h>
do struct student
{ {
clrscr( ); int roll_no;
cout<<”\n1. Calculate the area of circle” ; char name[30];
cout<<”\n2. Calculate the area of rectangle ” ; int marks[5];
cout<<”\n3. Calculate the area of right };
angled triangle ” ; void main( )
cout<<”\n\n Enter your choice” ; {
cin>>choice; student st;
}while((choice<1) || (choice>3)); char grade;
switch(choice) float avg , total = 0.0;
{ clrscr( );
case 1 : cout<<”\n Enter the radius :”; cout<<”\n Enter the student’s record : \n”;
cin>>circ.radius; cout<<”\n Roll number :”;
circ.area = 3.14 * pow(circ.radius , 2 ); cin>>st.roll_no;
cout<<”\n Area of circle is : cout<<”\n Enter Name :”;
” <<circ.area << “sq. units\n”; gets(st.name);
break; cout<<”\n Enter students marks :\n”;
case 2 : cout<<”\n Enter the length and for( int j = 0 ; j < 5 ; j++ )
breadth of rectangle :”; {
cin>>rect.length>>rect.breadth; cout<<”\n Subject “<<j+1<<”: ”;
rect.area = rect.length* rect.breadth; cin>>st.marks[j];
cout<<”\n Area of rectangle is : ” total + = st.marks[i];
<< rect . area << “sq. units\n”; }
avg = total/5.0; if(((a+b)>c)&&((b+c)>a)&&((c+a)>b))
if(avg > 80) {
grade = ‘A’; s = (a+b+c) / 2.0 ;
else ar = sqrt ( ( s*(s-a)*(s-b)*(s-c ));
{ cout<<”\n Area = “<< ar <<”sq. units\n\n”;
if(avg > = 60 && avg < 80 ) }
grade = ‘B’; else
else cout<<”\n Area of triangle is not possible\n\n”;
{ }
if(avg > = 40 && avg < 60 )
grade = ‘C’; void area( float r )
else {
grade = ‘D’; float ar;
} ar = PI * r * r ;
} cout<<”\n Area = “<< ar <<”sq. units\n\n”;
clrscr( ); }
cout<<”\n Students performance evaluation is : \n”; void area(int side)
cout<<”\n\n Name : ”<<st.name; {
cout<<”\n\n Roll Number : ”<<st.roll_no; float ar;
cout<<”\n\n Total Marks : ”<<total; ar = side * side ;
cout<<”\n\n Average : ”<<avg; cout<<”\n Area = “<< ar <<”sq. units\n\n”;
cout<<”\n\n Grade : ”<<grade; }
getch( ); void main( )
} {
float a, b, c, radius ;
OUTPUT : int side ,choice;
do
Enter the student’s record : {
Roll Number : 12555 clrscr( );
Name : Rohan Dixit cout<<”\n1. Calculate the area of Triangle” ;
Enter students marks : cout<<”\n2. Calculate the area of Circle ” ;
Subject 1 : 100 cout<<”\n3. Calculate the area of Square ” ;
Subject 2 : 98 cout<<”\n\n Enter your choice” ;
Subject 3 : 97 cin>>choice;
Subject 4 : 100 }while((choice<1) || (choice>3));
Subject 5 : 95 switch(choice)
Students performance evaluation is : \n”; {
Name : Rohan Dixit case 1 : cout<<”\n\n Enter the three sides :\n”;
Roll Number : 12555 cin>>a>>b>>c;
Total Marks :490 area(a, b, c);
Average : 98 break;
Grade : A case 2 : cout<<”\n\n Enter the radius :\n ”;
cin>>radius;
Q. 8. Write a program in C++ using the concept of area(radius);
function overloading to implement polymorphism break;
To calculate area of Triangle,Circle and Square . case 3 : cout<<”\n\n Enter the side of square :\n”;
Soln : cin>>side;
area(side);
#include<iostream.h> break;
#include<conio.h> }
#include<math.h> getch( );
#define PI 3.14159 }
void area(float a, float b, float c )
{ OUTPUT :
float s, ar; 1. Calculate the area of Triangle
2. Calculate the area of Circle }
3. Calculate the area of Square str[i] = ’\0’;
Enter the choice :1 }
Enter the three sides : void string : :display( )
3 4 5 {
Area = 6 sq. units clrscr( );
cout<<”Entered string is \n\n”;
Q.9. Write a C++ program to perform various i=0;
operations on string class without using language while(str[i] != ’\0’ )
supported built-in string functions . The operations {
on class are : cout<<str[i];
(a) Read a String (b) Display a string (c) Reverse i++;
a string (d) Copy a string into a empty string }
(e) Concatenate two strings }
Soln : void string : : reverse( )
#include<iostream.h> {
#include<conio.h> clrscr( );
#include<stdio.h> int mid ,len =0;
#include<process.h> while(str[len] != ‘\0’)
#define S 80 {
class string len + +;
{ }
private : mid = len/2;
char str[S] , ch; for( i = 0 ; i < mid ; i ++)
int i ; {
public : ch = str[len -1 – i ];
void init( ); str[len -1 – i ] = str[i];
void readstr( ); str[i] = ch;
void display( ); }
void reverse( ); cout<<”\n Reversed string is \n\n”;
void copystr( ); cout<<str;
void concatenate( ); }
}; void string : :copystr( )
void string ::init( ) {
{ char empty_str[S];
i=0; clrscr( );
while(i < S - 1) i=0;
{ while(str[i] = ‘\0’)
str[i] = ‘ ‘ ; {
i ++; empty_str[i] = str[i];
} i ++ ;
str[i] = ’\0’; }
} empty_str[i] = ‘\0’;
void string : : readstr( ) cout<<”\n\n Empty string after copy is \n \n ” ;
{ cout<<empty_str;
clrscr( ); }
cout<<”Enter a string of length <” << S << void string : : concatenate( )
endl<<endl; {
i = 0; char str1[S] , str2[S] , concatenate_str[S];
ch = getchar( ); int len1 , len2;
while(ch!=’\n’) len1 = len2 = 0;
{ clrscr( );
str[i] = ch ; cout<<”Enter the first string of length <” <<
i ++; S <<endl<<endl;
ch = getchar( ); i = 0;
ch = getchar( ); case 2 : obj.display( );
while(ch != ‘\n’) break;
{ case 3 : obj.reverse( );
str1[i] = ch; break;
i ++ ; case 4 : obj.copystr( );
ch = getchar( ); break;
} case 5 : obj.concatenate( );
str1[i] = ‘\0’; break;
cout<<”Enter the second string of length < ”<< case 6 : exit(0);
S <<endl<<endl; default :
i = 0; cout<<”\n Wrong choice \n”;
ch = getchar( ); getch( );
while(ch != ‘\n’) }
{ cout<<\n\n Press any key to continue\n”;
str2[i] = ch; getch( );
i ++ ; }
ch = getchar( ); }
} OUTPUT:
str2[i] = ‘\0’;
//finding string length 1. Read a string
for( i = 0 ; str1[i] != ’\0’ ; i ++) 2. Display the string
len1++; 3. Reverse the string
for( i = 0 ; str2[i] != ’\0’ ; i ++) 4. Copy the string into an empty string
len2++; 5. Concatenate two string
for( i = 0 ; i < len1 ; i ++) 6. Exit
concat_str[i] = str1[i]; Enter your choice(1-6) : 1
for( i = 0 ; i < len2 ; i ++) Enter a string of length < 80
concat_str[len1 + i] = str2[i]; RAMA O RAMA
concat_str[len1 + len2] = ‘\0’; Press any key to continue
cout<<”\n\n Without using string 1. Read a string
functions\n\n”; 2. Display the string
cout<<”\n Concatenated string is :\n\n”; 3. Reverse the string
cout<<concat_str; 4. Copy the string into an empty string
} 5. Concatenate two string
void main( ) 6. Exit
{ Enter your choice(1-6) :2
string obj; Entered string is
obj.init( ); RAMA O RAMA
int choice; Press any key to continue
while( 1 ) 1. Read a string
{ 2. Display the string
clrscr( ); 3. Reverse the string
cout<<”\n1. Read a string\n”; 4. Copy the string into an empty string
cout<<”\n2. Display the string\n”; 5. Concatenate two string
cout<<”\n3. Reverse the string\n”; 6. Exit
cout<<”\n4. Copy the string into an Enter your choice(1-6) :3
empty string \ n ” ; AMAR O AMAR
cout<<”\n5. Concatenate two string \n”; Q .10. Write a C ++ program using classes and
cout<<”\n6. Exit\n”; concept of friend function for multiplication of two
cout<<”\n Enter your choice(1-6) :”; matrices .
cin>>choice; Soln :
switch(choice) #include<iostream.h>
{ #include<conio.h>
case 1 : obj.readstr( ); #include<process.h>
break; #include<iomanip.h>
#define SIZE 5 z.mat[i][j] + = x.mat[i][k] * y.mat[k][j];
class matrix }
{ }
private : }
int i , j , colm , row, mat[SIZE] [SIZE] ; }
public : return(z);
void enter( ); }
void display( ); void main( )
friend matrix mat_multiplication(matrix x {
, matrix y) matrix a ,b ,c;
}; clrscr( );
void matrix : : enter( ) a.enter( );
{ b.enter( );
c = mat_multiplication(a , b);
cout<<”\n Enter the order of Matrix <= clrscr( );
“<<SIZE<<”*”<<SIZE<<”\n”; cout<<”*****First matrix is *****\n\n”;
cin>>row>>colm; a.display( );
cout<<”\mEnter the Matrix of order “ << row << ”
* ” << colm << endl; cout<<”*****Second matrix is *****\n\n”;
for( int i = 0 ;i < row ; i++ ) b.display( );
for( int j = 0 ; j < colm ; j++ ) cout<<”*****Resultant matrix is *****\n\n”;
cin>>mat[i][j]; c.display( );
} }
void matrix : : display( )
{ OUTPUT:
for( int i = 0 ;i < row ; i++ ) ENTER THE ORDER OF MATRIX <=5*5
{ 2 2
for( int j = 0 ; j < colm ; j++ ) Enter matrix of order 2 * 2
cout<<setw(8)<<mat[i][j]; 1 3
cout<<”\n”; 2 4
} ENTER THE ORDER OF MATRIX <=5*5
} 2 2
Enter matrix of order 2 * 2
matrix mat_multiplication(matrix x , matrix y) 6 4
9 7
{ *****First matrix is *****
matrix z;
int i , j , k ; 1 3
if(x.colm != y.row) 2 4
{ *****Second matrix is *****
cout<<”\n matrix multiplication is not
possible\n\n”; 6 4
exit(0); 9 7
}
else *****Resultant matrix is *****
{
z.row = x.row; 33 25
z.colm = y.colm; 48 36
for( int i = 0 ;i <x.row ; i++ )
{ Q. 11. Write a Program using concept of
for( int j = 0 ; j < y.colm ; j++ ) constructor overloading to calculate area of circle,
{ rectangle and triangle .
z.mat[i]j] = 0; Soln.
for( int k = 0 ;k < x.colm ; k++ ) #include<iostream.h>
{ #include<conio.h>
#include<math.h> triangle.area( );
#include<string.h> getch( );
class figure }
{ OUTPUT:
private : Area of circle is :314 sq units
float radius , side1 , side2 , side3; Area of rectangle is :309 sq units
char shape[10]; Area of triangle is : 6 sq units
public :
figure(float r) Q.12. Write a program to generate a series of
{ fibonacci using a copy constructor where the copy
radius = r; constructor is defined outside the class declaration .
strcpy(shape ,”circle”); Soln.
} #include<conio.h>
figure(float s1 , float s2) #include<iostream.h>
{ class fibonacci
side1 = s1; {
side2 = s2; private :
side3 = radius = 0.0; unsigned long int f0 , f1 , f2 ;
strcpy(shape , “rectangle”); public :
} fibonacci( );
figure(float s1 , float s2 , float s3) fibonacci(fibonacci &ptr);
{ void increment( );
side1 = s1; void display( );
side2 = s2; };
side3 = s3; fibonacci : : fibonacci( )
radius = 0.0; {
strcpy(shape , “triangle”); f0 = 0;
} f1 = 1;
void area( ) f2 = f0 + f1;
{ }
float ar , s; fibonacci : :fibonacci(fibonacci &ptr)
if(radius = = 0.0; {
{ f0 = ptr.f0;
if(side3 = = 0.0) f1 = ptr.f1;
ar = side1 * side2; f2 = ptr.f2;
else }
{ void fibonacci : :increment( )
s = (side1 + side2 + side3 )/2.0; {
ar = sqrt(s * (s - side1) * (s – side2) * (s – side3)); f0 = f1;
} f1 = f2;
} f2 = f0 + f1;
else }
ar = 3.14 * radius * radius ; void fibonacci : :display( )
cout<<”\n\n Area of the ”<<shape<<” is : ” << ar {
<< ” sq. units \n” ; cout<<f2<<’\t’;
} }
}; void main( )
void main( ) {
{ clrscr( );
clrscr( ); fibonacci number ;
figure circle(10.0); for( int j = 0 ; j < = 15; j++)
figure rectangle(15.0 , 20.6); {
figure triangle(3.0 , 4.0 , 5.0 ); number.display( );
circle.area( ); number.increment( );
rectangle.area( ); }
getch( ); cin>>amount;
} balance = balance + amount;
OUTPUT : }
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 void account : :withdraw( )
{
Q.13. Write a program to simulate a simple float amount ;
banking system in which the initial balance and the cout<<” How much to withdraw :”;
rate of interest are read from the key board and cin>>amount;
these values are initialized using the constructor if(amount < = balance)
member function . The destructor member function {
is defined in this program to destroy the class balance = balance – amount ;
objects created using the constructor member cout<< “ amount drawn = :” <<
function .The program consists of the following amount <<endl;
methods : cout<<”current balance = :”<<
1) To initialize the balance and the rate of interest balance<<endl;
using the constructor member function }
2) To make a deposit else
3) To withdraw an amount from the balance cout<<0;
4) To find the compound interest based on the rate }
of interest void account : :compound( )
5) To know the balance amount {
6) To display the menu options float interest ;
7) To destroy the object of the class , the destructor interest = balance * rate;
member function balance = balance + interest;
Soln. cout<<”interest amount = ” <<
#include<conio.h> interest<<endl;
#include<iostream.h> cout<<”total amount = ”<<balance<<endl;
#include<stdio.h> }
class account void account : : getbalance( )
{ {
float balance , rate; cout<<”Current balance = : ”;
public : cout<<balance<<endl;
account( ); }
~account( ); void account : :menu( )
void deposit ( ); {
void withdraw( ); cout<<”d -> deposit “<<endl;
void compount( ); cout<<”w -> withdraw “<<endl;
void getbalance( ); cout<<”c -> compound interest “<<endl;
void menu( ); cout<<”g -> get balance “<<endl;
}; cout<<”q -> quit “<<endl;
account : :account( ) cout<<”option please ? :\n“;
{ }
cout<<”enter the initial balance\n”; void main( )
cin>>balance; {
cout<<” Enter the interest rate \n”; clrscr( );
cin>>rate; account acct;
} char ch;
account : :~account( ) acct.menu( );
{ while( ( ch = getchar( ) ) !=’q’)
cout<<”The DataBase has been deleted ”; {
} switch( ch)
void account : :deposit( ) {
{ case ‘d’ :
float amount; acct.deposit( );
cout<<”Enter the amount : ”; break;
case ‘w’ : };
acct.withdraw( ); void base : :enter( )
break; {
cout<<”\n Enter the values of x and y”;
case ‘c’ : cin>>x>>y;
acct.compound( ); }
break; int base : : return_x( )
case ‘g’ : {
acct.getbalance( ); return x;
break; }
} void base : :disp_x( )
} {
} cout<<”\n\n x = “<<x<<endl;
OUTPUT: }
Enter the initial balance class derived : public base
1000 {
interest rate private :
0.2 int z;
d -> deposit public :
w -> withdraw void add( );
c-> compound interest void display( );
g -> get balance };
q -> quit void derived : : add( )
option ,please {
g z = y + return_x( );
current balance = : 1000 }
d void derived : :display( )
enter the amount :100 {
current balance : 1100 cout<<”\n\n x = ”<<return_x( );
w cout<<”\n\n y = ”<<y<<endl;
how much to withdraw ? cout<<”\n\n z = ”<<z<<endl;
amount drawn= : 200 }
current balance =: 900 void main( )
g {
current balance = : 900 derived obj;
c clrscr( );
interest amount = 180 obj.enter( );
total amount 1080 obj.add( );
q obj.disp_x( );
DataBase has been deleted obj.display( );
obj.y = 50;
Q. 14. IIlustrating single level inheritance using obj.add( );
public derivation . obj.display( );
Soln. getch( );
#include<conio.h> }
#include<iostream.h> OUTPUT :
class base Enter the values of x and y : 10 20
{ x=10
private : x=10
int x; y=20
public : z=30
int y ; x=10
void enter( ); y=50
int return_x; z=60
void disp_x( )
Q. 15. IIlustration of multilevel inheritance . void main( )
Soln. {
#ic\nclude<conio.h> book obj1;
#include<iostream.h> tape obj2;
#include<stdio.h> clrscr( )
const LEN = 41; obj1.getdata( );
class publication obj1.putdata( );
{ obj2.getdata( );
protected : obj2.putdata( );
char title[LEN]; }
float price;
}; OUTPUT:
class book : : protected publication Enter the values of x and y : 10 20
{ x = 10
private : y = 20
int pages; z = 30
public : Enter the values of x and y : 50 60
void getdata(void) x = 50
{ y = 60
cout<<”\n Enter the title :”; z = 110
gets(title); Q.16. IIlustration of static class members(data
cout<<”\n Enter the price :”; members and member functions)
cin>>price; #include<iostream.h>
cout<<”\n Enter the no.of pages : ” ; #include<conio.h>
cin>>pages; class student
} {
void putdata(void) private :
{ static int count;
cout<<”\nTitle : ”<<title; int rollno , marks ;
cout<<”\nPrice : ”<<price; public :
cout<<”\nPages: ”<<pages; void enter(int r int m)
} {
}; rollno = r ;
class tape : : protected publication marks = m ;
{ count + +;
private : }
int play_time; void show(void)
public : {
void getdata(viod) cout<<”\n Roll number :” << rollno
{ << ”\t”;
cout<<”\n Enter the title :”; cout<<”\n Marks :”<< marks<<” \t ” ;
gets(title); }
cout<<”\n Enter the price :”; static void showcount(void)
cin>>price; {
cout<<”\n Enter the play time in cout<<”count = “<<count<<”\n”;
minutes :”; }
cin>>play_time; };
} int static : :count = 0;
void putdata(void) void main( )
{ {
cout<<”\nTitle : ”<<title; clrscr( );
cout<<”\nPrice : ”<<price; student st1, st2 , st3 ;
cout<<”\nPlay_time:”<<play_time<<”minutes \n ”; st1.enter(1001 , 99);
} student : : showcount( );
}; st2.enter(1002 , 88);
student : : showcount( ); return -1;
st3.enter(1003 , 100); else
student : : showcount( ); return i;
st1.show( ); }
st2.show( ); void main( )
st3.show( ); {
getch( ); array a;
} clrscr( );
OUTPUT: a.add(11);
count = 1 a.add(2);
count = 2 a.add(9);
count = 3 a.add(13);
Roll number : 1001 Marks : 99 a.add(57);
Roll number : 1002 Marks : 88 a.add(25);
Roll number : 1003 Marks : 100 a.add(17);
a.add(1);
Q.17. Write a C++ program for linear search in a.add(90);
arrays . a.add(3);
Soln. int num;
#include<conio.h> cout<<”\n Enter the number to be searched :”;
#include<iostream.h> cin>>num;
const int MAX = 10; int r = a.lsearch(num);
class array if( r = = -1)
{ cout<<”\n Number is not present in the array.”;
private : else
int arr[max] , count; cout<<”\n The number is at position ”<<r
public : <<” in the array.”;
array( ); getch( );
void add( int item); }
int lsearch(int item); OUTPUT:
}; Enter the number to search :57
array : :array( ) The number is at position 4 in the array.
{ Q.18. Write a C++ program for Binary search in
count = 0; arrays .
for( int i = 0 ; i < MAX ; i + +) Soln.
arr[i] = 0; #include<conio.h>
} #include<iostream.h>
void array : : add(int item) const int MAX = 10;
{ class array
if(count < MAX) {
{ private :
arr[count] = item ; int arr[max] , count;
count + +; public :
} array( );
else void add( int item);
cout<<”\n Array is full ”<<endl; void bsearch(int item);
} };
int array : : lsearch(int item) array : :array( )
{ {
for( int i = 0 ; i < count ; i + +) count = 0;
{ for( int i = 0 ; i < MAX ; i + +)
if(arr[i]= =item) arr[i] = 0;
break; }
} void array : : add(int item)
if(i = = count) {
if(count < MAX) Q.19. Write a C++ program for sorting an array
{ using Bubble sort .
arr[count] = item ; Soln.
count + +; #include<conio.h>
} #include<iostream.h>
else const int MAX = 10;
cout<<”\n Array is full ”<<endl; class array
} {
void array : : bsearch(int item) private :
{ int arr[max] , count;
int mid , lower = 0 , upper = count -1 , flag = 1; public :
for(mid = (lower +upper )/2 ; lower<=upper; array( );
mid = (lower +upper )/2 ) void add( int item);
{ void bsort( );
if(arr[mid = =item) void display( );
{ };
cout<<”\n The number is at position ” <<mid array : :array( )
<<” in the array.”; {
flag = 0; count = 0;
break; for( int i = 0 ; i < MAX ; i + +)
} arr[i] = 0;
if(arr[mid] >item) }
upper = mid – 1; void array : : add(int item)
else {
lower = mid – 1; if(count < MAX)
} {
if(flag) arr[count] = item ;
cout<<”\n Element is not present in the array.”; count + +;
} }
void main( ) else
{ cout<<”\n Array is full ”<<endl;
array a; }
clrscr( ); void array : :bsort( )
a.add(1); { int tmp;
a.add(2); for( int i = 0 ; i <= count – 2 ; i + +)
a.add(3); {
a.add(9); for( int j = 0 ; j <= count – 2 - i ; j + +)
a.add(11); {
a.add(13); if(arr[j]>arr[j+1])
a.add(17); {
a.add(57); tmp = arr[j];
a.add(90); arr[j] = arr[j+1];
a.add(90); arr[j+1] = tmp;
int num; }
cout<<”\n Enter the number to be searched :”; }
cin>>num; }
a.bsearch(num); }
getch( ); void array: :display( )
} {
for( int i = 0 ; i < count ; i + +)
OUTPUT: cout<<arr[i]<<”\t”;
Enter the number to search :57 cout<<endl;
The number is at position 8 in the array. }

void main( )
{ void array : :Ssort( )
array a; { int tmp;
clrscr( ); for( int i = 0 ; i <= count – 2 ; i + +)
a.add(25); {
a.add(17); for( int j = i +1 ; j <= count – 1 ; j + +)
a.add(31); {
a.add(13); if(arr[i]>arr[j])
a.add(2); {
cout<<”\n Bubble sort.\n ”; tmp = arr[i];
cout<<”\n Array before sorting :”<<endl; arr[i] = arr[j];
a.display( ); arr[j] = tmp;
a.sort( ); }
cout<<”\n Array After sorting :”<<endl; }
a.display( ); }
getch( ); }
} void array: :display( )
OUTPUT: {
Bubble sort. for( int i = 0 ; i < count ; i + +)
Array before sorting : cout<<arr[i]<<”\t”;
25 17 31 13 2 cout<<endl;
Array before sorting : }
2 13 17 25 31
void main( )
Q.20. Write a C++ program for sorting an array {
using selection sort . array a;
Soln. clrscr( );
#include<conio.h> a.add(25);
#include<iostream.h> a.add(17);
const int MAX = 10; a.add(31);
class array a.add(13);
{ a.add(2);
private : cout<<”\n Selection sort.\n ”;
int arr[max] , count; cout<<”\n Array before sorting :”<<endl;
public : a.display( );
array( ); a.sort( );
void add( int item); cout<<”\n Array After sorting :”<<endl;
void Ssort( ); a.display( );
void display( ); getch( );
}; }
array : :array( ) OUTPUT:
{ Selection sort.
count = 0; Array before sorting :
for( int i = 0 ; i < MAX ; i + +) 25 17 31 13 2
arr[i] = 0; Array before sorting :
} 2 13 17 25 31
void array : : add(int item)
{ Q.21. Write a C++ program for sorting an array
if(count < MAX) using insertion sort .
{ Soln.
arr[count] = item ; #include<conio.h>
count + +; #include<iostream.h>
} const int MAX = 10;
else class array
cout<<”\n Array is full ”<<endl; {
} private :
int arr[max] , count; a.add(2);
public : cout<<”\n Insertion sort.\n ”;
array( ); cout<<”\n Array before sorting :”<<endl;
void add( int item); a.display( );
void Isort( ); a.sort( );
void display( ); cout<<”\n Array After sorting :”<<endl;
}; a.display( );
array : :array( ) getch( );
{ }
count = 0; OUTPUT:
for( int i = 0 ; i < MAX ; i + +) Insertion sort.
arr[i] = 0; Array before sorting :
} 25 17 31 13 2
void array : : add(int item) Array before sorting :
{ 2 13 17 25 31
if(count < MAX)
{ Q.22. Write a program to perform the following
arr[count] = item ; operations on arrays :
count + +; 1) insertion 2) deletion 3) reverse 4)traversal
} Soln.
else #include<conio.h>
cout<<”\n Array is full ”<<endl; #include<iostream.h>
} const int MAX = 5;
void array : :Isort( ) class array
{ int tmp; {
for( int i = 1 ; i <= count – 1 ; i + +) private :
{ int arr[MAX];
for( int j = 0 ; j < i ; j + +) public :
{ void insert(int pos , int num);
if(arr[j]>arr[i]) void del(int pos);
{ void reverse( );
tmp = arr[j]; void display( );
arr[j] = arr[i]; };
for( int k = i ; k < j ; k- -) void array : :insert( int pos , int num)
arr[k] = arr[k-1]; {
arr[k+1] = tmp; //shift elements to right
} for( int j = MAX – 1 ; j > = pos ; j- - )
} arr[j] = arr[j-1];
} arr[j] = num;
} }
void array: :display( ) void array : :del(int pos)
{ {
for( int i = 0 ; i < count ; i + +) //skip to the desired position
cout<<arr[i]<<”\t”; for(int j = pos ; j < MAX ; j + +)
cout<<endl; arr[j – 1] = arr[j];
} arr[j -1] = 0;
}
void main( ) void array : :reverse( )
{ {
array a; //reverses the entire array
clrscr( ); for(int j = 0 ; j < MAX/2 ; j + +)
a.add(25); {
a.add(17); int tmp = arr[j];
a.add(31); arr[j] = arr[MAX – 1 – j];
a.add(13); arr[MAX – 1 – j] = tmp;
} {
void array : :display( ) size = sz;
{ arr = new int[size];
for(int j = 0 ; j < MAX ; j + +) int n;
cout<<” “<<arr[j]; for( j = 0 ; j<size ; j + + )
} {
void main( ) cout<<”\nEnter the element no. ” << (j+1) <<” ”;
{ cin>>n;
array a ; arr[i] = n;
clrscr( ); }
a.insert(1,11); }
a.insert(2,12); void array : :sort( )
a.insert(3,13); {
a.insert(4,14); int tmp;
a.insert(5,15); for( j = 0 ; j<size ; j + + )
{
cout<<”\n Elements of Array: ”; for( k = j +1 ; k<size ; k + + )
a.display( ); {
if(arr[j] > arr[k])
a.del(5); {
a.del(2); tmp = arr[j];
cout<<”\n\n After Deletion :”; arr[j] = arr[k];
a.display( ); arr[k] = tmp;
}
a.insert(2,222); }
a.insert(5,555); }
cout<<”\n After insertion: ”; }
a.display( ); void array : :display( )
{
a.reverse( ); for( j = 0 ; j<size ; j + + )
cout<<”\n After reversing: ”; cout<<” ”<<arr[j];
a.display( ); }
void array : :merge(array &a , array &b)
getch( ); {
} int i , j , k ;
size = a.size + b.size ;
Q.23. Write a C++ program for merging of arr = new int[size];
two arrays . for(k=0 , j = 0 , i = 0 ; i<=size ; i + + )
Soln. {
#include<conio.h> if(a.arr[k]<b.arr[j])
#include<iostream.h> {
const int MAX1 = 5; arr[i] = a.arr[k];
const int MAX2 = 7; k++;
class array if(k>=a.size)
{ {
private : for(i++ ;j<b.size;j++,i++)
int *arr; arr[i] = b.arr[i];
int size; }
public : }
void create(int sz); else
void sort( ); {
void display( ); arr[i] = b.arr[j];
void merge(array &a , array &b); j++;
}; if(j>=b.size)
void array : : create(int sz) {
for(i++ ; k<=a.size ; k++ , i++) #include<conio.h>
#include<iostream.h>
arr[i] = a.arr[k]; const int MAX = 10;
} class stack
} {
} private :
} int arr[MAX] , top;
void main( ) public :
{ stack( );
clrscr( ); void push( );
array a; int pop( );
cout<<”\nEnter elements of first array :\n”; };
a.create(MAX1); stack : :stack( )
{
array b; top = -1;
cout<<”\nEnter elements of second array : \ n ”; }
b.create(MAX2); void stack: :push( int item)
{
a.sort( ); if(top= =MAX-1)
b.sort( ); {
cout<<”\n First array :\n”; cout<<endl<<”Stack is full”;
a.display( ); return ;
cout<<”\n Second array :\n”; }
b.display( ); top + +;
cout<<”\n\nAfter Merging :\n”; arr[top] = item;
array c; }
c.merge(a , b); int stack : :pop( )
c.display( ); {
getch( ); if(top= = -1)
} {
OUTPUT: cout<<endl<<”Stack is empty”;
Enter elements of first array: return NULL;
Enter element no. 1 67 }
Enter element no. 2 12 int data = arr[top];
Enter element no. 3 -4 top -- ;
Enter element no. 4 43 return data;
Enter element no. 5 2 }
Enter elements of second array: void main( )
Enter element no. 1 8 {
Enter element no. 2 10 stack s;
Enter element no. 3 -2 clrscr( );
Enter element no. 4 39 s.push(11);
Enter element no. 5 6 s.push(23);
Enter element no. 6 7 s.push(-8);
Enter element no. 7 19 s.push(16);
First array : s.push(27);
-4 2 12 43 67 s.push(14);
Second array : s.push(20);
-2 6 7 8 10 19 39 s.push(39);
After Merging : s.push(2);
-4 -2 2 6 7 8 10 12 19 39 43 67 s.push(15);
s.push(7);
Q.24. Write a C++ program to perform push and
pop operations in stack implemented using an array. int i = s.pop( );
Soln. cout<<”\n Item poped :”<<i ;
tmp -> data = item;
int i = s.pop( ); tmp -> link = top;
cout<<”\n Item poped :”<<i ; top = tmp;
}
int i = s.pop( ); int stack : :pop( )
cout<<”\n Item poped :”<<i ; {
if(top = = NULL)
int i = s.pop( ); {
cout<<”\n Item poped :”<<i ; cout<<endl<<”Stack is empty”;
return NULL;
int i = s.pop( ); }
cout<<”\n Item poped :”<<i ; node * tmp;
int item;
getch( ); tmp = top;
} item = tmp ->data;
OUTPUT: top = top - >link;
Stack is full delete tmp;
return item;
Item popped : 15 }
Item popped : 2 stack : : ~stack( )
Item popped : 39 {
Item popped : 20 if(top = =NULL)
Item popped : 14 return;
node *tmp;
Q.25. Write a C++ program to perform push and while(top!=NULL)
pop operations in stack implemented using Linked {
list . tmp = top;
Soln. top = top - >link;
#include<conio.h> delete tmp;
#include<iostream.h> }
class stack }
{ void main( )
private : {
struct node clrscr( );
{ stack s;
int data; s.push(14);
node * link; s.push(-3);
}*top; s.push(18);
public : s.push(29);
stack( ); s.push(31);
void push(int item); s.push(16);
int pop( );
~stack( ); int r = s.pop( );
}; cout<<”\n Item popped : “<<r;
stack : : stack( ) int r = s.pop( );
{ cout<<”\n Item popped : “<<r;
top = NULL; int r = s.pop( );
} cout<<”\n Item popped : “<<r;
void stack : :push(int item) getch( );
{ }
node *tmp; OUTPUT :
tmp = new node; Item popped :16
if(tmp = = NULL) Item popped :31
cout<<endl<<”Stack is full”; Item popped :29
Q.26. Write a C++ program to perform insertion a.addq(23);
and deletion operations in queue implemented using a.addq(9);
an array. a.addq(11);
Soln. a.addq(-10);
#include<conio.h> a.addq(25);
#include<iostream.h> a.addq(16);
const int MAX = 10; a.addq(17);
class queue a.addq(22);
{ a.addq(19);
private : a.addq(30);
int arr[MAX]; a.addq(32);
int front , rear; int r = a.delq( );
public : cout<<”\n Item deleted : ”<<r;
queue( ); int r = a.delq( );
void addq( int item); cout<<”\n Item deleted : ”<<r;
int delq( ); int r = a.delq( );
}; cout<<”\n Item deleted : ”<<r;
queue : :queue( ) getch( );
{ }
front = -1; OUTPUT:
rear = -1; Queue is full
} Item deleted : 23
void queue : : addq(int item) Item deleted : 9
{ Item deleted : 11
if(rear = =MAX – 1) Q.27. Write a C++ program to perform push and
{ pop operations in stack implemented using Linked
cout<<”\n Queue is full”; list .
return; Soln.
} #include<conio.h>
rear + +; #include<iostream.h>
arr[rear] = item; class queue
if(front = = -1) {
front = 0; private :
} struct node
int queue : : delq( ) {
{ int data;
int data; node * link;
if(front = =-1) }*front , *rear;
{ public :
cout<<”\n Queue is Empty”; queue( );
return Null; void addq(int item);
} int delq( );
data = arr[front]; ~queue( );
arr[front] = 0; };
if(front = =rear) queue : : queue( )
front = rear = -1; {
else front = rear =NULL;
front + +; }
void queue : :addq(int item)
return data; {
} node *tmp;
void main( ) tmp = new node;
{ if(tmp = = NULL)
clrscr( ); cout<<endl<<”Queue is full”;
queue a;
tmp -> data = item;
tmp -> link = NULL; getch( );
if(front = = NULL) }
{ OUTPUT :
rear = front = tmp; Item extracted :11
return; Item extracted :-8
} Item extracted :23
rear ->link = tmp;
rear = rear ->link; Q.28. Write a C++ program to perform insertion
} and deletion operations in a circular queue .
int queue : :delq( ) Soln.
{ #include<conio.h>
if(front = = NULL) #include<iostream.h>
{ const int MAX = 10;
cout<<endl<<”Queue is empty”; class queue
return NULL; {
} private :
node * tmp; int arr[MAX];
int item; int front , rear;
item = front ->data; public :
tmp = front; queue( );
front = front - >link; void addq( int item);
delete tmp; int delq( );
return item; void display( );
} };
queue : : ~queue( ) queue : :queue( )
{ {
if(front = =NULL) front = -1;
return; rear = -1;
node *tmp; for( int j = 0; j < MAX ; j+ +)
while(top!=NULL) arr[j]= 0;
{ }
tmp = front; void queue : : addq(int item)
front = front - >link; {
delete tmp; if(rear = =MAX – 1 && front= =0) ||
} (rear + 1 = = front) )
} {
void main( ) cout<<”\n Queue is full”;
{ return;
clrscr( ); }
queue a; if (rear = = MAX-1)
a.addq(11); rear = 0;
a.addq(-8); else
a.addq(23); rear + +;
a.addq(19);
a.addq(15); arr[rear] = item;
a.addq(16); if(front = = -1)
a.addq(28); front = 0;
}
int r = a.delq( ); int queue : : delq( )
cout<<”\n Item extracted : “<<r; {
int r = a.delq( ); int data;
cout<<”\n Item extracted : “<<r; if(front = =-1)
int r = a.delq( ); {
cout<<”\n Item extracted : “<<r; cout<<”\n Queue is Empty”;
return NULL; a.display( );
} getch( );
data = arr[front]; }
arr[front] = 0; OUTPUT:
if(front = =rear) Elements in the circular queue
front = rear = -1; 14 22 13 -6 25 0 0 0 0 0
else Item deleted : 14
{ Item deleted : 22
if(front= =MAX-1) Elements in the circular queue after deletion :
front = 0; 0 0 13 -6 25 0 0 0 0 0
else Elements in the circular queue after addition :
front + +; 0 0 13 -6 25 21 17 18 9 20
} Elements in the circular queue after addition :
return data; 32 0 13 -6 25 21 17 18 9 20
}
void queue : :display( )
{
cout<<endl; Q.29. Write a C++ program to read and write class
for( int j = 0; j < MAX ; j+ +) object in a binary file ?
cout<< arr[j]<<” ”; Soln.
cout<<endl; #include<fstream.h>
} #include<stdio.h>
void main( ) #include<process.h>
{ #include<conio.h>
clrscr( ); class joke
queue a; { private :
a.addq(14); int jokeid;
a.addq(22); char type[5];
a.addq(13); char jokedesc[255];
a.addq(-6); public :
a.addq(25); void newjokeentry( )
cout<<”\n Elements in the circular queue :”; { cout<<”\n Enter joke id :” ;
a.display( ); cin>>jokeid;
int r = a.delq( ); cout<<”\n Enter joke type : ”;
cout<<”\n Item deleted : ”<<r; gets(type);
cout<<”\n Enter joke : ”;
int r = a.delq( ); gets(jokedesc);
cout<<”\n Item deleted : ”<<r; }
cout<<”\n Elements in the circular queue void showjoke( )
after deletion :”; { cout<<”\n joke id :” <<jokeid;
a.display( ); cout<<”\n joke type : ”;
puts(type);
a.addq(21); cout<<”\n joke details : ”;
a.addq(17); puts(jokedesc);
a.addq(18); }
a.addq(9); };
a.addq(20); void main( )
cout<<”\n Elements in the circular queue {
after addition:”; clrscr( );
a.display( ); void addjoke( );//function prototype
void dispfile( ); //function prototype
a.addq(32); clrscr( );
addjoke( );//function call
cout<<”\n Elements in the circular queue dispfile( );//function call
after addition:”; getch( );
} What a wonder fool fellow
void addjoke( ) Do you wish to add more object(y/n) ? :
{ Y
joke obj; Enter joke id
char choice; 2
ofstream ofile; Enter type
ofile.open(“jokes.dat”,ios: :app| ios: :binary); Sad
if(!ofile) Ramlal married Miss world and lives a HAPPY
{ DAILY life
cout<<”\n cannot open jokes.dat for writing\n”; Do you wish to add more object(y/n) ? :
exit(1); n
} Contents of file jokes.dat are :
while(1) 1: Fun
{ What a wonder fool fellow
{ 2 : Sad
clrscr( ); Ramlal married Miss world and lives a HAPPY
cout<<\n Do you wish to add more DAILY life
objects(y/n) ?\n\n”;
cin>>choice;
if( ( choice = =’y’)| | (choice = = ‘Y’))
{
obj.newjokeentry( );
ofile.write((char *)&obj , sizeof(obj);
}
else
break;
}
ofile.close( );
}
void dispfile( )
{
joke obj;
ifstream ifile;
ifile.open(“jokes.dat”,ios: :app| ios: :binary);
if(!ifile)
{
cout<<”\n cannot open jokes.dat for reading\n”;
exit(1);
}
cout<<”\n contents of file jokes.dat are :\n\n”;
ifile.read((char*)&obj,sizeof(obj);
while( !ifile.eof( ))
{
obj.showjoke( );
ifile.read((char*)&obj,sizeof(obj);
}
ifile.close( );
}
OUTPUT:
Do you wish to add more object(y/n) ? :
Y
Enter joke id
1
Enter type
Fun

Вам также может понравиться