You are on page 1of 153

# C++ programming class XI & XII .

2011
Class11 Getting started with c++

## Q.1.write a program to Find the sum of n natural numbers.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0; cout<<"upto how many numbers:"; cin>>n; for(int i=1;i<=n;i++) sum+=i; cout<<"\nSum upto "<<n<<" is "<<sum; }

## output: upto how many number:4 sum upto 4 is 8

Q.2.write a program to Find the smallest and largest odd no. #include<iostream.h>
Labhesh Khetawat Page 1

## C++ programming class XI & XII .2011

#include<conio.h> void main() { clrscr(); int n,x,ol=1,os=99; cout<<"\nUpto?"; cin>>n; for(int i=0;i<n;i++) { cout<<"Enter value for x"<<(i+1); cin>>x; if(x%2!=0) { if(x>ol) ol=x; if(x<os) os=x; } } cout<<"\nLargest odd no is:"<<ol; cout<<"\nSmallest odd no is:"<<os; } output: upto? 4
Labhesh Khetawat Page 2

## C++ programming class XI & XII .2011

enter the value foe x1:2 enter the value foe x2:4 enter the value foe x3:7 enter the value foe x4:13 largest odd number is:13 smallest odd number is:7

Q.3.write a program to calculate student's % accepting marks in 3 subs #include<iostream.h> #include<conio.h> void main() { clrscr(); float sub1,sub2,sub3,marks,perc; cout<<"\nEnter marks obtained in three subjects out of 100:"; cin>>sub1>>sub2>>sub3; marks=sub1+sub2+sub3; perc=marks/3; cout<<"\nThe percentage of the student is:"<<perc<<"%"; } otuput: Enter marks obtained in three subjects out of 100:86 89 95 the percantage of the student is:90%

Labhesh Khetawat

Page 3

## C++ programming class XI & XII .2011

Q.4.wtite a program to find the smallest number out of n numbers. #include<iostream.h> #include<conio.h> void main() { clrscr(); int smallest,n,x,c=1; cout<<"\nEnter the value for n:"; cin>>n; cout<<"\nEnter the integer:"; cin>>x; smallest=x; while(c<n) { cout<<"\nEnter the integer:"; cin>>x; if(x<smallest) smallest=x; ++c; } cout<<"\nThe smallest number is:"<<smallest; } output: enter the value for n:599
Labhesh Khetawat Page 4

## C++ programming class XI & XII .2011

enter the integer:1 enter the integer:5 enter the integer:9 enter the integer:89 enter the integer:6576 the smallest no is: 1

Q.5.write a program to calculate temperature in celsius to fahrenheit #include<iostream.h> #include<conio.h> void main() { clrscr(); float f,c; cout<<"Enter temperature in celsius:"; cin>>c; f=((9*c)/5)+32; cout<<"\nTemperature in fahrenheit is:"<<f; } output: enter the temperature in celsius:33 temperature in farehrenheit is :91.400002

Data handling
Labhesh Khetawat Page 5

## C++ programming class XI & XII .2011

Q.1.Program to enter a character and print its ASCII code and ASCII code of next character. #include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"enter the character:"; cin>>ch; int num=ch; cout<<"the ascii code for"<<ch<<" is"<<num<<"\n"; cout<<"adding 1 to the character code:\n"; ch=ch+1; num=ch; cout<<"the ascii code for "<<ch<<"is"<<num; }

Output: Enter the character : k The ASCII code for 'k' is 107 Adding 1 to the character code : The ACSII code for 'l' is 108
Labhesh Khetawat Page 6

## C++ programming class XI & XII .2011

Q.2.Program to print cube of a number. #include<iostream.h> #include<conio.h> float cube(float); int main() { clrscr(); float num; cout<<"\nEnter a number:"; cin>>num; cout<<"\nThe cube of"<<num<<"is"<<cube(num); return 0; } float cube(float a) { return a*a*a; } INPUT: Enter a number:5

## OUTPUT: The cube of 5 is 125

Labhesh Khetawat Page 7

## C++ programming class XI & XII .2011

Q.3.Program to print area of a square. #include<iostream.h> #include<conio.h> void main() { clrscr(); float side,area; cout<<"enter the side of the square:"; cin>>side; cout<<"the area of the square is:"<<side*side<<"square-units"<<endl; }

Output: Enter the side of the square : 5 The area of the square is : 25 square units

Q.4.Program to print area and perimeter of a rectangle. #include<iostream.h> #include<conio.h> void main() { int area,perimeter,length,breadth; cout<<"enter the length and breadth of the the rectangle:";
Labhesh Khetawat Page 8

## C++ programming class XI & XII .2011

cin>>length>>breadth; area=length*breadth; perimeter=2*(length+breadth); cout<<"the area of the rectangle is:"<<area; cout<<"the perimeter of the rectangle is:"<<perimeter; }

Output: Enter the lenght and breadth of the rectangle: 3 5 The area of the rectangle is : 15 The perimeter of rectangle is : 16

Q.5.Program to enter a character and print next fout characters. #include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"\nEnter the character between A and V:"; cin>>ch; int num=ch; cout<<"\nNext four character are:\n"; cout<<(char)(ch+1)<<"\n"
Labhesh Khetawat Page 9

## C++ programming class XI & XII .2011

<<(char)(ch+2)<<"\n" <<(char)(ch+3)<<"\n" <<(char)(ch+4)<<"\n"; } INPUT: Enter the character between A and V:N

## OUTPUT: Next four characters are: O P Q R

Q.6.Program to enter a number and print no to the power #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; cout<<"\nEnter the value of n:";
Labhesh Khetawat

2,3,4 and 5.

Page 10

## C++ programming class XI & XII .2011

cin>>n; cout<<"\nthe square of n:"<<pow(n,2); cout<<"\n the cube of n:"<<pow(n,3); cout<<"\n the value of n^4:"<<pow(n,4); cout<<"\n the value of n^5:"<<pow(n,5); } INPUT: Enter the value of n:2

OUTPUT: the square of n:4 the cube of n:8 the value of n^4:16 the value of n^5:32

Operators and expression Q.1.Program to calculate salary of a person on the basis of his experience and age using conditional operator. #include<iostream.h> #include<conio.h> void main()
Labhesh Khetawat Page 11

## C++ programming class XI & XII .2011

{ clrscr(); int exper,age,salary; cout<<"\nIs person the experienced?(enter 1 for yes and 0 for no):"; cin>>exper; cout<<"\nEnter age of the person:"; cin>>age; salary=exper?((age>35)?6000:(age>28)?4800:3000):2000; cout<<"\nThe salary of the person is:"<<salary; }

Output: Is the person experinced ? : 1 Enter the age of the person : 92 The salary of the person is : 6000

Q.2.Progarm to calculate no of teams and left overs from given no of players. #include<iostream.h> #include<conio.h> void main() { clrscr(); int no_of_players,no_of_teams,left_overs; cout<<"\nEnter the no of players:";
Labhesh Khetawat Page 12

## C++ programming class XI & XII .2011

cin>>no_of_players; no_of_teams=no_of_players/5; left_overs=no_of_players%5; cout<<"\nThere will be "<<no_of_teams<<" teams with "<<left_overs<<" left overs"; }

Output: Enter the no. of players : 11 There will be 2 teams with 1 left overs.

## The result of given expression is : -91.1963

Q.3.Program to raise x to power n. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int x,n; cout<<"\nEnter the base and exponent:"; cin>>x>>n; cout<<"\n"<<x<<" to the power "<<n<<" is "<<pow(x,n); }
Labhesh Khetawat Page 13

## Output: Enter the base and exponent : 2 4 2 to the power 4 is 16

Q.4.Program to print square if no is even otherwise print cube. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; cout<<"\nEnter the number:"; cin>>n; if(n%2==0) cout<<"\nThe number is even and its square is "<<pow(n,2); else cout<<"\nThe number is odd and its cube is "<<pow(n,3); }

Output: Enter the no. : 3 The no. is odd and its cube is 27
Labhesh Khetawat Page 14

## C++ programming class XI & XII .2011

Q.5.Program to print square root if number is odd and positive otherwise print number power 5. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n; float y; cout<<"\nEnter the number:"; cin>>n; if((n%2!=0)&&(x>0)) y=sqrt(n); else y=pow(n,5); cout<<y; }

Labhesh Khetawat

Page 15

## C++ programming class XI & XII .2011

Q.6.Program to print area of circle if choice is 1 otherwise print its perimeter. #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int r,ch; cout<<"Enter the radius:"; cin>>r; cout<<"\nEnter the choice:"; cout<<"\n1.Area of circle"; cout<<"\n2.Perimeter of circle"; cin>>ch; if(ch==1) cout<<"\nArea of circle of radius "<<r<<" is "<<3.14*pow(r,2); else cout<<"\nPerimeter of circle of radius "<<r<<" is "<<2*3.14*r; }

## Output: Enter the radius : 5 Enter the choice : 1.Area of circle

Labhesh Khetawat Page 16

## C++ programming class XI & XII .2011

2.Perimeter of circle : 2 Perimeter of circle of radius 5 is 31.4

Flow of control

## Q.1:Write a program to generate the pyramid?

#include<iostream.h> #include<conio.h> void main() { int i,j; clrscr(); cout<<"desired pattern is\n\n"; for(i=1;i<=5;i++) { for(j=1;j<=5-i;j++) cout<<" "; for(j=1;j<=i;j++) cout<<"* "; cout<<"\n"; } getch(); }
Labhesh Khetawat Page 17

## Q.2:Program to generate the pattern?

#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); int n,i,j; cout<<"\nenter the number <=11 for generating the pattern"; cin>>n; cout<<"\ndesired pattern is\n"; for(i=1;i<=n;i++) {
Labhesh Khetawat Page 18

## C++ programming class XI & XII .2011

cout<<"\t"; for(j=1;j<=n-i;j++) cout<<" "; for(j=n;j>=n+1-i;j--) cout<<setw(3)<<j; for(j=n+2-i;j<=n;j++) cout<<setw(3)<<j; cout<<endl; } for(i=1;i<n;i++) { cout<<"\t"; for(j=1;j<=i;j++) cout<<" "; for(j=n;j>=i+1;j--) cout<<setw(3)<<j; for(j=i+2;j<=n;j++) cout<<setw(3)<<j; cout<<endl; } getch(); }

OUTPUT:
Labhesh Khetawat Page 19

## C++ programming class XI & XII .2011

4 434 43234 4321234 43234 434 4

Q.3:Draw a flow chart and write a C++ program to generate first n fibonacci series?

#include<iostream.h> #include<conio.h> void main() { int a,b,next,n,count; clrscr(); cout<<"How many Fibonacci terms required >=2 and <=24?\n"; cin>>n; a=0; b=1; cout<<"\nfirst "<<n<<" fibonacci terms are:\n\n"; cout<<a<<' '<<b; count=2; while(count<n)
Labhesh Khetawat Page 20

## C++ programming class XI & XII .2011

{ next=a+b; cout<<' '<<next; count++; a=b; b=next; } getch(); }

## #include<iostream.h> #include<conio.h> void main() { int n,i,j,space;

Labhesh Khetawat Page 21

## C++ programming class XI & XII .2011

char ch; clrscr();ss cout<<"enter the value of n<=13\n\n"; cin>>n; cout<<"\ndesired 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(); }

## OUTPUT: enter the value of n<=13 4 desired pattren is

Labhesh Khetawat Page 22

## C++ programming class XI & XII .2011

A ABC ABCDE ABCDEFG

## Q.5:Program to find the roots of the quadratic equation?

#include<iostream.h> #include<conio.h> #include<math.h> void main() { float a,b,c,disc,xr1,xr2,img1,img2; clrscr(); cout<<"enter the coefficients\n"; cin>>a>>b>>c; cout<<"\na="<<a<<"b= "<<b<<"c= "<<c<<"\n"; if(a==0.0) { if(b==0.0) cout<<"\nequation is degenerate\n"; else { xr1=c/b;
Labhesh Khetawat Page 23

## C++ programming class XI & XII .2011

cout<<"linear equation has single root\n"; cout<<"\nroot= "<<xr1; } } else { disc=b*b-4.0*a*c; if(disc>0.0) { cout<<"\nreal and dinstinct roots\n"; xr1=(-b+sqrt(disc))/(2.0*a); xr2=(-b-sqrt(disc))/(2.0*a); cout<<"\nfirst root= "<<xr1; cout<<"\n\nsecond root= "<<xr2; } else { if(disc==0.0) { cout<<"\nreal and equal root\n"; xr1=-b/(2.0*a); xr2=xr1; cout<<"\n\nfirst root = "<<xr1; cout<<"\n\nsecond root = "<<xr2;
Labhesh Khetawat Page 24

## C++ programming class XI & XII .2011

} else { cout<<"\ncomplex conjugate roots\n"; xr1=-b/(2.0*a); img1=sqrt(-disc)/(2.0*a); xr2=xr1; img2=img1; cout<<"\nfirst root is:\n"; cout<<"\nreal part= "<<xr1; cout<<"\img.part= "<<img1; cout<<"\n\nsecond root is:\n"; cout<<"\n real part= "<<xr2; cout<<"/nimg.part= "<<img2; } } } }

## OUTPUT: enter the coefficients 1 -5

Labhesh Khetawat Page 25

## C++ programming class XI & XII .2011

6

a=1 b=-5 c=6 real and distinct roots first root=3 second root=2

## Standard library functions

Q.1. write a program that checks whether the given character is alphanumeric or a digit #include<iostream.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void main() { clrscr(); char ch; int a; cout<<"\n enter the character:\n"; cin>>ch; a=ch;
Labhesh Khetawat Page 26

## C++ programming class XI & XII .2011

if(isalnum(a)) { cout<<"\n alphanumeric chracter\n"; if(isdigit(a)) cout<<"\n and digit charcter\n"; else if(isalpha(a)) cout<<"\n and alphabetic chracter::\n"; } else cout<<"\n other character\n"; getch(); } output: enter the character:d alphanumeric chracter and alphabetic character

Q.2.write a program that checks whether the given character is an alphabet or not.if it is an alphabet,whether its lowercase character or uppercase character? #include<iostream.h> #include<conio.h> #include<ctype.h> void main()
Labhesh Khetawat Page 27

## C++ programming class XI & XII .2011

{ clrscr(); char ch; cout<<"\n enter the character:\n"; cin>>ch; if(isalpha(ch)) { if (islower(ch)) cout<<"\n lowercase\n"; else if(isupper(ch)) cout<<"\n uppercase\n"; } else cout<<"\n other character\n"; getch(); } output: enter the character:52 other case enter the character:d lowercase

Q.3.write a program that reads two string and appends the #include<iostream.h>
Labhesh Khetawat

Page 28

## C++ programming class XI & XII .2011

#include<conio.h> #include<string.h> void main () { clrscr(); char str1[25],str2[50]; cout<<"\n enter first string\n"; cin.getline(str1,25); cout<<"\n enter second string\n"; cin.getline(str2,50); strcat(str2,str1); int len=strlen(str2); cout.write(str2,len); } output: enter first string: labhesh enter second string: is in class. labhesh is in class.

Q.4.write a program that reads two strind and copies the smaller string into the bigger string #include<iostream.h> #include<conio.h> #include<ctype.h> #include<string.h>
Labhesh Khetawat Page 29

## C++ programming class XI & XII .2011

void main() { clrscr(); char str1[25],str2[25]; int len1,len2; cout<<"\n enter first string"; cin.getline(str1,25); cout<<"\nenter second string"; cin.getline(str2,25); if(strlen(str1)>strlen(str2)) { strcpy(str1,str2); cout<<"\n second string is copied to first string\n"; len1=strlen(str1); cout.write(str1,len1); } else if(strlen(str2)>strlen(str1)) { strcpy(str2,str1); cout<<"\n first string is copied to second string\n"; len2=strlen(str2); cout.write(str2,len2); } else if(strlen(str2)==strlen(str1)) {
Labhesh Khetawat Page 30

## C++ programming class XI & XII .2011

cout<<"\n strings are equal\n"; len1=strlen(str1); cout<<"\nstrig 1 is"; cout.write(str1,len1); cout<<"\nstring2 is :"; len2=strlen(str2); cout.write(str2,len2); } } output: enter first string: love the way enter second string:dream ur way string are equal string one is: love the way string second is: dream ur way

Q.5.write a program that converts lower case letters in uppercase & vice versa. #include<iostream.h> #include<conio.h> #include<ctype.h> #include<string.h> void main() {
Labhesh Khetawat Page 31

## C++ programming class XI & XII .2011

clrscr(); char str[30]; cout<<" enter a string:\n"; cin.getline(str,30);

for(int i=0;str[i]!='\0';i++) { if(isupper(str[i])) str[i]=tolower(str[i]); else if (islower(str[i])) str[i]=toupper(str[i]); } int x=strlen(str); cout.write(str,x); } output: enter a string: dream it do it DREAM IT DO IT

User defined functions Q.1.write program to print cube of a given number using a function.
Labhesh Khetawat Page 32

## C++ programming class XI & XII .2011

#include<iostream.h> #include<conio.h> int main() { clrscr(); float cube(float);float x,y; cout<<"enter number whose cube is to be calculated:"; cin>>x; y=cube(x); cout<<"the cube of "<<x<<" is "<<y; getch (); return 0; } float cube(float a) { float n; n=a*a*a; return n; }

## output: enter number whose cube is to be calculated:3 the cabe of 3 is 27

Labhesh Khetawat Page 33

## C++ programming class XI & XII .2011

Q.2.write a program to illustrate call by value methid of function invoking. #include<iostream.h> #include<conio.h> void main() { clrscr(); int change(int); int orig=10; cout<<"\nthe original value is:"<<orig; cout<<"\nthe return of function change() is"<<change(orig); cout<<"\nthe value after function change() is over "<<orig; getch (); } int change(int a) { a=20; return a ; }

output: The orignal value is :10 the return of function change() is 20 the value after function change() is over 10
Labhesh Khetawat Page 34

## C++ programming class XI & XII .2011

Q.3. write a program to swap values using call by value method. #include<iostream.h> #include<conio.h> int main() { clrscr(); void swap(int,int); int a,b; cout<<"\nenter the value of a:"; cin>>a; cout<<"\nenter the value of b:"; cin>>b; cout<<"\noriginal value are:"; cout<<"a="<<a<<",b="<<b; swap(a,b); cout<<"\nthe value after swap() are:"; cout<<"a="<<a<<",b="<<b; getch (); return 0; } void swap(int x,int y) { int temp;
Labhesh Khetawat Page 35

## C++ programming class XI & XII .2011

temp=x; x=y; y=temp; cout<<"\nswapped values are:"; cout<<"a="<<x<<",b="<<y; }

output: enter the value of a:3 enter the value of b:5 original value are:a=3,b=5 swapped values are:a=5,b=3 the value after swap() are:a=3,b=5

Q.4.write a program to convert distance in feet or inches using a call by reference method. #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); void convert(float &,char&,char); float distance; char choice,type='f';
Labhesh Khetawat Page 36

## C++ programming class XI & XII .2011

cout<<"\nenter the distance in feet:"; cin>>distance; cout<<"\nyou want distance in feet or inches?(f/i):"; cin>>choice; switch(choice) { case'f':convert(distance,type,'f'); break; case'i':convert(distance,type,'i'); break; default:cout<<"you have entered a wrong choice!!!"; exit(0); } cout<<"distance="<<distance<<" "<<type; getch (); } void convert(float &d,char &t,char ch) { switch(ch) { case'f':if(t=='i') { d=d/12; t='f';
Labhesh Khetawat Page 37

## C++ programming class XI & XII .2011

} break; case'i':if(t=='f') { d=d*12; t='i'; } break; } return ; }

output: enter the distance in feet:6 you want distance in feet or inches?(f/i):f distance=6f enter the distance in feet:6 you want distance in feet or inches?(f/i):i distance=72i

Q.5.write a program to check weather a given character is contained in a string or not and find its position. #include<iostream.h>< #include<conio.h>
Labhesh Khetawat Page 38

## C++ programming class XI & XII .2011

int main() { clrscr(); int findpos(char s[],char c); char string[80],ch; int y=0; cout<<"\nenter the main string:"; cin.getline(string,80); cout<<"\nenter character to be searched for:"; cin.get(ch) ; y=findpos(string,ch); if(y==-1) cout<<"\nsorry!!!character is not in the string:"; getch (); return 0; } int findpos(char s[],char c) { int flag=-1; for(int i=0;s[i]!='\0';i++) { if(s[i]==c) { flag=0;
Labhesh Khetawat Page 39

## C++ programming class XI & XII .2011

cout<<"\nthe character is in the string at position"<<i+1; } } return(flag); } output: enter the main string: I am great enter the character to be searched for:g the character is in the string at position 6 Arrays

Q.1.Program that reads two matrices A and B of order m*n and computes C=A+B where C is the third matrix of order m*n.

#include<iostream.h> #include<conio.h> void main() { clrscr (); int i,m,n,j; cout<<"\nenter the values for m & n:"; cin>>m>>n; int a[5][5],b[5][5],c[5][5]; cout<<"\nenter the elements of matrix a:";
Labhesh Khetawat Page 40

## C++ programming class XI & XII .2011

for(i=1;i<=m;i++) { for(j=1;j<=n;j++) cin>>a[i][j]; } cout<<"\nenter the elements of matrix b:"; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) cin>>b[i][j]; } cout<<"\nthe resultant matrix is.........."; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { c[i][j]=a[i][j]+b[i][j]; cout<<c[i][j]<<"\n"; } cout<<"\n"; } }

OUTPUT:
Labhesh Khetawat Page 41

## C++ programming class XI & XII .2011

enter the values for m & n:2 enter the elements of matrix a:1 2 3 4

enter the values for m & n:2 enter the elements of matrix b:1 2 3 4

## Q.2.Write a program to transpose a matrix.

#include<iostream.h> #include<conio.h>
Labhesh Khetawat Page 42

## C++ programming class XI & XII .2011

void main() { clrscr(); int a[20][20],b[20][20],i,j,m,n; cout<<"\nenter number of rows & columns of matrix:"; cin>>m>>n; cout<<"\ninput matrix:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>a[i][j]; } cout<<"\n\tmatrix"; for(i=0;i<m;i++) { cout<<"\n"; for(j=0;j<m;j++) cout<<" "<<a[i][j]; } for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=a[j][i]; }
Labhesh Khetawat Page 43

## C++ programming class XI & XII .2011

cout<<"\n\ttranspose of matrix:"; for(i=0;i<n;i++) { cout<<"\n"; for(j=0;j<m;j++) cout<<" "<<b[i][j]; } }

Output: Enter the number of rows and coloumns of matrix: 3 3 Input matrix: 1 2 3 4 5 6 7 8 9

Matrix: 1 2 3 4 5 6 7 8 9

Transpose: 1 4 7 2 5 8
Labhesh Khetawat Page 44

## C++ programming class XI & XII .2011

3 6 9

Q.3.Write a c++ program to find sum of the elements of rows and column of matrix.

#include<iostream.h> #include<conio.h> void main () { clrscr (); int a[10][10],m,n,i,j,r[10],c[10]; cout<<"\nenter the no. of rows and column of a matrix:"; cin>>m>>n; cout<<"\nenter the matrix:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>a[i][j]; } cout<<"\n\tmatrix:"; for(i=0;i<m;i++) { cout<<"\n"; for(j=0;j<n;j++)
Labhesh Khetawat Page 45

## C++ programming class XI & XII .2011

cout<<" "<<a[i][j]; } for(i=0;i<m;i++) { r[i]=0; for(j=0;j<n;j++) r[i]+=a[i][j]; } for(j=0;j<n;j++) { c[j]=0; for(i=0;i<m;i++) c[j]+=a[i][j]; } cout<<"\n"; for(j=0;j<m;j++) cout<<"\nthe sum of column"<<j+1<<"is:"<<c[j]; for(i=0;i<n;i++) cout<<"\nthe sum of"<<i+1<<"row is:"<<r[i]; }

OUTPUT:

Labhesh Khetawat

Page 46

## C++ programming class XI & XII .2011

Enter the no. of rows and coloumns of a matrix: 2 2

## Enter the matrix: 1 2 3 4

Matrix: 1 2 3 4

The sum of coloumn1 is : 4 The sum of coloumn2 is : 6 The sum of row1 is : 3 The sum of row2 is : 7

Q.4.Write a c++ program that reads two matrix A[]m*n and B[]m*n and compare them for equality.

## #include<iostream.h> #include<conio.h> void main () { clrscr (); int a[10][10],b[10][10],m,n,i,j,flag=0;

Labhesh Khetawat Page 47

## C++ programming class XI & XII .2011

cout<<"enter the rows and column of two matrices:"; cin>>m>>n; cout<<"input the first matrix:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>a[i][j]; } cout<<"\ninput the second matrix:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>b[i][j]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(a[i][j]!=b[i][j]) { flag=1; break; } }
Labhesh Khetawat Page 48

## C++ programming class XI & XII .2011

if(flag==1) break; } if(!flag) cout<<"\n\tthe matrices are equal:"; else cout<<"\n\tthe matrices are unequal:"; }

OUTPUT:

Enter the rows and coloumns of two matrices: 2 2 Input the first matrix: 1 2 3 4 Input the second matrix: 1 2 3 4

## #include<iostream.h> #include<conio.h> int main()

Labhesh Khetawat Page 49

## C++ programming class XI & XII .2011

{ clrscr(); int vector[10],i,p; cout<<"enter elements of vector(max 10)\n"; for(i=0;i<10;i++) cin>>vector[i]; cout<<"the vector orignally is as follows :\n"; for(i=0;i<10;i++) cout<<" "<<vector[i]; for(i=0,p=10-1;i<10/2;i++,p--) { vector[i]=vector[i]+vector[p]; vector[p]=vector[i]-vector[p]; vector[i]=vector[i]-vector[p]; } cout<<"\nthe reverse vector is as follows:\n"; for(i=0;i<10;i++) cout<<" "<<vector[i]; return 0; }

## OUTPUT: enter elements of vector(max 10) 1 2 3

Labhesh Khetawat Page 50

## C++ programming class XI & XII .2011

4 5 6 7 8 9 0 the vector origanally is as follows: 1234567890 the reverse vector is as follows: 0987654321

Structures Q.1.Program to read values in nested structure. #include<iostream.h> #include<conio.h> #include<stdio.h> struct addr { int houseno ; char area[6]; char city[6]; char state[6]; };
Labhesh Khetawat Page 51

## C++ programming class XI & XII .2011

struct emp { int empno ; char name[6]; char desig[4]; addr address ; float basic ; } worker ; void main() { clrscr(); cout<<"\nEnter employee no :"<<"\n"; cin>>worker.empno; cout<<"\n"<<"Name:"; gets(worker.name); cout<<"\n"<<"Designation:"; gets(worker.desig) ; cout<<"\n Enter address :"<<"\n"; cout<<"House no :"; cin>>worker.address.houseno; cout<<"\n"<<"Area"; gets(worker.address.area); cout<<"\n"<<"City"; gets(worker.address.state);
Labhesh Khetawat Page 52

## C++ programming class XI & XII .2011

cout<<"\n"<<"State :"; gets(worker.address.state) ; cout<<"\n"<<"Basic pay :"; cin>>worker.basic; } INPUT: Enter employee no :11 Name:Labhesh Designation: Senior Manager Enter address: House no:23 Area:xyz City:Gotan State:Rajasthan Basic pay:50000

Q.2.Program to add two lenghts given in feet and inches. #include<iostream.h> #include<conio.h> struct distance { int feet; int inches; };
Labhesh Khetawat Page 53

## C++ programming class XI & XII .2011

void main() { clrscr; distance length1,length2; void prnsum(distance & l1,distance & l2); cout<<"\nEnter length 1:"<<"\n"; cout<<"\nfeet:"; cin>>length1.feet; cout<<"\ninches:"; cin>>length1.inches; cout<<"\n\nEnter length2:"<<"\n"; cout<<"\nfeet:"; cin>>length2.feet; cout<<"\ninches:"; cin>>length2,inches; prnsum(length1,length2); } void prnsum(distance & l1,distance & l2) { distance l3; l3.feet = l1.feet+l2.feet+(l1.inches+l2.inches)/12; l3.inches=(l1.inches+l2.inches)%12; cout<<"\n\nTotal feet:"<<l3.feet<<"\n"; cout<<"Total inches:"<<l3.inches;
Labhesh Khetawat Page 54

## C++ programming class XI & XII .2011

} INPUT: Enter lenght 1: feet:23 inches:34 Enter lenght 2: feet:45 inches:21

## OUTPUT: Total feet:72 Total inches:7

Q:3.Write a c++ program by using structures to print all the months in a year with days.

#include<iostream.h> #include<conio.h> #include<stdio.h> struct year { char month[12]; int days; }; year table[12]={{"january",31},
Labhesh Khetawat Page 55

## C++ programming class XI & XII .2011

{"february",28}, {"march",31}, {"april",30}, {"may",31}, {"june",30}, {"july",31}, {"august",31}, {"september",30}, {"october",31}, {"november",30}, {"december",31} }; void main() { clrscr(); for(int i=0;i<12;i++) { cout<<"\n"; cout<<table[i].month<<" "<<table[i].days; } }

OUTPUT: january,31
Labhesh Khetawat Page 56

## C++ programming class XI & XII .2011

february,28 march,31 april,30 may,31 june,30 july,31 august,31 september,30 october,31 november,30 december,31

MATRICES

//Q:1.write the c++ program to find the largest & smallest elements in a vector.

## #include<iostream.h> #include<conio.h> void main() {

Labhesh Khetawat Page 57

## C++ programming class XI & XII .2011

clrscr(); int v[50],large,small; int i,n; cout<<"enter how many elements(max 50) ?"; cin>>n; cout<<"\nenter the values in vector"; for(i=0;i<n;i++) cin>>v[i]; large=v[i]; small=v[i]; for(i=0;i<n;i++) { if(v[i]>large) large=v[i]; if(v[i]<small) small=v[i]; } cout<<"\nlargest element="<<large; cout<<"\nsmallest element="<<small;

## OUTPUT: Enter the element=1,2,3 Largest element=3

Labhesh Khetawat Page 58

## //Q:2.Write a program to calculate length of a string without using a library function.

#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str[80]; int i; cout<<"\nenter any string:"; gets(str); for(i=0;str[i]!='\0';++i); cout<<"\nlength of the string is:"<<i; getch(); }

OUTPUT: Enter the string: labhesh The length of the string is:7

Labhesh Khetawat

Page 59

## C++ programming class XI & XII .2011

//Q:3.Program to delete a substring from a string. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str[30]; int i,p,n,len; cout<<"\nenter the string:"; gets(str); for(len=0;str[len]!='\0';++len); cout<<"\nenter the starting position of substring to be deleted:"; cin>>p; if(p>len) { cout<<"\nenetr bthe number of characters of substring to be deleted:"; cin>>n; if(n+p>=len) n=len-p+1; for(i=p-1;str[i]!='\0';++i) str[i]=str[i+n]; cout<<"\nstring after deletion of sub string is:"<<str; getch();
Labhesh Khetawat Page 60

## C++ programming class XI & XII .2011

} }

OUTPUT: Enter the string: labhesh Enter the positon of the string to be deleted: 4 The string is: labesh

## //Q:4.program to concatenate two strings.

#include<iostream.h> #include<conio.h> #include<string.h> int main() { clrscr(); char str1[25],str2[25],str3[50]; int i, k,x1; cout<<"\nenter the first string:\n"; cin.getline(str1,25); cout<<"\nenter the second string:\n"; cin.getline(str2,25);
Labhesh Khetawat Page 61

## C++ programming class XI & XII .2011

for(i=0;str1[i]!='\0';++i) str3[i]=str1[i]; for(k=0;str2[k]!='\0';++k) str3[i]=str1[i]; str3[i+k]='\0'; x1=strlen(str3); cout<<"\nthe concatenated string is:\n"; cout.write(str3,x1); return 0; }

OUTPUT: Enter the first string: tanishk Enter the second string: ankit The concated string is: tanishk

## #include<iostream.h> #include<conio.h> #include<string.h>

Labhesh Khetawat Page 62

## C++ programming class XI & XII .2011

#include<process.h> int main() { clrscr(); char mainstr[50],substr[50]; int count,pos,i,j,len,num,x1; cout<<"\nenter the main string\n"; cin.getline(mainstr,50); len=strlen(mainstr); cout<<"\nenter the starting position of the substring\n"; cin>>pos; if(pos>len) { cout<<"\nstarting position exceeds the total" <<"length of the string!!!"; exit(0); } cout<<"\nthe number of the characters in the string?\n"; cin>>count; if(pos<=0) { cout<<"\n\nextracted string is empty\!!!n"; exit(0); } else if(((pos + count)-1)>len) { cout<<"\n\nstring to be extracted exceeds length\n";
Labhesh Khetawat Page 63

## C++ programming class XI & XII .2011

num=(len-pos); } else { num=count; cout<<"\nnum"<<num; } j=0; for(i=--pos;i<=(pos+num);++i) { substr[j]=mainstr[i]; j++; } cout<<"\nsubstring is\n"; cout.write(substr,j); return 0; }

OUTPUT: Enter the string:labhesh Enter the starting position of the substring:1 Number of characters in the string:7 Num7
Labhesh Khetawat Page 64

## C++ programming class XI & XII .2011

Substring is labhesh

CLASS 12

## Classes and objects

Q:1. Program to illustrate the use of object arrays by storing details of 10 items in an array of object.

## #include<iostream.h> #include<conio.h> class item{

Labhesh Khetawat Page 65

## C++ programming class XI & XII .2011

int itemno; float price; public: void getdata(int i,float j) { itemno=i; price=j; } void putdata(void) { cout<<"ittem no:"<<itemno<<"\t"; cout<<"price:"<<price<<"\n"; } }; const int size=10; item order[size]; int main() { clrscr(); int ino; float cost; for(int a=0;a<size;a++) { cout<<"enter itemno & price for item"<<a+1<<"\n";
Labhesh Khetawat Page 66

## C++ programming class XI & XII .2011

cin>>ino>>cost; order[a].getdata(ino,cost); }

## for(a=0;a<size;a++) { cout<<"item"<<a+1<<"\n"; order[a].putdata(); } return 0; }

OUTPUT: enter itemno and price for item1 1 50 enter itemno and price for item2 2 100 enter itemno and price for item3 3 150

item 1
Labhesh Khetawat Page 67

## C++ programming class XI & XII .2011

item no:1 item 2 item no:2 item 3 item no:3 price 150 price 100 price 50

## Q:1.Program to illustrate order of constructor invocation.

#include<iostream.h> #include<conio.h> class subject{ int days; int subjectno; public: subject(int d=123,int sn=101); void printsub(void) { cout<<"subjec no:"<<subjectno; cout<<"\n"<<"days:"<<days<<"\n"; } };
Labhesh Khetawat Page 68

## C++ programming class XI & XII .2011

subject::subject(int d,int sn) { cout<<"construct subject\n"; days=d; subjectno=sn; } class student{ int rollno; float marks; public: student() { cout<<"constructing student\n"; rollno=0; marks=0.0; } void getval(void) { cout<<"\nenter rollno and marks:"; cin>>rollno>>marks; cout<<"\n"; } void print(void) { cout<<"rollno no:"<<rollno;
Labhesh Khetawat Page 69

## C++ programming class XI & XII .2011

cout<<"\n"<<"marks:"<<marks<<"\n"; } }; class admission{ subject sub; student stud; float fees; public: admission() { cout<<"\nconstructing admission "; fees=0.0; } void print(void) { stud.print(); sub.printsub(); cout<<"fees:"<<fees<<"\n"; } }; void main() { clrscr(); admission adm;
Labhesh Khetawat Page 70

## C++ programming class XI & XII .2011

cout<<"\n Back in main()\n"; }

## Constucting admission Back in main()

Q:2.program to illustrate working of default arguments.calculate interest amount making use of default arguments.

#include<iostream.h> #include<conio.h> void amount(float princ,int time=2,float rate=0.08); void amount(float princ,int time,float rate) { cout<<"\npricipal amount:rs"<<princ; cout<<"\t time:"<<time<<"years"; cout<<"\t rate:"<<rate; cout<<"\n interested amount:"<<(princ*rate*time)<<endl; }
Labhesh Khetawat Page 71

## C++ programming class XI & XII .2011

void main() { clrscr(); cout<<"case1";amount(2000); cout<<"case2"; amount(2500,3); cout<<"case3"; amount(2300,3,0.11); cout<<"case4"; amount(2500,0.08) }

OUTPUT: case 1: principal amount:RS 2000 interest amount: 320 case 2: principal amount:RS 2500 interest amount: 600 case 3: principal amount:RS 2300 interest amount: 759 case 4: principal amount:RS 2500
Labhesh Khetawat

time:2years

rate:0.08

time:3years

rate:0.08

time:3years

rate:0.11

time:0years

rate:0.08
Page 72

## C++ programming class XI & XII .2011

interest amount: 0

Inheritance

## Q:1.Program to show working of virtual base classes?

#include<iostream.h> #include<conio.h> class base { public: int a; }; class d1:virtual public base { public: int b; }; class d2:virtual public bases { public: int c; };
Labhesh Khetawat Page 73

## C++ programming class XI & XII .2011

class d3:public d1,public d2 { public: int total; }; int main() { clrscr(); d3 ob; ob.a=35; ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total<<endl; return 0; }

OUTPUT: 35 50 75 160

Labhesh Khetawat

Page 74

## C++ programming class XI & XII .2011

Q:2.Program to check working of constructors and destructors in multiple inheritance?

#include<iostream.h> #include<conio.h> class base1 { protected: int a; public: base1(int x) { a=x; cout<<"constructing base1\n"; } ~base1() { cout<<"destructing base 1\n"; } }; class base2 { protected: int b; public:
Labhesh Khetawat Page 75

## C++ programming class XI & XII .2011

base2(int y) { b=y; cout<<"constructing base2\n"; } ~base2() { cout<<"destructing base2\n"; } }; class derived:public base2,public base1 { int c; public: derived(int i,int j,int k):base2(i),base1(j) { c=k; cout<<"constructing derived.\n"; }; ~derived() { cout<<"destructing derived.\n"; } void show()
Labhesh Khetawat Page 76

## C++ programming class XI & XII .2011

{ cout<<"1."<<a<<"\t2."<<b<<"\t3."<<c<<"\n"; } }; int main() { clrscr(); derived ob(14,15,16); ob.show(); return 0; }

OUTPUT: Constructing base 2 constructing base 1 constructing derived 1.15 2.14 3.16

Labhesh Khetawat

Page 77

## C++ programming class XI & XII .2011

Q.1. Program to write and read a structure using write() and read() function using a binary file.

#include<fstream.h> #include<conio.h> #include<string.h> struct customer{ char name[51]; float balance; }; void main() { clrscr(); customer savac; strcpy(savac.name,"Akshat Khemka"); savac.balance=9876543.21; ofstream fout; fout.open("saving",ios::out|ios::binary); if(!fout) { cout<<"\n File cannot be opened\n"; } fout.write((char*)&savac,sizeof(savac));
Labhesh Khetawat Page 78

## C++ programming class XI & XII .2011

fout.close(); ifstream fin; fin.open("saviong",ios::in|ios::binary); fin.read((char*)&savac,sizeof(savac)); cout<<savac.name<<" has the balance amount of Rs."<<savac.balance<<"\n"; fin.close(); }

OUTPUT: file cannot be opened labhesh khetawat has the balance amount of RS 987545

Q.2. Program to create a single file and then display its contents.

#include<fstream.h> #include<conio.h> void main() { clrscr(); ofstream fout("student"); char name[30],ch; float marks=0.0; for(int i=0;i<5;i++)
Labhesh Khetawat Page 79

## C++ programming class XI & XII .2011

{ cout<<"Student:"<<(i+1)<<"\tname:"; cin.get(name,30); cout<<"\t\tMarks:"; cin>>marks; cin.get(ch); fout<<name<<"\n"<<marks<<"\n"; } fout.close(); ifstream fin("student"); fin.seekg(0); cout<<"\n"; for(i=0;i<5;i++) { fin.get(name,30); fin.get(ch); fin>>marks; fin.get(ch); cout<<"\n Student name:"<<name; cout<<"\t Marks:"<<marks<<"\n"; } fin.close(); getch(); }
Labhesh Khetawat Page 80

## C++ programming class XI & XII .2011

OUTPUT: student: 1 name:labhesh marks:76 student: 2 name:akshat marks:89 student: 3 name:raj marks:65 student: 4 name:g.one marks:75 student: 5 name:ra.one marks:99

## Name:labhesh marks:76 Name:akshat marks:89 Name:raj Name:g.one marks:65 marks:78

Name:raone marks:98

Q.3. A file named marks.dat already stores some students details like rollno and marks. Write a program that reads more such details and append them to this file. Make sure that previous contents of the the file are not lost.
Labhesh Khetawat Page 81

## C++ programming class XI & XII .2011

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { clrscr(); ofstream fout; fout.open("marks.dat",ios::app); char ans='y'; int rollno; float marks; if(ans=='y'||ans=='Y') { cout<<"\nEnter Rollno.:"; cin>>rollno; cout<<"\nEnter marks:"; cin>>marks; fout<<rollno<<"\n"<<marks<<"\n"; cout<<"\nWant to enter more records?(y/n)"; cin>>ans; } else exit(0);
Labhesh Khetawat Page 82

## C++ programming class XI & XII .2011

fout.close(); getch(); }

OUTPUT:

## Q.4. Program to display content of a file using get() function

#include<fstream.h> #include<conio.h> void main() { clrscr(); char ch; ifstream fin; fin.open("marks.dat",ios::in); if(!fin) { cout<<"\nCannot open file!!!\n";
Labhesh Khetawat Page 83

## C++ programming class XI & XII .2011

} while(fin) { fin.get(ch); cout<<ch; } fin.close(); getch(); }

OUTPUT: 1 56 43 345 5 65

## #include<fstream.h> #include<conio.h> void main() {

Labhesh Khetawat Page 84

## C++ programming class XI & XII .2011

clrscr(); ofstream fout; fout.open("aschars",ios::app); if(!fout) { cout<<"\n The file cannot be created.\n"; } char ch; int line=0; for(int i=65;i<70;i++) { fout.put((char)i); } fout.close(); ifstream fin; fin.open("ankit",ios::in); fin.seekg(0); for(i=65;i<70;i++) { fin.get(ch); cout<<" "<<i<<"="; cout.put((char)i); if(!(i%8)) {
Labhesh Khetawat Page 85

## C++ programming class XI & XII .2011

cout<<endl; line++; } if(line>22) { getch(); line=0; } } }

## OUTPUT: file contains: 65=A 66=B 67=C 68=D 69=E

Pointers

Q.1:Program to read the 2-D array alongwith rowsum and column sum and display its alogorithm?

Labhesh Khetawat

Page 86

## C++ programming class XI & XII .2011

#include<iostream.h> #include<conio.h> void main() { clrscr(); int *val,*rsum,*csum; int maxr,maxc,i,j; cout<<"enter dimensions(row col):"; cin>>maxr>>maxc; val=new int[maxr*maxc]; rsum=new int[maxr]; csum=new int[maxc]; for(i=0;i<maxr;i++) { cout<<"\n enter the elements of row"<<i+1<<":"; rsum[i]=0; for(j=0;j<maxc;j++) { cin>>val[i*maxc+j]; rsum[i]+=val[i*maxc+j]; } } for(j=0;j<maxc;j++) {
Labhesh Khetawat Page 87

## C++ programming class XI & XII .2011

csum[j]=0; for(i=0;i<maxr;i++) csum[j]+=val[i*maxc+j]; } cout<<"\n\nenter the elements along with rowsum and columnsumis:\n\n"; for(i=0;i<maxr;i++) { for(j=0;j<maxc;j++) cout<<val[i *maxc +j]; cout<<rsum[i]<<endl; } for(j=0;j<maxc;j++) cout<<csum[j]; cout<<endl; }

Labhesh Khetawat

Page 88

## C++ programming class XI & XII .2011

enter the elements of row 2:1 1 1

## enter the elements of row 3:1 1 1

enter the elements along with rowsum and columnsum is: 1113 1113 1113 333

Q.2:Exchange the positions of the string stored in array using array of pointers?

## #include<iostream.h> #include<conio.h> #include<string.h> void main()

Labhesh Khetawat Page 89

## C++ programming class XI & XII .2011

{ clrscr(); char *names[]={"labhesh","kapil","ajay","abhishek","Anil"}; int len=0; len=strlen(names[1]); cout<<"\n originally string 2 is "; cout.write(names[1],len).put('\n'); cout<<"and string 4 is "; cout.write(names[3],len).put('\n'); char *t; t=names[1]; names[1]=names[3]; names[3]=t; len=strlen(names[1]); cout<<"exchanged string 2 is "; cout.write(names[1],len).put('\n'); cout<<"and string 4 is "; cout.write(names[3],len).put('\n'); }

OUTPUT:

## originally string 2 is kapil and string 4 is abhishek

Labhesh Khetawat Page 90

## C++ programming class XI & XII .2011

exchanged string 2 is abhishek string 4 is kapil

Q.3:Program to search for a given character inside a string and to print the string from the point of match?

#include<iostream.h> #include<conio.h> char *match(char ,char*); void main() { clrscr(); char string[80],*cp,ch; cout<<"enter a string\n"; cin.getline(string,80); cout<<"enter a character to be searched for\n"; cin>>ch; cp=NULL; cp=match(ch,string); if(*cp) { cout<<"\n"; for(;(*cp!='\0');cp++) cout<<*cp;
Labhesh Khetawat Page 91

## C++ programming class XI & XII .2011

} else cout<<"no matchfound!\n"; cout<<"\n"; } char *match(char c,char *s) { while ((c!= *s) && (*s)) s++; return(s); }

OUTPUT:

## Q.4:Program to swap variables by passing pointers?

#include<iostream.h>
Labhesh Khetawat Page 92

## C++ programming class XI & XII .2011

#include<conio.h> void main() { clrscr(); void swap(int *x,int *y); int a=7,b=4; cout<<"original values\n"; cout<<"a="<<a<<",b="<<b<<"\n"; swap(&a,&b); cout<<"swapped values\n"; cout<<"a="<<a<<",b="<<b<<"\n"; } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; }

## OUTPUT: original values a=7,b=4

Labhesh Khetawat Page 93

## C++ programming class XI & XII .2011

swapped values a=4,b=7

## Q.5:write a program to exchange the positon of strings in an array?

#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); char *games[]={"football","cricket","hockey","baseball","golf"}; char *temp; int len=0; len=strlen(games[0]); cout<<"\nstring1 is"; cout.write(games[0],len); cout<<"\nstring4 is"; cout.write(games[3],len); temp=games[0]; games[3]=temp; len=strlen(games[0]); cout<<"\nstring1 is"; cout.write(games[0],len);
Labhesh Khetawat Page 94

## C++ programming class XI & XII .2011

cout<<"\nstring4 is"; cout.write(games[3],len); }

OUTPUT:

## Q:6. Program to check whether the string is palindrome or not.

#include<iostream.h> #include<conio.h> #include<string.h> class strn { char *a, flag; int i,j,k; public: void read(); void ch_pal(); strn()
Labhesh Khetawat Page 95

## C++ programming class XI & XII .2011

{ flag='y'; } }; void strn::read() { cout<<"\n"; cout<<"\nenter the string "; cin>>a; } void strn::ch_pal() { cout<<"\nthe entered string"; for(i=0;(a+i)!='\0';i++) cout<<*(a+i); for(j=0,i-=1;i>j;j++,i--){ if(*(a+i) !=*(a+j)) { flag='n'; break; } } if(flag=='n') cout<<"is not a palindrome";
Labhesh Khetawat Page 96

## C++ programming class XI & XII .2011

else cout<<"is a palindrome"; } void main() { strn x; clrscr(); cout<<"\n"; x.read(); x.ch_pal(); cout<<"\n"; getch (); }

OUTPUT: enter the string labhesh the entered string is not a palindrome

## Arrays Q.1. Linear search technique in a sorted array.

#include<iostream.h> #include<conio.h>
Labhesh Khetawat Page 97

## C++ programming class XI & XII .2011

int linearsearch(int [],int,int); int main() { clrscr(); const int numel=10; int nums[numel]={5,10,22,32,45,67,73,98,99,101}; int item,location; cout<<"Enter the item you are searching for:"; cin>>item; location=linearsearch(nums,numel,item); if(location>-1) cout<<"The item was found at the index else cout<<"The item was not found in the list\n"; return 0; } location:"<<location<<endl;

// This function returns the location of key in the list // a-1 is returned if the value is not found

## int linearsearch(int list[],int size,int key) { for(int i=0;i<size;i++) {

Labhesh Khetawat Page 98

## C++ programming class XI & XII .2011

if (list[i]==key) return i; } return -1; }

OUTPUT: enter the item you are searching for:10 the item was found at the idex location 2 Q.2. Write a C++ program to find the location of an element in an array using search process consedering the element in ascending order using binary search method.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int series[100]; int i,n,pos,x; cout<<"Enter the size of the list\n"; cin>>n; for(i=0;i<n;i++)
Labhesh Khetawat Page 99

## C++ programming class XI & XII .2011

{ cout<<"\n Enter the element "<<i+1<<":"; cin>>series[i]; } int first=0; pos=-1; int last=n-1; cout<<"\n Enter the value to be searched for:"; cin>>x; while((first<=last)&&(pos==-1)) { int middle=(first+last)/2; if(series[middle]==x) pos=middle; else if(series[middle]<x) first=middle+1; else last=middle-1; } if(pos>-1) cout<<"\n The position of the element="<<pos; else cout<<"\n Unsuccesful search.";
Labhesh Khetawat Page 100

## C++ programming class XI & XII .2011

}

OUTPUT: enter the size of the list:3 enter the element 1:5 enter the element 2:7 enter the element 3:9

## enter the value to be searched for:8 search is unsuccessful

Q.3. Write a C++ program to sort the element of an array in ascending order using selection sort.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int list[30]; int small,pos; int temp,i,j,n; cout<<"\n Enter the size of the list:"; cin>>n;
Labhesh Khetawat Page 101

## C++ programming class XI & XII .2011

cout<<"\n Enter the list:"; for(i=0;i<n;i++) { cout<<"\n Enter the data elements for the list"<<i+1<<":"; cin>>list[i]; } for(i=0;i<n-1;i++) { small=list[i]; pos=i; for(j=i+1;j<n;j++) { if(small>list[j]) pos=j; } temp=list[i]; list[i]=list[pos]; list[pos]=temp; } cout<<"\n The sorted list is as follows:"; for(i=0;i<n;i++) { cout<<"\n Data element"<<list[i]; }
Labhesh Khetawat Page 102

## C++ programming class XI & XII .2011

} OUTPUT: enter the size of the list:3 enter the list:1

enter the data elements of list1:3 enter the data elements of list2:9 enter the data elements of list3:7

the sorted list is as follows: data element 3 data element 7 data element 9 Q.4. Write a C++ program to sort the element in descending order using bubble sort.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int list[]={2,13,6,9,8,1,10}; int i=0; int n,j,temp;
Labhesh Khetawat Page 103

## C++ programming class XI & XII .2011

n=sizeof(list)/2; cout<<"\n The given list is:\n"; for(i=0;i<n;i++) { cout<<list[i]<<" "; } i=0; while(i<n-1) { i++; for(j=0;j<n-i;j++) { if(list[j]<list[j+1]) { temp=list[j]; list[j]=list[j+1]; list[j+1]=temp; } } } cout<<"\n The sorted list is:"; for(i=0;i<n;i++) { cout<<list[i]<<" ";
Labhesh Khetawat Page 104

getch(); } }

## Q.5.WRITE A C++ PROGRAM TO DELEATE AN ELEMENT FROM AN ARRAY.

#include<iostream.h> #include<conio.h> void main () { clrscr(); int reg[50]; int i,loc,x,n,back; cout<<"\nenter the size of list......"; cin>>n; for(i=0;i<n;i++) { cout<<"\nenter the data element>";
Labhesh Khetawat Page 105

## C++ programming class XI & XII .2011

cin>>reg[i]; } cout<<"\n enter the location from zero location:"; cin>>loc; x=reg[loc]; back=loc; while(back<n) { reg[back]=reg[back+1]; back++; } n--; cout<<"\nthe deleted data item="<<x; cout<<"\nthe new list after deleation........"; for(i=0;i<n;i++) { cout<<"\ndata element="<<reg[i]; } }

OUTPUT: enter the size of list...4 enter the data element:1 enter the data element:2
Labhesh Khetawat Page 106

## C++ programming class XI & XII .2011

enter the data element:3 enter the data element:4

## enter the location from zero location:2

Q.6.Suppose A,B,C are arraysof integers of size M,N and M+N numbers in array A and B appear in ascending order.Give in neccesary declaration for array A,B and C in C++.Write a program in C++ to produce third array C by merging arrays A and B in ascending order.

respectively.The

#include<iostream.h> #include<conio.h> void main () { int par1[20],par2[20],final[40]; int m,n; int ptr1,ptr2,ptr3; int i; cout<<"\n Enter the Size of lists 1 & 2:"; cin>>n>>m; cout<<"\n Enter the list 1:"; for(i=0;i<n;i++) cin>>par1[i];
Labhesh Khetawat Page 107

## C++ programming class XI & XII .2011

cout<<"\nEnter the list 2"; for(i=0;i<m;i++) cin>>par2[i]; ptr1=ptr2=ptr3=0; while(ptr1<n && ptr2<m) { if(par1[ptr1]<par2[ptr2]) final[ptr3++]=par1[ptr1++]; else final[ptr3++]=par2[ptr2++]; } while(ptr2<m) final[ptr3++]=par2[ptr2++]; while(ptr1<n) final[ptr3++]=par1[ptr1++]; cout<<"\n Final List is........."; for(i=0;i<m+n;i++) cout<<final[i]<<" "; }

## enter the list1:

Labhesh Khetawat Page 108

1 2 3

## ascending order using

#include<iostream.h> #include<conio.h> void main() { clrscr (); int list[30]; int i,j,k,n; int temp; cout<<"\n Enter the size of the List:"; cin>>n;
Labhesh Khetawat Page 109

## C++ programming class XI & XII .2011

cout<<"\n\nEnter the List:"; for(i=0;i<n;i++) { cout<<"\nEnter the data Element>"; cin>>list[i]; } for(i=1;i<n;i++) { temp=list[i]; j=i-1; while((temp<list[j])&&(j>=0)) { list[j+1]=list[j]; j=j-1; } list[j+1]=temp; } cout<<"\nthe sorted list; for(i=0;i<n;i++) { cout<<"\ndata element>"<<list[i]; } }

OUTPUT:
Labhesh Khetawat Page 110

## C++ programming class XI & XII .2011

enter the size of the list:4 enter the list: enter the data element:435 enter the data element:23 enter the data element:67 enter the data element:1

## Q:8.Write a C++ program to insert an element inyo an array in Kth

position.

#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); int reg[50]; int i,n,x,loc,back; cout<<"\nEnter Size Of The List>"; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter the element of the array>"; cin>>reg[i]; }
Labhesh Khetawat Page 111

## C++ programming class XI & XII .2011

cout<<"\nEnter The Value To Be Inserted>"; cin>>x; cout<<"\nEnter The Location From Zero Location>"; cin>>loc; back=n; while(back>loc) { reg[back]=reg[back-1]; back--; } reg[back]=x; cout<<"\nThe Final List After Insertion......."; for(i=0;i<=n;i++) { cout<<"\nElement "<<i<<"="<<reg[i]; } }

OUTPUT: enter the size of the list:3 enter the element of array:1 enter the element of array:2 enter the element of array:3 enter the value to be inserted:4
Labhesh Khetawat Page 112

## C++ programming class XI & XII .2011

enter the location from zero location:2

## linked list and queue

Q:1. Write a program to create and traverse a linked list.The linked list contains data of integer type.

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; };
Labhesh Khetawat Page 113

## C++ programming class XI & XII .2011

void main() { node *first,*temp,*last; int n,i; clrscr(); cout<<"\nEnter how many nodes to be create in the linked list"; cin>>n; first=new node; cout<<"\nEnter the data value of node ->"; cin>>first->data; first->link=NULL; temp=first; for(i=0;i<n;i++) { last= new node; cout<<"\nEnter the data value of node "<<i+1<<"->"; cin>>last-> data; last->link=NULL; temp->link=last; temp=last; } temp=first; clrscr(); cout<<"The linked list value are:\n";
Labhesh Khetawat Page 114

## C++ programming class XI & XII .2011

while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } getch(); }

Output: Enter how many nodes to create in the linked list: 2 Enter the data value of node-> 1 Enter the data value of node1-> 2 Enter the data value of node2-> 3

## The linked list values are : 1 2 3

Q:2.Write a program to create a linked and searched a particular data value of integer data type

Labhesh Khetawat

Page 115

## C++ programming class XI & XII .2011

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; }; void main() { node *first,*temp,*last; int n,i,flag,item; clrscr(); cout<<"\n\tEnter how many nodes to be create in the linked list->"; cin>>n; first=new node; cout<<"\n\t Enter the data value of node 1"; cin>>first->data; first->link=NULL; temp=first; for(i=1;i<n;i++) { last= new node;
Labhesh Khetawat Page 116

## C++ programming class XI & XII .2011

cout<<"\n\t\ Enter the data value of node->"<<i+1<<"->"; cin>>last-> data; last->link=NULL; temp->link=last; temp=last; } temp=first; clrscr(); cout<<"The linked list value are:\n"; while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } flag=0; cout<<"\n\ndata value to be searched-->"; cin>>item; temp=first; while(temp!=NULL) { if(temp->data==item) { flag=1; break;
Labhesh Khetawat Page 117

## C++ programming class XI & XII .2011

} temp=temp->link; } if(flag==1) cout<<"\n\n\tsearch is successful"; else cout<<"\n\n\tsearch is unsuccessful"; getch(); }

Output: Enter how many nodes to create in the linked list: 2 Enter the data value of node1-> 2 Enter the data value of node2-> 7

the linked list value are: 2 7 data value to be searched->2 search is suceesfull

Labhesh Khetawat

Page 118

## C++ programming class XI & XII .2011

Q:3.write a program to create a linked list and insert elements according to users choice?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; }; node *addfirst(node*first,int value); node *addbetween(node*first,int value,int val); node *addlast(node*first,int value); void traverse(node *first); void main() { clrscr(); int i,n, choice,val,value; node *first,*temp,*last; cout<<"enter how many nodes to created in link list->"; cin>>n; first=new node; cout<<"\nenter the element of node 1->";
Labhesh Khetawat Page 119

## C++ programming class XI & XII .2011

cin>>first->data; first->link=NULL; temp=first; for(i=1;i<n;i++) { last=new node; cout<<"\nenter the element of node "<<i+1<<"->"; cin>>last->data; last->link=NULL; temp->link=last; temp=last; } do { cout<<"\nmain menu"; cout<<"\n1.inserting at first"; cout<<"\n2.inserting at between"; cout<<"\n3.insert at last"; cout<<"\n4.transversing at last"; cout<<"\n5.exit"; cout<<"\nenter your choice->"; cin>>choice; switch(choice) {
Labhesh Khetawat Page 120

## C++ programming class XI & XII .2011

case 1:cout<<"\nenter data value to inserted->"; cin>>value; first=addfirst(first,value); break; case 2:cout<<"enter data value to be inserted-->"; cin>>value; cout<<"enter the value of node after which insertion is made-->"; cin>>val; first=addbetween(first,value,val); break; case 3:cout<<"enter the data to be inserted-->"; cin>>value; first=addlast(first,value); break; case 4:traverse(first); break; case 5:exit(0); } } while(choice!=0); } void traverse(node*first) { node*temp;
Labhesh Khetawat Page 121

## C++ programming class XI & XII .2011

temp=first; cout<<"the linked list value are-->"; while (temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } } node*addfirst(node*first,int value) { node*temp; temp=new node; temp->data=value; temp->link=first; first=temp; return(first); } node*addbetween(node*first,int value,int val) { node *temp; node *temp1; temp=new node; temp->data=value; temp1=first;
Labhesh Khetawat Page 122

## C++ programming class XI & XII .2011

while(temp1!=NULL) { if(temp1->data==val) { temp->link=temp1->link; break; } temp1=temp1->link; } return(first); } node*addlast(node*first,int value) { node *temp; node *temp1,*back; temp=new node; temp->data=value; temp->link=NULL; back=first; temp1=first->link; while(temp1!=NULL) { back=temp1; temp1=temp1->link;
Labhesh Khetawat Page 123

## C++ programming class XI & XII .2011

} back->link=temp; return(first); }

## enter element of node 1-> 1 enter element of node 2 ->2

main menu: 1. Inserting at first 2. Inserting in between 3. Inserting at last 4. traversing at last 5. exit enter your choice-> 1 Enter data value to be inserted->3

Q:4.Write a program to create a linked list and delete elements according to users choice.

Labhesh Khetawat

Page 124

## C++ programming class XI & XII .2011

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *link; }; //function for deleting element in linked list. node *delfirst(node *first,int &value); node *delbetween(node *first,int &value,int val); node *dellast(node *first,int &value); void traverse(node *first); //main programming logic void main() { clrscr(); node *first, *temp, *last; int i,n,value,choice,val; cout<<"\n\n Enter how many nodes in the linked list:"; cin>>n; //creation of linked list first=new node;
Labhesh Khetawat Page 125

## C++ programming class XI & XII .2011

cout<<"\n\n Enter the data value of node 1:"; cin>>first->data; first->link=NULL; temp=first; for(i=1;i<n;i++) { last=new node; cout<<"\n\n Enter the data value of node "<<i+1<<":"; cin>>last->data; last->link=NULL; temp->link=last; temp=last; temp=last; } do { cout<<"\n\t main menu:"; cout<<"\n\t 1. For deleting at first"; cout<<"\n\t 2.For deleting in between"; cout<<"\n\t 3.For deleting at last"; cout<<"\n\t 4.Traversing the list"; cout<<"\n\t 5.For exit"; cout<<"\n\t Enter your choice:"; cin>>choice;
Labhesh Khetawat Page 126

## C++ programming class XI & XII .2011

switch(choice) { case 1:first=delfirst(first,value); if(value!=-1) cout<<"\n\t The deleted data is:"<<value; cout<<"\n\t"; break;

case 2:cout<<"\n\t Enter the value of node which is to be deleted:"; cin>>val; first=delbetween(first,value,val); if(value!=-1) cout<<"\n\t The deleted value is:"<<value<<"\n\t"; break;

## case 4:traverse(first); break;

case 5:exit(0);
Labhesh Khetawat Page 127

## C++ programming class XI & XII .2011

} }while(choice!=5); }void traverse(node*first) { node *temp; temp=first; cout<<"\n the linked listed values are"; while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } }

node*delfirst(node*first,int&value) { node *temp; temp=first; if(first==NULL) { cout<<"\n list empty"; value=-1; } else
Labhesh Khetawat Page 128

## C++ programming class XI & XII .2011

{ value=temp->data; first=first->link; temp->link=NULL; delete(temp); } return(first); } node*delbetween(node*first,int &value,int val) { node *temp,*temp1,*back; temp=first; back=temp; temp=first->link; if(first==NULL) { cout<<"\nlist empty"; value=-1; } else { while(temp!=NULL) { if(temp1->data==val)
Labhesh Khetawat Page 129

## C++ programming class XI & XII .2011

{ back->link=temp->link; temp->link=NULL; value=temp->data; delete(temp); break; } back=temp; temp=temp->link; } } return(first); } node*dellast(node*first,int &value) { node *temp; node *temp1,*back; temp=first; if(first==NULL) { cout<<"\n\tlist empty"; value=-1; }else {
Labhesh Khetawat Page 130

## C++ programming class XI & XII .2011

while(temp->link!=NULL) { back=temp; temp=temp->link; } back->link=NULL; value=temp->data; delete(temp); } return(first); }

output: enter how many nodes in the linked list:2 enter the data value of node 1:2 enter the data value of node 2:3 main menu: 1.for deleting at first 2.for deleting in between 3.for deleting at last 4.traversing the list 5.for exit enter tour choice:1 the deleted data is:2
Labhesh Khetawat Page 131

## C++ programming class XI & XII .2011

Q:5.Program to perform all basic operation on stack the stack contain data of integer type?

#include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> # define max 100 int stack[max]; int top; void push(int stack[],int val,int &top); int pop(int stack[],int &top); void show_stack(int stack[],int top); void main() { int choice,val; char opt='y'; top=-1; clrscr(); do { clrscr(); cout<<"\n main menu"
Labhesh Khetawat Page 132

## C++ programming class XI & XII .2011

<<"\n1 Addition" <<"\n2 Deletion" <<"\n3 traverse" <<"\n4 exit"; cout<<"\n enter your choice from above"; cin>>choice; switch(choice) { case 1: do { cout<<"\n enter the value to be added: "; cin>>val; push(stack,val,top); cout<<"do u want more element(y/n)"; cin>>opt; } while(toupper(opt)=='y'); break; case 2: opt='y'; do { val=pop(stack,top);
Labhesh Khetawat Page 133

## C++ programming class XI & XII .2011

if(val!=-1) { cout<<"value deleted from the stack"<<val; cout<<"\ndo u want more element(y/n)"; cin>>opt; } } while(toupper(opt)=='y'); break; case 3: show_stack(stack,top); break; case 4: exit(0); } } while(choice!=4); } void push (int stack[],int val,int&top) { if(top==max-1) { cout<<"\n\tstack full"; }
Labhesh Khetawat Page 134

## C++ programming class XI & XII .2011

else { top=top+1; stack[top]=val; } } int pop (int stack[],int&top) { int value; if(top<0) { cout<<"\n\tstack empty"; value=-1; } else { value=stack[top]; top=top-1; } return(value); } void show_stack(int stack[],int top) { int i;
Labhesh Khetawat Page 135

## C++ programming class XI & XII .2011

if(top<0) { cout<<"\n\tstack empty"; return; } i=top; clrscr(); cout<<"\n\tthe value are-->"; do { cout<<"\n\t"<<stack[i]; i=i-1; } while(i>=0); getch(); }

output: main menu: 1.addition 2.deletion 3.traverse 4.exit enter your chice:1
Labhesh Khetawat Page 136

## C++ programming class XI & XII .2011

enter the value to be added :5 do you want to add more(y/n)->n

Q.6.Program to display the basic operations of add stack,delete stack.The stack contains data of integer type.

#include<iostream.h> #include<conio.h> #include<ctype.h> #include<process.h> struct node{ int data; node *link; }; node *push(node *top,int val); node *pop(node *top,int &val); void show(node *top); void main() { clrscr(); node *top; int ch,val;
Labhesh Khetawat Page 137

## C++ programming class XI & XII .2011

char opt='y'; top=NULL; do { clrscr(); cout<<"\n\nMain menu"; cout<<"\n1.Add"; cout<<"\n2.Delete"; cout<<"\n3.Traverse"; cout<<"\n4.Exit"; cin>>ch; switch(ch) { case 1:do { cout<<"\nEnter the value to be added:"; cin>>val; top=push(top,val); cout<<"\nDo you want to add more elements:"; cin>>opt; }while(toupper(opt)=='Y'); break; case 2:do {
Labhesh Khetawat Page 138

## C++ programming class XI & XII .2011

top=pop(top,val); if(val!=-1) cout<<"\vValue deleted is-->"<<val; cout<<"\nDo you want to delete more elements:"; cin>>opt; }while(toupper(opt)=='Y'); break; case 3:show(top); break; case 4:exit(0); } }while(ch!=4); } node *push(node *top,int val) { node *temp; temp=new node; temp->data=val; temp->link=NULL; if(top==NULL) top=temp; else { temp->link=top;
Labhesh Khetawat Page 139

## C++ programming class XI & XII .2011

top=temp; } return top; } node *pop(node *top,int &val) { node *temp; if(top==NULL) { cout<<"\nSTACK EMPTY"; val=-1; } else { temp=top; top=top->link; val=temp->data; temp->link=NULL; delete temp; } return top; } void show(node *top) {
Labhesh Khetawat Page 140

## C++ programming class XI & XII .2011

node *temp; temp=top; if(temp==NULL) cout<<"\nSTACK EMPTY"; else { cout<<"\n\nStack contains:\n\n"; while(temp!=NULL) { cout<<"\t"<<temp->data; temp=temp->link; } } getch(); } OUTPUT: main menu: 1.add 2.delete 3.traverse 4.exit enter your choice:1 enter the value to be added:34 do you want to add more elements(y\n):n
Labhesh Khetawat Page 141

## C++ programming class XI & XII .2011

Q:7.Program to perform all basic operation of add queue, delete queue and show queue?

#include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> # define max 100 int queue[max]; int top,front,rear; void add_q(int queue[],int val,int &rear); int del_q(int queue[],int &front,int rear); void show_q(int queue[],int front,int rear); void main() { int choice,val; char opt='y'; rear=-1; front=-1; top=-1; clrscr(); do
Labhesh Khetawat Page 142

## C++ programming class XI & XII .2011

{ clrscr(); cout<<"\n main menu" <<"\n1. addition" <<"\n2. deletion" <<"\n3. traverse" <<"\n4. exit"; cout<<"\n enter your choice from above:"; cin>>choice; switch(choice) { case 1: do { cout<<"\n enter the value to be added :"; cin>>val; add_q(queue,val,rear); cout<<"do u want more element(y/n):"; cin>>opt; } while(toupper(opt)=='y'); break; case 2: opt='y';
Labhesh Khetawat Page 143

## C++ programming class XI & XII .2011

do { val=del_q(queue,front,rear); if(val!=-1) cout<<"\nvalue deleted from the queue:"<<val; cout<<"\ndo u want more element(y/n):"; cin>>opt; } while(toupper(opt)=='y'); break; case 3: show_q(queue,front,rear); break; case 4: exit(0); } } while(choice!=4); }

## void add_q(int queue[],int val,int &rear) { if(rear==max) cout<<"\n \t Queue full!!!!";

Labhesh Khetawat Page 144

## C++ programming class XI & XII .2011

else { rear=rear+1; queue[rear]=val; } }

int del_q(int queue[],int &front,int rear) { int value; if(front==rear) { cout<<"\n \t Queue empty!!!!"; value=-1; } else { front=front+1; value=queue[front]; } return(value); }

## void show_q(int queue[],int front,int rear)

Labhesh Khetawat Page 145

## C++ programming class XI & XII .2011

{ clrscr(); if(front==rear) { cout<<"\n \t Queue empty!!!"; return; } else { cout<<"\nThe values are:"; do { front=front+1; cout<<"\n"<<queue[front]; } while(front!=rear); getch(); } }

output:

Labhesh Khetawat Page 146

## C++ programming class XI & XII .2011

2.deletion 3.traverse 4.exit enter your choice from above:1 enter the value to be added:4 do u want to enter more(y/n):n

Q:8. Program to perform the basic operation of add queue,delete queue,using linked list the queue contains data of integer type?

#include<ctype.h> #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> struct node { int data; node*link; }; node*add_q(node*rear,int val); node*del_q(node*front,int &val); void show_q(node*front);
Labhesh Khetawat Page 147

## C++ programming class XI & XII .2011

void main() { clrscr(); node *rear,*front; int choice,val; char opt='y'; front=rear=NULL; do { clrscr (); cout<<"\nmain menu"; cout<<"\n1.addition of queue"; cout<<"\n2.deletion from queue"; cout<<"\n3.traverse of queue"; cout<<"\n4.exit from menu"; cout<<"\nenter your choice from above:"; cin>>choice; switch(choice) { case 1: do { cout<<"\nenter data value to be added in the queue->"; cin>>val;
Labhesh Khetawat Page 148

## C++ programming class XI & XII .2011

rear=add_q(rear,val); if(front==NULL) front=rear; cout<<"\ndo you want to add more element<y/n>"; cin>>opt; } while(toupper(opt)=='y'); break; case 2: opt='y'; do { front=del_q(front,val); if(front==NULL) rear=front; if(val!=-1) cout<<"\nvalue deleted from queue is->"<<val; cout<<"\ndo you want to delete more element:"; cin>>opt; } while(toupper(opt)=='y'); break; case 3: show_q(front);
Labhesh Khetawat Page 149

## C++ programming class XI & XII .2011

case 4: exit(0); } } while(choice!=4); } node *add_q(node*rear,int val) { node*temp; temp=new node; temp->data=val; temp->link=NULL; if(rear==NULL) rear=temp; else { rear->link=temp; rear=temp; } return(rear); } node *del_q(node *front,int &val) { node *temp;
Labhesh Khetawat Page 150

## C++ programming class XI & XII .2011

clrscr(); temp=front; front=front->link; val=temp->data; temp->link=NULL; delete temp; return (front); }

void show_q(node *front) { node *temp; temp=front; clrscr(); cout<<"\nthe values are:"; while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; } getch(); }

output:
Labhesh Khetawat Page 151

## C++ programming class XI & XII .2011

main menu 1.addition of queue 2.deletion of queue 3.traverse of queue 4.exit from menu enter your choice from above enter data value to be added i n queue-> do you want to add more element<y/n>n

Labhesh Khetawat

Page 152

Labhesh Khetawat

Page 153