c++

32
Q.1 Write a program to print the following pattern: A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFGHIJK ABCDEFGHIJKLM ABCDEFGHIJKLMNO ABCDEFGHIJKLMNOPQ Sol n . #include<conio.h> #include<iostream.h> void main( ) { int n, i , j , space; char ch; clrscr( ) ; cout<<”Enter the value of n<=13 :\ n\n”; cin>>n; cout<<”\n\n Desired pattern is ….\ n\n”; for(i=1;i<=n;i++) { for(space=1;space<=n-i ; space++) cout<<’ ’; ch=’A’; for(j=1;j<=2*i-1;j++) { cout<<ch; ch++; } cout<<endl; } getch( ); } Q.2. WAP to calculate frequency of all characters in a string . Sol n . #include<conio.h> #include<iostream.h> void main( ) { char a[80] , ch; int i, j , count ,occurred ,len=0, total=0; clrscr( ) ; cout<<”\nEnter the stream of characters terminated by \”ENTER\”key :\n”; ch=getche( ); while( ch!=’\r’) { a[len++]=ch; ch=getche( ); } cout<<”\n The inputted String is :\n”; for( i=0;i<len;i++) cout<<a[i]; cout<<”\n\n***************** OUTPUT ***************\n”; cout<<”Total characters inputted : “<<len; cout<<”\nCharacter Frequency\n\n”; for( i=0 ;i<len; i++ ) { occurred=0; count=1; //check in the array wether the character has //already been counted from the current //position to the first in reverse direction for(j=i-1 ; j>=0; j--) { if(a[j]= =a[i] ) { occurred =1; //set occurred =1 if //character is found break; } }

Upload: gurvinder-singh

Post on 23-Nov-2014

332 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: c++

Q.1 Write a program to print the following pattern:

A ABC ABCDE

ABCDEFG ABCDEFGHI ABCDEFGHIJK ABCDEFGHIJKLM ABCDEFGHIJKLMNO ABCDEFGHIJKLMNOPQSol n . #include<conio.h>#include<iostream.h>void main( ) { int n, i , j , space; char ch; clrscr( ) ; cout<<”Enter the value of n<=13 :\n\n”;cin>>n;cout<<”\n\n Desired pattern is ….\n\n”;for(i=1;i<=n;i++) { for(space=1;space<=n-i ; space++) cout<<’ ’; ch=’A’; for(j=1;j<=2*i-1;j++) {

cout<<ch;ch++;

}cout<<endl;

}getch( ); }

Q.2. WAP to calculate frequency of all characters in a string .Sol n .

#include<conio.h>#include<iostream.h>void main( ) { char a[80] , ch;int i, j , count ,occurred ,len=0, total=0;

clrscr( ) ;cout<<”\nEnter the stream of characters terminated by \”ENTER\”key :\n”;ch=getche( );while( ch!=’\r’)

{ a[len++]=ch;

ch=getche( );}

cout<<”\n The inputted String is :\n”; for( i=0;i<len;i++)

cout<<a[i];cout<<”\n\n***************** OUTPUT ***************\n”;cout<<”Total characters inputted : “<<len;cout<<”\nCharacter Frequency\n\n”;for( i=0 ;i<len; i++ ){ occurred=0;count=1;//check in the array wether the character has //already been counted from the current //position to the first in reverse directionfor(j=i-1 ; j>=0; j--){

if(a[j]= =a[i] ) {

occurred =1; //set occurred =1 if //character is found break; }}if(! occurred ) //if the character is not found //earlier{ total++;//count the characters in forward direction for( j=i+1 ;j <len ; j+ +) { if ( a[j]= = a[i])

{ count++;

total++; } }cout<<” “<<a[i]<<” “<<count<<”\n”;}if(total= =len )//if all characters have been //counted break;}getch( );}

output :Enter the stream of characters terminated by “ENTER” key :Computer science C++Inputted stream of characters is Computer science C++***********OUTPUT***************Total characters inputted :20

Page 2: c++

Character FrequencyC 2o 1m 1p 1u 1t 1e 3r 1

2

S 1c 2i 1n 1+ 2

Q.3. Write a function named SUMFUN( ) ,with arguments x and N , which returns the sum of the following series :x-x3/3+x5/5-x7/7+x9/9+…….Write the main( ) function to access this function .Sol n :

#include<conio.h>#include<iostream.h>#include<math.h>float SUMFUN(float x,int N){

float sum =0.0,sign = 1,term;int I;for( i = 1 ; I <=2*N ; I + = 2){ term = sign * pow(x , i) / I ;sum + = term ;sign *= -1;}return sum ;}

void main( ) {

clrscr( );float result, x;int N;cout<<”Enter x and N \n ”;cin>>x>>N;result = SUMFUN( x, N);

cout<<”\n Result = “<<result<<endl;getch( );

}Output :Enter x and N 1 3Result = 0.866667

Q.4. WAP for matrix multiplication using Functions .Soln ://Multiplication of two matrices

#include<conio.h>#include<iostream.h>#define SIZE 5//definition for function enter( )void enter(int x[SIZE][SIZE] ,int row , int colm){for( int i = 0 ;i < row ; i++ ) for( int j = 0 ; j < colm ; j++ )

cin>>x[i][j];}//definition of function display( )void display(int x[SIZE][SIZE] ,int row , int colm){

int i , j ;for( int i = 0 ;i < row ; i++ )

{ for( int j = 0 ; j < colm ; j++ )

cout<<x[i][j]<<”\t”;cout<<endl;

}}

//definition for function mat_multiplication

void mat_multiplication(int x[SIZE][SIZE] , int y[SIZE][SIZE] , int z[SIZE][SIZE] ,int row1, int colm1 , int colm2 ) {

int i , j , k;for( int i = 0 ;i < row1 ; i++ )

{ for( int j = 0 ; j < colm1 ; j++ )

{ z[i][j]=0;

for( int k = 0 ; k < colm2 ; k++ )z[i][j] + = x[i][k] *y[k][j];

} } }void main( ) { int a[SIZE][SIZE] , b[SIZE][SIZE] , c[SIZE][SIZE] , row1 , colm1 , row2 , colm2 ;clrscr( );cout<<”\n Enter the order of first Matrix <= “<<SIZE<<”*”<<SIZE<<”\n”;cin>>row1>>colm1;cout<<”\n Enter the order of Second Matrix

Page 3: c++

<= “<<SIZE<<”*”<<SIZE<<”\n”;cin>>row2>>colm2;if(colm1= = row2) {cout<<”\mEnter the first Matrix of order “ << row1 << ” * ” << colm1 << endl;enter( a , row1,colm1);cout<<”\mEnter the second Matrix of order “ << row2 << ” * ” << colm2 << endl;enter( b , row2,colm2);clrscr( );cout<<”\n\n First Matrix is ….. \n”;display( a,row1, colm1);cout<<”\n\n Second Matrix is ….. \n”;display( b,row2, colm2);mat_multiplication(a , b , c , row1,colm1,colm2 ); //function callcout<<”\n\n Multiplication of two Matrices is \n\n ”;display( c,row1, colm2);}else {cout<<”\n\n Matrix Multiplication is not possible \n\n “return;}getch( );}Output :Enter the order of the first matrix<=5*53 3Enter the order of the first matrix<=5*53 3Enter the first matrix of order 3 * 3 5 2 81 4 07 1 9Enter the second matrix of order 3 * 3 6 9 38 2 65 7 1First matrix is 5 2 81 4 07 1 9Second matrix is 6 9 38 2 65 7 1Multiplication of two matrices is

86 105 3538 17 2795 128 36

Q.5. Write a program to convert a 2-digit octal number to its binary equivalent .Sol n : #include<conio.h>#include<iostream.h>#include<math.h>void oct_to_bin( int octnum ) {

unsigned long bin_num[6];int i = 0 , j , dec_num=0 , p = 0 , r ;

// p for power and r for remainder //convert from octal to decimal first while( octnum) { r = octnum % 10; dec_num = dec_num + ( r * pow(8 ,p ) ); p++; octnum / = 10; }cout<<”\n Decimal equivalent is : ” << dec_num << endl;//now convert from decimal to binary do { r=dec_num % 2; bin_num[i++] =r; dec_num / = 2 ; }while(dec_num);cout<<”\n Binary equivalent is : ”;for(j = i - 1 ; j > = 0 ; j--)cout<<bin_num[j];}void main( ) { int octnum ; clrscr( ); cout<< “\n Enter any 2 digit octal number”; cin>>octnum; oct_to_bin(octnum);//function callgetch( );}

Output : Enter any 2 digit octal number 77Decimal equivalent is : 63Binary equivalent is : 111111

Page 4: c++

Q . 6. Write a C++ program to calculate the area of circle ,rectangle or triangle depending upon the user’s choice using structures .Sol n :

#include<conio.h>#include<iostream.h>#include<math.h>struct circle { float radius ;float area;}circ;

struct rectangle { float length; float breadth; float area; }rect;

struct triangle { float base; float height ; float area; }tri;

void main( ) { int choice; do { clrscr( ); cout<<”\n1. Calculate the area of circle” ; cout<<”\n2. Calculate the area of rectangle ” ; cout<<”\n3. Calculate the area of right angled triangle ” ; cout<<”\n\n Enter your choice” ;

cin>>choice; }while((choice<1) || (choice>3)); switch(choice) { case 1 : cout<<”\n Enter the radius :”;

cin>>circ.radius; circ.area = 3.14 * pow(circ.radius , 2 ); cout<<”\n Area of circle is : ” <<circ.area << “sq. units\n”;

break; case 2 : cout<<”\n Enter the length and breadth of rectangle :”;

cin>>rect.length>>rect.breadth; rect.area = rect.length* rect.breadth; cout<<”\n Area of rectangle is : ” << rect . area << “sq. units\n”;

break; case 3 : cout<<”\n Enter the base and height of right angled triangle :”;

cin>>tri.base>>tri.height; tri.area = tri.base* tri.height/2.0; cout<<”\n Area of triangle is : ” << tri.area << “sq. units\n”;

break; } getch( ); } OUTPUT : 1. Calculate the area of circle2. Calculate the area of rectangle 3. Calculate the area of right angled triangle Enter the choice :1Enter the radius :5Area of circle is :78.5 sq. units

Q. 7. Write a C++ program to evaluate students performance using Structure :

struct student { int roll_no , marks[5] ; char name[30]; };

Sol n :

#include<iostream.h>#include<conio.h>#include<stdio.h>struct student

{ int roll_no; char name[30]; int marks[5];};

void main( ){ student st; char grade; float avg , total = 0.0; clrscr( ); cout<<”\n Enter the student’s record : \n”; cout<<”\n Roll number :”; cin>>st.roll_no; cout<<”\n Enter Name :”; gets(st.name); cout<<”\n Enter students marks :\n”; for( int j = 0 ; j < 5 ; j++ ) {

cout<<”\n Subject “<<j+1<<”: ”;cin>>st.marks[j];total + = st.marks[i];

}

Page 5: c++

avg = total/5.0;if(avg > 80)

grade = ‘A’;else {

if(avg > = 60 && avg < 80 )grade = ‘B’;

else {

if(avg > = 40 && avg < 60 ) grade = ‘C’;else grade = ‘D’;

} } clrscr( );cout<<”\n Students performance evaluation is : \n”;cout<<”\n\n Name : ”<<st.name;cout<<”\n\n Roll Number : ”<<st.roll_no;cout<<”\n\n Total Marks : ”<<total;cout<<”\n\n Average : ”<<avg;cout<<”\n\n Grade : ”<<grade;getch( );}

OUTPUT :

Enter the student’s record :Roll Number : 12555Name : Rohan DixitEnter students marks :Subject 1 : 100Subject 2 : 98 Subject 3 : 97Subject 4 : 100Subject 5 : 95Students performance evaluation is : \n”;Name : Rohan DixitRoll Number : 12555Total Marks :490Average : 98Grade : A

Q. 8. Write a program in C++ using the concept of function overloading to implement polymorphism To calculate area of Triangle,Circle and Square .Sol n :

#include<iostream.h>#include<conio.h>#include<math.h>#define PI 3.14159void area(float a, float b, float c ) { float s, ar;

if(((a+b)>c)&&((b+c)>a)&&((c+a)>b)){ s = (a+b+c) / 2.0 ; ar = sqrt ( ( s*(s-a)*(s-b)*(s-c ));

cout<<”\n Area = “<< ar <<”sq. units\n\n”;}

else cout<<”\n Area of triangle is not possible\n\n”; }

void area( float r ) {

float ar;ar = PI * r * r ;cout<<”\n Area = “<< ar <<”sq. units\n\n”;

}void area(int side) { float ar;

ar = side * side ; cout<<”\n Area = “<< ar <<”sq. units\n\n”; }void main( ) {

float a, b, c, radius ; int side ,choice; do

{ clrscr( );

cout<<”\n1. Calculate the area of Triangle” ; cout<<”\n2. Calculate the area of Circle ” ; cout<<”\n3. Calculate the area of Square ” ; cout<<”\n\n Enter your choice” ; cin>>choice; }while((choice<1) || (choice>3)); switch(choice) { case 1 : cout<<”\n\n Enter the three sides :\n”; cin>>a>>b>>c;

area(a, b, c); break;

case 2 : cout<<”\n\n Enter the radius :\n ”; cin>>radius; area(radius); break;

case 3 : cout<<”\n\n Enter the side of square :\n”; cin>>side;

area(side); break;

} getch( ); }

OUTPUT : 1. Calculate the area of Triangle

Page 6: c++

2. Calculate the area of Circle 3. Calculate the area of SquareEnter the choice :1Enter the three sides :3 4 5Area = 6 sq. units

Q.9. Write a C++ program to perform various operations on string class without using language supported built-in string functions . The operations on class are :(a) Read a String (b) Display a string (c) Reverse a string (d) Copy a string into a empty string(e) Concatenate two strings Sol n : #include<iostream.h>#include<conio.h>#include<stdio.h>#include<process.h>#define S 80class string { private :

char str[S] , ch; int i ;public : void init( );

void readstr( ); void display( );

void reverse( ); void copystr( ); void concatenate( );

};void string ::init( ) {

i = 0 ;while(i < S - 1) { str[i] = ‘ ‘ ; i ++;

}str[i] = ’\0’;

}void string : : readstr( ) {

clrscr( );cout<<”Enter a string of length <” << S <<

endl<<endl;i = 0;ch = getchar( );while(ch!=’\n’) {

str[i] = ch ; i ++;

ch = getchar( );

}str[i] = ’\0’;

}void string : :display( ) {

clrscr( );cout<<”Entered string is \n\n”;i = 0 ; while(str[i] != ’\0’ ) {

cout<<str[i];i++;

} }void string : : reverse( ) {

clrscr( );int mid ,len =0;while(str[len] != ‘\0’) {

len + +; }mid = len/2;for( i = 0 ; i < mid ; i ++) {

ch = str[len -1 – i ];str[len -1 – i ] = str[i];str[i] = ch;

}cout<<”\n Reversed string is \n\n”;cout<<str;

}void string : :copystr( ) {

char empty_str[S];clrscr( );i = 0 ;while(str[i] = ‘\0’) {

empty_str[i] = str[i];i ++ ;

}empty_str[i] = ‘\0’;

cout<<”\n\n Empty string after copy is \n \n ” ; cout<<empty_str; }void string : : concatenate( ) {

char str1[S] , str2[S] , concatenate_str[S];int len1 , len2;len1 = len2 = 0;clrscr( );cout<<”Enter the first string of length <” <<

S <<endl<<endl;i = 0;

Page 7: c++

ch = getchar( );while(ch != ‘\n’) {

str1[i] = ch;i ++ ;ch = getchar( );

}str1[i] = ‘\0’;

cout<<”Enter the second string of length < ”<< S <<endl<<endl;

i = 0;ch = getchar( );while(ch != ‘\n’) {

str2[i] = ch;i ++ ;ch = getchar( );

}str2[i] = ‘\0’;

//finding string length for( i = 0 ; str1[i] != ’\0’ ; i ++) len1++; for( i = 0 ; str2[i] != ’\0’ ; i ++) len2++; for( i = 0 ; i < len1 ; i ++) concat_str[i] = str1[i]; for( i = 0 ; i < len2 ; i ++) concat_str[len1 + i] = str2[i]; concat_str[len1 + len2] = ‘\0’; cout<<”\n\n Without using string functions\n\n”; cout<<”\n Concatenated string is :\n\n”; cout<<concat_str; }void main( ) {

string obj;obj.init( );int choice;while( 1 ) {

clrscr( ); cout<<”\n1. Read a string\n”; cout<<”\n2. Display the string\n”; cout<<”\n3. Reverse the string\n”;

cout<<”\n4. Copy the string into an empty string \ n ” ; cout<<”\n5. Concatenate two string \n”;

cout<<”\n6. Exit\n”; cout<<”\n Enter your choice(1-6) :”;

cin>>choice;switch(choice) {

case 1 : obj.readstr( ); break;

case 2 : obj.display( ); break;

case 3 : obj.reverse( ); break;

case 4 : obj.copystr( ); break;

case 5 : obj.concatenate( ); break;

case 6 : exit(0);default :

cout<<”\n Wrong choice \n”;getch( );

} cout<<\n\n Press any key to continue\n”; getch( );}

}OUTPUT:

1. Read a string2. Display the string3. Reverse the string4. Copy the string into an empty string 5. Concatenate two string6. ExitEnter your choice(1-6) : 1Enter a string of length < 80 RAMA O RAMAPress any key to continue1. Read a string2. Display the string3. Reverse the string4. Copy the string into an empty string 5. Concatenate two string6. ExitEnter your choice(1-6) :2Entered string is RAMA O RAMAPress any key to continue1. Read a string2. Display the string3. Reverse the string4. Copy the string into an empty string 5. Concatenate two string6. ExitEnter your choice(1-6) :3AMAR O AMARQ .10. Write a C ++ program using classes and concept of friend function for multiplication of two matrices .Sol n : #include<iostream.h>#include<conio.h>#include<process.h>#include<iomanip.h>

Page 8: c++

#define SIZE 5class matrix {

private : int i , j , colm , row, mat[SIZE] [SIZE] ;public : void enter( ); void display( );

friend matrix mat_multiplication(matrix x , matrix y) }; void matrix : : enter( ) { cout<<”\n Enter the order of Matrix <= “<<SIZE<<”*”<<SIZE<<”\n”; cin>>row>>colm; cout<<”\mEnter the Matrix of order “ << row << ” * ” << colm << endl; for( int i = 0 ;i < row ; i++ ) for( int j = 0 ; j < colm ; j++ )

cin>>mat[i][j]; }void matrix : : display( ) { for( int i = 0 ;i < row ; i++ ) { for( int j = 0 ; j < colm ; j++ )

cout<<setw(8)<<mat[i][j]; cout<<”\n”;

} } matrix mat_multiplication(matrix x , matrix y)

{ matrix z; int i , j , k ; if(x.colm != y.row)

{ cout<<”\n matrix multiplication is not

possible\n\n”; exit(0); } else { z.row = x.row; z.colm = y.colm; for( int i = 0 ;i <x.row ; i++ ) { for( int j = 0 ; j < y.colm ; j++ ) { z.mat[i]j] = 0; for( int k = 0 ;k < x.colm ; k++ )

{

z.mat[i][j] + = x.mat[i][k] * y.mat[k][j]; } } } } return(z); }void main( )

{ matrix a ,b ,c; clrscr( ); a.enter( ); b.enter( ); c = mat_multiplication(a , b); clrscr( ); cout<<”*****First matrix is *****\n\n”; a.display( );

cout<<”*****Second matrix is *****\n\n”; b.display( ); cout<<”*****Resultant matrix is *****\n\n”; c.display( ); }

OUTPUT:ENTER THE ORDER OF MATRIX <=5*52 2Enter matrix of order 2 * 21 32 4ENTER THE ORDER OF MATRIX <=5*52 2Enter matrix of order 2 * 26 49 7*****First matrix is *****

1 32 4*****Second matrix is *****

6 49 7

*****Resultant matrix is *****

33 2548 36

Q. 11. Write a Program using concept of constructor overloading to calculate area of circle, rectangle and triangle .Sol n . #include<iostream.h>#include<conio.h>

Page 9: c++

#include<math.h>#include<string.h>class figure {

private : float radius , side1 , side2 , side3; char shape[10];public : figure(float r) {

radius = r;strcpy(shape ,”circle”);

} figure(float s1 , float s2) {

side1 = s1;side2 = s2;side3 = radius = 0.0;strcpy(shape , “rectangle”);

} figure(float s1 , float s2 , float s3)

{side1 = s1;side2 = s2;side3 = s3;radius = 0.0;strcpy(shape , “triangle”);

} void area( ) { float ar , s;

if(radius = = 0.0; {

if(side3 = = 0.0) ar = side1 * side2;

else {

s = (side1 + side2 + side3 )/2.0; ar = sqrt(s * (s - side1) * (s – side2) * (s – side3));

} }else ar = 3.14 * radius * radius ;

cout<<”\n\n Area of the ”<<shape<<” is : ” << ar << ” sq. units \n” ; } };void main( )

{ clrscr( ); figure circle(10.0); figure rectangle(15.0 , 20.6);

figure triangle(3.0 , 4.0 , 5.0 ); circle.area( ); rectangle.area( );

triangle.area( ); getch( );}

OUTPUT: Area of circle is :314 sq units Area of rectangle is :309 sq units Area of triangle is : 6 sq units

Q.12. Write a program to generate a series of fibonacci using a copy constructor where the copy constructor is defined outside the class declaration .Sol n . #include<conio.h>#include<iostream.h>class fibonacci {

private :unsigned long int f0 , f1 , f2 ;

public : fibonacci( );fibonacci(fibonacci &ptr);void increment( );void display( );

};fibonacci : : fibonacci( ) {

f0 = 0;f1 = 1;f2 = f0 + f1;

}fibonacci : :fibonacci(fibonacci &ptr) {

f0 = ptr.f0;f1 = ptr.f1;f2 = ptr.f2;

}void fibonacci : :increment( ) {

f0 = f1;f1 = f2;f2 = f0 + f1;

}void fibonacci : :display( ) {

cout<<f2<<’\t’; }void main( ) {

clrscr( ); fibonacci number ;

for( int j = 0 ; j < = 15; j++) {

number.display( );number.increment( );

}

Page 10: c++

getch( ); }OUTPUT :0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Q.13. Write a program to simulate a simple banking system in which the initial balance and the rate of interest are read from the key board and these values are initialized using the constructor member function . The destructor member function is defined in this program to destroy the class objects created using the constructor member function .The program consists of the following methods :1) To initialize the balance and the rate of interest using the constructor member function2) To make a deposit 3) To withdraw an amount from the balance4) To find the compound interest based on the rate of interest 5) To know the balance amount6) To display the menu options 7) To destroy the object of the class , the destructor member functionSoln.#include<conio.h>#include<iostream.h>#include<stdio.h>class account {

float balance , rate; public :

account( );~account( );void deposit ( );void withdraw( );void compount( );void getbalance( );void menu( );

}; account : :account( ) { cout<<”enter the initial balance\n”;

cin>>balance;cout<<” Enter the interest rate \n”;cin>>rate;

} account : :~account( ) {

cout<<”The DataBase has been deleted ”; }void account : :deposit( ) { float amount;

cout<<”Enter the amount : ”;

cin>>amount; balance = balance + amount;

}void account : :withdraw( ) {

float amount ; cout<<” How much to withdraw :”; cin>>amount; if(amount < = balance) {

balance = balance – amount ;cout<< “ amount drawn = :” <<

amount <<endl;cout<<”current balance = :”<<

balance<<endl; }else

cout<<0; }void account : :compound( ) {

float interest ;interest = balance * rate;balance = balance + interest;cout<<”interest amount = ” <<

interest<<endl;cout<<”total amount = ”<<balance<<endl;

}void account : : getbalance( ) {

cout<<”Current balance = : ”;cout<<balance<<endl;

}void account : :menu( ) {

cout<<”d -> deposit “<<endl; cout<<”w -> withdraw “<<endl;

cout<<”c -> compound interest “<<endl; cout<<”g -> get balance “<<endl; cout<<”q -> quit “<<endl;

cout<<”option please ? :\n“; }void main( ) {

clrscr( );account acct;char ch;acct.menu( );while( ( ch = getchar( ) ) !=’q’) {

switch( ch) { case ‘d’ :

acct.deposit( );break;

Page 11: c++

case ‘w’ :acct.withdraw( );break;

case ‘c’ :acct.compound( );break;

case ‘g’ :acct.getbalance( );break;

} } }OUTPUT:Enter the initial balance 1000interest rate0.2d -> depositw -> withdrawc-> compound interestg -> get balanceq -> quitoption ,pleasegcurrent balance = : 1000denter the amount :100current balance : 1100whow much to withdraw ?amount drawn= : 200current balance =: 900gcurrent balance = : 900cinterest amount = 180total amount 1080qDataBase has been deleted

Q. 14. IIlustrating single level inheritance using public derivation .Sol n . #include<conio.h>#include<iostream.h>class base {

private : int x;public :

int y ; void enter( ); int return_x; void disp_x( )

};void base : :enter( ) {

cout<<”\n Enter the values of x and y”; cin>>x>>y;

}int base : : return_x( ) {

return x; }void base : :disp_x( ) {

cout<<”\n\n x = “<<x<<endl; }class derived : public base { private :

int z;public : void add( ); void display( );

};void derived : : add( ) {

z = y + return_x( ); }void derived : :display( ) { cout<<”\n\n x = ”<<return_x( ); cout<<”\n\n y = ”<<y<<endl; cout<<”\n\n z = ”<<z<<endl; }void main( ) { derived obj;

clrscr( ); obj.enter( ); obj.add( ); obj.disp_x( ); obj.display( ); obj.y = 50; obj.add( ); obj.display( ); getch( );

}OUTPUT :Enter the values of x and y : 10 20x=10x=10y=20z=30x=10y=50z=60

Page 12: c++

Q. 15. IIlustration of multilevel inheritance .Sol n . #ic\nclude<conio.h>#include<iostream.h>#include<stdio.h>const LEN = 41;class publication {

protected : char title[LEN]; float price;

};class book : : protected publication { private : int pages;

public : void getdata(void)

{ cout<<”\n Enter the title :”; gets(title); cout<<”\n Enter the price :”;

cin>>price; cout<<”\n Enter the no.of pages : ” ;

cin>>pages;}

void putdata(void){ cout<<”\nTitle : ”<<title; cout<<”\nPrice : ”<<price; cout<<”\nPages: ”<<pages;}

};class tape : : protected publication {

private : int play_time;

public : void getdata(viod)

{ cout<<”\n Enter the title :”;

gets(title); cout<<”\n Enter the price :”;

cin>>price; cout<<”\n Enter the play time in minutes :”; cin>>play_time;

}void putdata(void)

{ cout<<”\nTitle : ”<<title; cout<<”\nPrice : ”<<price; cout<<”\nPlay_time:”<<play_time<<”minutes \n ”; }};

void main( ){

book obj1;tape obj2;clrscr( )obj1.getdata( );obj1.putdata( );obj2.getdata( );obj2.putdata( );

}

OUTPUT:Enter the values of x and y : 10 20x = 10y = 20z = 30Enter the values of x and y : 50 60x = 50y = 60z = 110Q.16. IIlustration of static class members(data members and member functions)#include<iostream.h>#include<conio.h>class student {

private : static int count;

int rollno , marks ; public :

void enter(int r int m) {

rollno = r ;marks = m ;count + +;

} void show(void)

{ cout<<”\n Roll number :” << rollno << ”\t”;

cout<<”\n Marks :”<< marks<<” \t ” ; } static void showcount(void) { cout<<”count = “<<count<<”\n”; }};

int static : :count = 0;void main( )

{ clrscr( );

student st1, st2 , st3 ; st1.enter(1001 , 99); student : : showcount( ); st2.enter(1002 , 88);

Page 13: c++

student : : showcount( ); st3.enter(1003 , 100);

student : : showcount( ); st1.show( );

st2.show( ); st3.show( );

getch( ); }OUTPUT:count = 1count = 2count = 3Roll number : 1001 Marks : 99Roll number : 1002 Marks : 88Roll number : 1003 Marks : 100

Q.17. Write a C++ program for linear search in arrays .Sol n . #include<conio.h>#include<iostream.h> const int MAX = 10;class array {

private : int arr[max] , count;public : array( ); void add( int item); int lsearch(int item);

};array : :array( ) {

count = 0;for( int i = 0 ; i < MAX ; i + +) arr[i] = 0;

}void array : : add(int item) {

if(count < MAX) { arr[count] = item ; count + +;

}else cout<<”\n Array is full ”<<endl;

}int array : : lsearch(int item) {

for( int i = 0 ; i < count ; i + +) {

if(arr[i]= =item) break;

} if(i = = count)

return -1; else

return i; }void main( ) {

array a;clrscr( );a.add(11);a.add(2);a.add(9);a.add(13);a.add(57);a.add(25);a.add(17);a.add(1);a.add(90);a.add(3);int num;

cout<<”\n Enter the number to be searched :”; cin>>num; int r = a.lsearch(num);

if( r = = -1) cout<<”\n Number is not present in the array.”; else cout<<”\n The number is at position ”<<r <<” in the array.”; getch( ); }OUTPUT:Enter the number to search :57The number is at position 4 in the array.Q.18. Write a C++ program for Binary search in arrays .Sol n . #include<conio.h>#include<iostream.h> const int MAX = 10;class array {

private : int arr[max] , count;public : array( ); void add( int item); void bsearch(int item);

};array : :array( ) {

count = 0;for( int i = 0 ; i < MAX ; i + +) arr[i] = 0;

}void array : : add(int item) {

Page 14: c++

if(count < MAX) { arr[count] = item ; count + +;

}else cout<<”\n Array is full ”<<endl;

}void array : : bsearch(int item) { int mid , lower = 0 , upper = count -1 , flag = 1; for(mid = (lower +upper )/2 ; lower<=upper; mid = (lower +upper )/2 )

{ if(arr[mid = =item)

{ cout<<”\n The number is at position ” <<mid <<” in the array.”;

flag = 0; break; }

if(arr[mid] >item)upper = mid – 1;

elselower = mid – 1;

}if(flag) cout<<”\n Element is not present in the array.”; }void main( ) {

array a;clrscr( );a.add(1);a.add(2);a.add(3);a.add(9);a.add(11);a.add(13);a.add(17);a.add(57);a.add(90);a.add(90);int num;

cout<<”\n Enter the number to be searched :”; cin>>num; a.bsearch(num); getch( ); }

OUTPUT:Enter the number to search :57The number is at position 8 in the array.

Q.19. Write a C++ program for sorting an array using Bubble sort .Sol n . #include<conio.h>#include<iostream.h> const int MAX = 10;class array {

private : int arr[max] , count;public : array( ); void add( int item); void bsort( ); void display( );

};array : :array( ) {

count = 0;for( int i = 0 ; i < MAX ; i + +) arr[i] = 0;

}void array : : add(int item) {

if(count < MAX) { arr[count] = item ; count + +;

}else cout<<”\n Array is full ”<<endl;

}void array : :bsort( ) { int tmp;

for( int i = 0 ; i <= count – 2 ; i + +) { for( int j = 0 ; j <= count – 2 - i ; j + +)

{ if(arr[j]>arr[j+1]) { tmp = arr[j];

arr[j] = arr[j+1];arr[j+1] = tmp;

}}

} }void array: :display( ) {

for( int i = 0 ; i < count ; i + +) cout<<arr[i]<<”\t”; cout<<endl;}

void main( )

Page 15: c++

{array a;clrscr( );a.add(25);a.add(17);a.add(31);a.add(13);a.add(2);

cout<<”\n Bubble sort.\n ”; cout<<”\n Array before sorting :”<<endl;

a.display( );a.sort( );cout<<”\n Array After sorting :”<<endl;a.display( );

getch( ); }OUTPUT:Bubble sort.Array before sorting :25 17 31 13 2Array before sorting :2 13 17 25 31

Q.20. Write a C++ program for sorting an array using selection sort .Sol n . #include<conio.h>#include<iostream.h> const int MAX = 10;class array {

private : int arr[max] , count;public : array( ); void add( int item); void Ssort( ); void display( );

};array : :array( ) {

count = 0;for( int i = 0 ; i < MAX ; i + +) arr[i] = 0;

}void array : : add(int item) {

if(count < MAX) { arr[count] = item ; count + +;

}else cout<<”\n Array is full ”<<endl;

}

void array : :Ssort( ) { int tmp;

for( int i = 0 ; i <= count – 2 ; i + +) { for( int j = i +1 ; j <= count – 1 ; j + +)

{ if(arr[i]>arr[j]) { tmp = arr[i];

arr[i] = arr[j];arr[j] = tmp;

}}

} }void array: :display( ) {

for( int i = 0 ; i < count ; i + +) cout<<arr[i]<<”\t”; cout<<endl;}

void main( ) {

array a;clrscr( );a.add(25);a.add(17);a.add(31);a.add(13);a.add(2);

cout<<”\n Selection sort.\n ”; cout<<”\n Array before sorting :”<<endl;

a.display( );a.sort( );cout<<”\n Array After sorting :”<<endl;a.display( );

getch( ); }OUTPUT:Selection sort.Array before sorting :25 17 31 13 2Array before sorting :2 13 17 25 31

Q.21. Write a C++ program for sorting an array using insertion sort .Sol n . #include<conio.h>#include<iostream.h> const int MAX = 10;class array {

private :

Page 16: c++

int arr[max] , count;public : array( ); void add( int item); void Isort( ); void display( );

};array : :array( ) {

count = 0;for( int i = 0 ; i < MAX ; i + +) arr[i] = 0;

}void array : : add(int item) {

if(count < MAX) { arr[count] = item ; count + +;

}else cout<<”\n Array is full ”<<endl;

}void array : :Isort( ) { int tmp;

for( int i = 1 ; i <= count – 1 ; i + +) { for( int j = 0 ; j < i ; j + +)

{ if(arr[j]>arr[i]) { tmp = arr[j];

arr[j] = arr[i];for( int k = i ; k < j ; k- -)

arr[k] = arr[k-1]; arr[k+1] = tmp; }}

} }void array: :display( ) {

for( int i = 0 ; i < count ; i + +) cout<<arr[i]<<”\t”; cout<<endl;}

void main( ) {

array a;clrscr( );a.add(25);a.add(17);a.add(31);a.add(13);

a.add(2); cout<<”\n Insertion sort.\n ”; cout<<”\n Array before sorting :”<<endl;

a.display( );a.sort( );cout<<”\n Array After sorting :”<<endl;a.display( );

getch( ); }OUTPUT:Insertion sort.Array before sorting :25 17 31 13 2Array before sorting :2 13 17 25 31

Q.22. Write a program to perform the following operations on arrays :1) insertion 2) deletion 3) reverse 4)traversalSol n . #include<conio.h>#include<iostream.h>const int MAX = 5;class array {

private : int arr[MAX];public : void insert(int pos , int num); void del(int pos); void reverse( ); void display( );

};void array : :insert( int pos , int num) { //shift elements to right

for( int j = MAX – 1 ; j > = pos ; j- - )arr[j] = arr[j-1];

arr[j] = num; }void array : :del(int pos) {

//skip to the desired positionfor(int j = pos ; j < MAX ; j + +)

arr[j – 1] = arr[j]; arr[j -1] = 0;

}void array : :reverse( ) {

//reverses the entire arrayfor(int j = 0 ; j < MAX/2 ; j + +) { int tmp = arr[j]; arr[j] = arr[MAX – 1 – j]; arr[MAX – 1 – j] = tmp;

Page 17: c++

}void array : :display( ) {

for(int j = 0 ; j < MAX ; j + +)cout<<” “<<arr[j];

}void main( ) {

array a ;clrscr( );

a.insert(1,11);a.insert(2,12);a.insert(3,13);a.insert(4,14);a.insert(5,15);

cout<<”\n Elements of Array: ”;a.display( );

a.del(5);a.del(2);

cout<<”\n\n After Deletion :”;a.display( );

a.insert(2,222);

a.insert(5,555);cout<<”\n After insertion: ”;a.display( );

a.reverse( );cout<<”\n After reversing: ”;a.display( );

getch( ); }

Q.23. Write a C++ program for merging of two arrays .

Sol n . #include<conio.h>#include<iostream.h>const int MAX1 = 5;const int MAX2 = 7;class array {

private : int *arr; int size;public : void create(int sz); void sort( );

void display( ); void merge(array &a , array &b); };void array : : create(int sz)

{size = sz;arr = new int[size];int n;for( j = 0 ; j<size ; j + + ) {

cout<<”\nEnter the element no. ” << (j+1) <<” ”;cin>>n;arr[i] = n;

} }void array : :sort( ) {

int tmp;for( j = 0 ; j<size ; j + + ) {

for( k = j +1 ; k<size ; k + + ){ if(arr[j] > arr[k])

{ tmp = arr[j]; arr[j] = arr[k]; arr[k] = tmp;}

} }

}void array : :display( ) {

for( j = 0 ; j<size ; j + + )cout<<” ”<<arr[j];

}void array : :merge(array &a , array &b) {

int i , j , k ;size = a.size + b.size ;arr = new int[size];for(k=0 , j = 0 , i = 0 ; i<=size ; i + + ) { if(a.arr[k]<b.arr[j]) {

arr[i] = a.arr[k];k++;if(k>=a.size) { for(i++ ;j<b.size;j++,i++)

arr[i] = b.arr[i]; }

} else {

arr[i] = b.arr[j]; j++; if(j>=b.size) {

Page 18: c++

for(i++ ; k<=a.size ; k++ , i++)

arr[i] = a.arr[k]; } } } }void main( ) {

clrscr( );array a;cout<<”\nEnter elements of first array :\n”;a.create(MAX1);

array b; cout<<”\nEnter elements of second array : \ n ”;

b.create(MAX2);

a.sort( );b.sort( );cout<<”\n First array :\n”;a.display( );

cout<<”\n Second array :\n”;b.display( );cout<<”\n\nAfter Merging :\n”;array c;c.merge(a , b);c.display( );getch( );

}OUTPUT:Enter elements of first array:Enter element no. 1 67Enter element no. 2 12Enter element no. 3 -4Enter element no. 4 43Enter element no. 5 2Enter elements of second array:Enter element no. 1 8Enter element no. 2 10Enter element no. 3 -2Enter element no. 4 39Enter element no. 5 6Enter element no. 6 7Enter element no. 7 19First array :-4 2 12 43 67Second array :-2 6 7 8 10 19 39After Merging :-4 -2 2 6 7 8 10 12 19 39 43 67

Q.24. Write a C++ program to perform push and pop operations in stack implemented using an array.Sol n .

#include<conio.h>#include<iostream.h>const int MAX = 10;class stack { private :

int arr[MAX] , top; public :

stack( );void push( );int pop( );

};stack : :stack( ) {

top = -1; }void stack: :push( int item) {

if(top= =MAX-1) { cout<<endl<<”Stack is full”; return ; } top + +; arr[top] = item;

}int stack : :pop( ) { if(top= = -1)

{ cout<<endl<<”Stack is empty”; return NULL;} int data = arr[top]; top -- ; return data;

}void main( ) {

stack s;clrscr( );s.push(11);s.push(23);s.push(-8);s.push(16);s.push(27);s.push(14);s.push(20);s.push(39);s.push(2);s.push(15);s.push(7);

int i = s.pop( );cout<<”\n Item poped :”<<i ;

Page 19: c++

int i = s.pop( );cout<<”\n Item poped :”<<i ;

int i = s.pop( );cout<<”\n Item poped :”<<i ;

int i = s.pop( );cout<<”\n Item poped :”<<i ;

int i = s.pop( );cout<<”\n Item poped :”<<i ;

getch( ); }OUTPUT: Stack is full

Item popped : 15Item popped : 2Item popped : 39Item popped : 20Item popped : 14

Q.25. Write a C++ program to perform push and pop operations in stack implemented using Linked list .Sol n . #include<conio.h>#include<iostream.h>class stack { private :

struct node {

int data;node * link;

}*top; public :

stack( );void push(int item);int pop( );~stack( );

};stack : : stack( ) {

top = NULL; }void stack : :push(int item) {

node *tmp;tmp = new node;if(tmp = = NULL) cout<<endl<<”Stack is full”;

tmp -> data = item;tmp -> link = top;top = tmp;

}int stack : :pop( ) {

if(top = = NULL) {

cout<<endl<<”Stack is empty”;return NULL;

}node * tmp;int item;tmp = top;item = tmp ->data;top = top - >link;delete tmp;return item;

}stack : : ~stack( ) {

if(top = =NULL) return;node *tmp;while(top!=NULL) {

tmp = top;top = top - >link;delete tmp;

} }void main( ) {

clrscr( );stack s;s.push(14);

s.push(-3);s.push(18);s.push(29);s.push(31);s.push(16);

int r = s.pop( );cout<<”\n Item popped : “<<r;int r = s.pop( );cout<<”\n Item popped : “<<r;int r = s.pop( );cout<<”\n Item popped : “<<r;getch( );

}OUTPUT :Item popped :16Item popped :31 Item popped :29

Page 20: c++

Q.26. Write a C++ program to perform insertion and deletion operations in queue implemented using an array.Sol n . #include<conio.h>#include<iostream.h>const int MAX = 10;class queue {

private : int arr[MAX]; int front , rear;public : queue( ); void addq( int item); int delq( );

};queue : :queue( )

{front = -1;rear = -1;

}void queue : : addq(int item)

{if(rear = =MAX – 1) {

cout<<”\n Queue is full”;return;

}rear + +;arr[rear] = item;if(front = = -1) front = 0;

} int queue : : delq( ) {

int data;if(front = =-1) {

cout<<”\n Queue is Empty”;return Null;

}data = arr[front];arr[front] = 0;if(front = =rear) front = rear = -1;else front + +;

return data; }void main( ) {

clrscr( );queue a;

a.addq(23);a.addq(9);a.addq(11);a.addq(-10);a.addq(25);a.addq(16);a.addq(17);a.addq(22);a.addq(19);a.addq(30);a.addq(32);int r = a.delq( );cout<<”\n Item deleted : ”<<r;int r = a.delq( );cout<<”\n Item deleted : ”<<r;int r = a.delq( );cout<<”\n Item deleted : ”<<r;

getch( );}

OUTPUT:Queue is fullItem deleted : 23Item deleted : 9Item deleted : 11Q.27. Write a C++ program to perform push and pop operations in stack implemented using Linked list .Sol n . #include<conio.h>#include<iostream.h>class queue { private :

struct node {

int data;node * link;

}*front , *rear; public :

queue( );void addq(int item);int delq( );~queue( );

};queue : : queue( ) {

front = rear =NULL; }void queue : :addq(int item) {

node *tmp;tmp = new node;if(tmp = = NULL) cout<<endl<<”Queue is full”;

Page 21: c++

tmp -> data = item;tmp -> link = NULL;if(front = = NULL){

rear = front = tmp;return;

}rear ->link = tmp;rear = rear ->link;

}int queue : :delq( ) {

if(front = = NULL) {

cout<<endl<<”Queue is empty”;return NULL;

}node * tmp;int item;item = front ->data;tmp = front;front = front - >link;delete tmp;return item;

}queue : : ~queue( ) {

if(front = =NULL) return;node *tmp;while(top!=NULL) {

tmp = front;front = front - >link;delete tmp;

} }void main( ) {

clrscr( );queue a;a.addq(11);

a.addq(-8);a.addq(23);a.addq(19);a.addq(15);a.addq(16);a.addq(28);

int r = a.delq( );cout<<”\n Item extracted : “<<r;int r = a.delq( );cout<<”\n Item extracted : “<<r;int r = a.delq( );cout<<”\n Item extracted : “<<r;

getch( ); }OUTPUT :Item extracted :11Item extracted :-8 Item extracted :23

Q.28. Write a C++ program to perform insertion and deletion operations in a circular queue . Sol n . #include<conio.h>#include<iostream.h>const int MAX = 10;class queue {

private : int arr[MAX]; int front , rear;public : queue( ); void addq( int item); int delq( ); void display( );

};queue : :queue( )

{front = -1;rear = -1;for( int j = 0; j < MAX ; j+ +)

arr[j]= 0;}

void queue : : addq(int item){

if(rear = =MAX – 1 && front= =0) || (rear + 1 = = front) )

{cout<<”\n Queue is full”;return;

}if (rear = = MAX-1) rear = 0;else rear + +;

arr[rear] = item;if(front = = -1) front = 0;

} int queue : : delq( ) {

int data;if(front = =-1) {

cout<<”\n Queue is Empty”;

Page 22: c++

return NULL; }data = arr[front];arr[front] = 0;if(front = =rear) front = rear = -1;else { if(front= =MAX-1)

front = 0; else front + +; }return data;

}void queue : :display( ) {

cout<<endl; for( int j = 0; j < MAX ; j+ +)cout<< arr[j]<<” ”;cout<<endl;

}void main( ) {

clrscr( );queue a;a.addq(14);a.addq(22);a.addq(13);a.addq(-6);a.addq(25);

cout<<”\n Elements in the circular queue :”;a.display( );int r = a.delq( );cout<<”\n Item deleted : ”<<r;

int r = a.delq( );cout<<”\n Item deleted : ”<<r;

cout<<”\n Elements in the circular queue after deletion :”;

a.display( );

a.addq(21);a.addq(17);a.addq(18);a.addq(9);a.addq(20);

cout<<”\n Elements in the circular queue after addition:”;

a.display( );

a.addq(32);

cout<<”\n Elements in the circular queue after addition:”;

a.display( );getch( );

}OUTPUT:Elements in the circular queue14 22 13 -6 25 0 0 0 0 0Item deleted : 14Item deleted : 22Elements in the circular queue after deletion :0 0 13 -6 25 0 0 0 0 0Elements in the circular queue after addition :0 0 13 -6 25 21 17 18 9 20Elements in the circular queue after addition :32 0 13 -6 25 21 17 18 9 20

Q.29. Write a C++ program to read and write class object in a binary file ?Sol n . #include<fstream.h>#include<stdio.h>#include<process.h>#include<conio.h>class joke { private :

int jokeid; char type[5]; char jokedesc[255];public : void newjokeentry( )

{ cout<<”\n Enter joke id :” ; cin>>jokeid; cout<<”\n Enter joke type : ”; gets(type); cout<<”\n Enter joke : ”; gets(jokedesc);}

void showjoke( ){ cout<<”\n joke id :” <<jokeid; cout<<”\n joke type : ”; puts(type); cout<<”\n joke details : ”; puts(jokedesc);}

};void main( )

{ clrscr( ); void addjoke( );//function prototype

void dispfile( ); //function prototype clrscr( ); addjoke( );//function call dispfile( );//function call getch( );

Page 23: c++

}void addjoke( )

{ joke obj; char choice; ofstream ofile;

ofile.open(“jokes.dat”,ios: :app| ios: :binary); if(!ofile) {

cout<<”\n cannot open jokes.dat for writing\n”;exit(1); }while(1) { { clrscr( ); cout<<\n Do you wish to add more 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) ? :YEnter joke id1Enter typeFun

What a wonder fool fellowDo you wish to add more object(y/n) ? :YEnter joke id2Enter typeSad Ramlal married Miss world and lives a HAPPY DAILY lifeDo you wish to add more object(y/n) ? :nContents of file jokes.dat are :1: Fun What a wonder fool fellow2 : SadRamlal married Miss world and lives a HAPPY DAILY life