Contains All Programs Included in The 2 Folders (Contains Small Errors in Programs)
Contains All Programs Included in The 2 Folders (Contains Small Errors in Programs)
START
STEP 1: Read the size of array A[n]
STEP 2: For i=1 to n step 3
STEP 3: Read A[i]
STEP 4: Read the number to be searched (m)
STEP 5: Print “1.Binary search 2.Linear search”
STEP 6: Read choice (ch)
STEP 7: Assign flag 0
STEP 8: if ch = 1 then
STEP 9: Assign upper n, lower 1, mid ([upper+lower/2)]
STEP 10: While upper is greater than or equal to lower repeat steps
11-16
STEP 11: if A[mid] = m then
STEP 12: Print “The position of “m” is” mid
STEP 13: Assign flag 1
STEP 14: Else if A[mid] is greater than x then
STEP 15: Assign lower mid
STEP16: Else assign upper mid
STEP 17: if ch = 2 then
STEP 18: For i=1 to n by 1 steps 19-20
STEP 19: if A[i] = x then
STEP 20: Print “the position of “x” is” i
STEP 21: if flag = 0
STEP 22: Print “No: not found”
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int ch,n,x,A[100],mid,c=0,l=0,i,j,k,u,f=0;
clrscr();
cout<<"1.linear search"
<<"2.binary search"
<<"3.exit";
z:
cout<<endl;
cout<<"enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the limit of array"<<endl;
cin>>n;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
cout<<"enter the element which should be
searched for"<<endl;
cin>>x;
for(i=0;i<n;i++)
if(A[i]==x)
{ c++;
cout<<"element is present at "<<i+1<<"th
position"<<endl;}
if(c==0)
{ cout<<"the element is not present"<<endl;}
goto z;
case 2:
cout<<"enter the limit of array"<<endl;
cin>>n;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
cout<<"enter the element which should be
searched for"<<endl;
cin>>x;
u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(A[mid]==x)
{ f++;
cout<<"element is present at"<<mid+1<<"th
position";
break;}
if(A[mid]>x)
u=mid-1;
if(A[mid]<x)
l=mid+1;
}
if(f==0)
cout<<"element is not present";
goto z;
case 3:exit(0);
default:cout<<"wrong choice";
goto z;
}
getch( );
}
SAMPLE OUTPUT
1.linear search
2.binary search
3.exit
enter the choice
1
enter the limit of array
5
enter the elements
35724
enter the element which should be searched for
5
element is present at 2th position
enter the choice
1
enter the limit of array
3
enter the elements
347
enter the element which should be searched for
1
element is not present
enter the choice
2
enter the limit of array
5
enter the elements
26841
enter the element which should be searched for
7
element is not present
enter the choice
2
enter the limit of array
3
enter the elements
478
enter the element which should be searched for
4
element is present at 1th position
enter the choice
3
//ALGORITHM FOR MATRIX
MULTIPLICATION
START
STEP 1 : read A[50][50],B[50][50],m,n,p,q
STEP 2: if(n==p)
{
for i<-0 to m by step 1
{
for k<-0 to q by step 1
{
for j<-0 to n by step 1
{
s<-a[i][j]*b[i][j]
}
sum<-sum+s
s<-0
j<-j+1
}
c[i][k]<-sum
k<-k+1
}
sum<-0
i<-i+1
}
STEP 3: display c[50[50]
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<math.h>
//class
class matrix
{
int x[2][2],m,n;
public:
void input();
void display();
void multiply(matrix,matrix);
};
void matrix :: input()
{
cout<<"enter the no of rows and columns"<<endl;
cin>>m>>n;
cout<<"enter the matrix"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>x[i][j];
}}}
void matrix::display()
{
for(int i=0;i<m;i++)
{ for(int j=0;j<n;j++)
{ cout<<x[i][j]<<" ";}
cout<<endl;
}}
void matrix::multiply(matrix m1,matrix m2)
{ int sum=0;
if(m1.n==m2.m)
{
for(int i=0;i<m1.m;i++)
{
for(int j=0;j<m2.n;j++)
{
for(int k=0;k<m2.m;k++)
{ sum=sum+(m1.x[i][j]*m2.x[k][j]);}
x[i][j]=sum;
sum=0;
}}
m=m1.m;
n=m2.n;
}}
//main program
void main()
{ matrix m1,m2,m3;
clrscr();
m1.input();
m2.input();
cout<<"matrix 1 is"<<endl;
m1.display();
cout<<"matrix 2 is"<<endl;
m2.display();
m3.multiply(m1,m2);
cout<<"the resultant matrix is"<<endl;
m3.display();
getch( );}
SAMPLE OUTPUT
enter the no of rows and columns
22
enter the matrix
11
11
enter the no of rows and columns
22
enter the matrix
11
11
matrix 1 is
11
11
matrix 2 is
11
11
the resultant matrix is
22
22
//ALGORITHM TO ENTER DETAILS IN PHONE
DIRECTORY
START
STEP 1: Declare a structure named ‘name’ having variables first
[40]of character type,mid[40]of character type, last [60]of
Character type
STEP 2: Declare a structure named phone having variables area[4] of
character type,exchange[4]of character type ,number[6]of
integer type.
STEP 3: Declare a class named prec having private variables name of
name type and phone of phone type and public members
prec( ),input( ) and displayrec( ).
STEP 4: Prec::prec( )
STEP 5: Read name.first
STEP 6: Read name.mid
STEP 7: Read name.last
STEP 8: Read phone.area
STEP 9: Read phone.exchange
STEP 10: Read phone.number
STEP 11: void prec::displayrec( )
STEP 12: Display name.first
STEP13: Display name.mid
STEP 14: Display name.last
STEP 15: Display phone.area
STEP 16: Display phone.exchange
STEP 17: Display phone.number
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct name
{
char first[40];
char mid[40];
char last[60];
};
struct phone
{
char area[4];
char exch[4];
char numb[6];
};
class p rec
{
name name;
phone phone;
public:
p_rec();
void displayp_rec();
};
p_rec::p_rec()
{
cout<<"enter the name"<<endl;
cout<<"first name ";
gets(name.first);
cout<<"mid name ";
gets(name.mid);
cout<<"last name ";
gets(name.last);
cout<<"enter the phone details"<<endl;
cout<<"area ";
gets(phone.area);
cout<<"exchange ";
gets(phone.exch);
cout<<"number ";
gets(phone.numb);
}
void p_rec::displayp_rec()
{
cout<<"the details are"<<endl;
cout<<"first name : ";
puts(name.first);
cout<<endl;
cout<<"second name : ";
puts(name.mid);
cout<<endl;
cout<<"last name : ";
puts(name.last);
cout<<endl;
cout<<"area : ";
puts(phone.area);
cout<<endl;
cout<<"exchange : ";
puts(phone.exch);
cout<<endl;
cout<<"number : ";
puts(phone.numb);
cout<<endl;
}
void main()
{ clrscr();
p_rec record;
record.displayp_rec();
getch( );
}
SAMPLE OUTPUT
enter the name
first name RAJ
mid name KUMAR
last name SINGH
enter the phone details
area MUMBAI
exchange 0484
number 2611006
area : MUMB04842611006
exchange : 04842611006
number : 2611006
//ALGORITHM TO SORT EMPLOYEES
ACCORDING TO THEIR SALARIES
START
STEP 1: Define structure employee with variables eno, name, salary
STEP 2: Read the number of employees (n)
STEP 3: for i=1 to n by 1 step 4
STEP 4: Read eno, name, salary
STEP 5: for i= 1 to n by step 6-8
STEP 6: for i= 1 to n by 1 step 7-8
STEP 7: if salary [i] > salary[i+1]
STEP 8: Assign k salary[i], salary[i] salary[i+1], salary[i+1]= k
STEP 9: Print “sorted list”
STEP 10: for i= 1 to n by 1 step 11
STEP 11: Print eno[i], name[i], salary[i]
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
//structure
struct employee
{
int empno;
char name[20];
float salary;
}e[5];
//main prg
void main()
{ clrscr();
employee x;
for(int i=0;i<5;i++)
{
cout<<"employee no :";
cin>>e[i].empno;
cout<<endl;
cout<<"name :";
gets(e[i].name);
cout<<endl;
cout<<"salary :";
cin>>e[i].salary;
cout<<endl;}
for (int j=0;j<5;j++)
{
for(i=0;i<4;i++)
{
if(e[i].salary<e[i+1].salary)
{
x=e[i+1];
e[i+1]=e[i];
e[i]=x;
}
}
}
cout<<endl;
cout<<"the employee details"<<endl;
for(i=0;i<5;i++)
{ cout<<"employee no : "<<e[i].empno<<endl;
cout<<"name :";
puts(e[i].name);
cout<<endl;
cout<<"salary :"<<e[i].salary<<endl;
}
getch( );
}
SAMPLE OUTPUT
employee no :45
name :JINY
salary :87900
employee no :47
name :JEAN
salary :88590
employee no :33
name :JENZ
salary :56700
employee no :54
name :JINGITH
salary :77000
employee no:78
name :JINCY
salary :90000
the sorted employee details
employee no : 78
name :JINCY
salary :90000
employee no : 47
name :JEAN
salary :88590
employee no : 45
name :JINY
salary :87900
employee no : 54
name :JINGITH
salary :77000
employee no : 33
name :JENZ
salary :56700
//ALGORITHM TO MERGE TWO ARRAY
START
STEP 1: Read the size of first array (m)
STEP 2: Read the size of second array (n)
STEP 3: For i= 1 to m by 1 step 4
STEP 4: Read a[i] in ascending order
STEP 5: For i= 1 to n by 1 step 6:
STEP 6: Read b[i] in descending order
STEP 7: k 0, i n, j 0
STEP 8: if i>= 0 or j< m
STEP 9: if(A[i-1]> B[j]) then step 10-12
STEP 10: Assign C[k] A[i-1], i i-1
STEP 11: Else assign C[k]= B[j], j j+1
STEP 12: k k+1
STEP 13: if i= -1 then steps 14-15
STEP 14: if j< m step 15
STEP 15: Assign C[k] B[j], j j+1, k k+1
STEP 16: if j= m then step 17-18
STEP 17: if i>= 0 then step 18
STEP 18: Assign C[k] A[i-1], k k+1,i i-1
STEP 19: Print “sorted array”
STEP 20: for i= 1 to n by 1 step 21
STEP 21: Print C[i]
STOP
//PROGRAM
#include <iostream.h>
#include<conio.h>
void main()
{
int A[20],B[20],C[40],n,m,k,i,j;
clrscr();
cout<<"enter the limit of array A"<<endl;
cin>>n;
cout<<"enter the array"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
cout<<"enter the limit of array B"<<endl;
cin>>m;
cout<<"enter the array"<<endl;
for(j=0;j<m;j++)
cin>>B[j];
i=n-1;
j=0;
k=0;
while(i>=0&&j<m)
{
if(A[i]>B[j])
{
C[k]=A[i];
k=k+1;
i=i-1;}
if(B[j]>A[i])
{
C[k]=B[j];
k=k+1;
j=j+1;
}}j=0;
if(i==-1)
{
while(j<m)
{
C[k]=B[j];
k=k+1;
j=j+1;
}
}i=n-1;
if(j==m)
{ while(i>=0)
{
C[k]=A[i-1];
k=k+1;
i=i-1;
}
}
cout<<"merged array is"<<endl;
for(k=0;k<m+n;k++)
cout<<C[k]<<endl;
getch( );}
SAMPLE OUTPUT
enter the limit of array A
5
enter the array
3 5 8 10 13
enter the limit of array B
5
enter the array
23 17 14 11 6 2
merged array is
23
17
14
13
11
10
8
6
3
2
//ALGORITHM TO COUNT NUMBER OF
RECORDS
START
STEP 1: Declare a class named hoteldata having members duration
and room of int type and name[20] of char type
STEP 2: c0
STEP 3: Read duration, name[20], room
STEP 4: Open a binary file named count.dat
STEP 5: Write duration, name[20] and room into the file count.dat
STEP 6: Close the file count.dat
STEP 7: Open the file count.dat
STEP 8: hoteldata j
STEP 9: while (f.read ((char*) &j, sizeof (j)))
{c<-c+1
Display duration, name[20], room
STEP 10: Display c
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
class hoteldata
{
int room,duration;
char name[20];
public:
void enterdata()
{
cout<<"Enter room:";
cin>>room;
cout<<"\nNAME:";
gets(name);
cout<<"\nDuration:";
cin>>duration;
}
void display()
{
cout<<"Room:";
cout<<room<<"\n";
cout<<"NAME:";
puts(name);
cout<<endl;
cout<<"Duration:"<<duration<<endl;}};
void main()
{
int c=0;
char ch;
clrscr();
do
{
ofstream fout("count.dat",ios::binary|ios::app);
if(fout)
{
hoteldata h;
cout<<"\nEnter the datas\n";
h.enterdata();
fout.write((char*)&h,sizeof(h));
fout.close();
}
else if(!fout)
{
cout<<"FILE NOT OPENED\n";
}
ifstream f("count.dat",ios::binary);
f.seekg(0);
hoteldata j;
if(!f)
{
cout<<"FILE NOT OPENED\n";
}
else
while(f.read((char*)&j,sizeof(j)))
{
j.display();
c++;
}
cout<<"\nTotal number of record="<<c;
f.close();
cout<<"\nDo u want to continue(enter y for 'yes' & any other
for 'no'):";
cin>>ch;
}while(ch=='y');
getch( );
}
SAMPLE OUTPUT
Duration:2
Room:3
NAME:JOHN
Duration:1
Room:1
NAME:JIS
Duration:4
Room:3
NAME:JERIN
Duration:5
Room:3
NAME:BESSY
Duration:3
Room:1
NAME:ALWINA
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int ch,queue[5],front=-1,rear=-1,i;
clrscr();
cout<<"\n 1.Insert"<<"\n 2.Delete"<<"\n 3.Exit";
X:
cout<<endl;
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{ case1:
if(((front==0)&&(rear==4))||(rear+1==front))
{ cout<<"overflow"<<endl;}
else
{ if((rear==-1)&&(front==-1))
{ rear=0;
front=0;}
else if(rear==4)
{ rear=0;}
else
{ rear++;}
cout<<"enter the element"<<endl;
cin>>queue[rear];
}
//display
if(rear>=front)
{
for(int i=0;i<front;i++)
{ cout<<"_";}
for(i=front;i<=rear;i++)
{ cout<<queue[i];}
for(i=rear+1;i<5;i++)
{ cout<<"_";}
}
else
{ for( i=0;i<=rear;i++)
{ cout<<queue[i];}
for(i=rear+1;i<front;i++)
{ cout<<"_";}
for(i=front;i<5;i++)
{ cout<<queue[i];}
}
goto X;
case 2:
if((front==-1)&&(rear==-1))
{ cout<<"underflow";}
else
{ if(front==4)
{ front=0;}
else
if(front==rear)
{ front=-1;
rear=-1;}
else
{ front++;}}
//display
if(rear>=front)
{
for( i=0;i<front;i++)
{ cout<<"_";}
for(i=front;i<=rear;i++)
{ cout<<queue[i];}
for(i=rear+1;i<5;i++)
{ cout<<"_";}
}
else
{ for(i=0;i<=rear;i++)
{ cout<<queue[i];}
for(i=rear+1;i<front;i++)
{ cout<<"_";}
for(i=front;i<5;i++)
{ cout<<queue[i];}
}
goto X;
case 3:exit(0);
default: cout<<"wrong choice"<<endl;
goto X;}getch( );}
SAMPLE OUTPUT
1.Insert
2.Delete
3.Exit
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{int rear=-1,front=-1,queue[5];
int ch;
clrscr();
cout<<"1.Insertion"<<endl<<"2.Deletion"<<endl<<"3.Exit"<<endl;
X:
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1: if((front==-1)&&(rear==-1))
{front++;
rear++;
cout<<"enter the element";
cin>>queue[rear];}
else
if(rear==4)
{cout<<"overflow"<<endl;}
else
{rear++;
cout<<"enter the element";
cin>>queue[rear];
}
//Display
for(int i=0;i<front;i++)
{cout<<"_";}
for(i=front;i<=rear;i++)
{cout<<queue[i];}
for(i=rear;i<4;i++)
{cout<<"_";}
goto X;
case 2:if (front==-1)
{cout<<"underflow";}
else
if(rear==front)
{rear=-1;
front=-1;}
else
{front++;}
//display
for(i=0;i<front;i++)
{cout<<"_";}
for(i=front;i<=rear;i++)
{cout<<queue[i];}
for(i=rear;i<4;i++)
{cout<<"_";}
goto X;
case 3:exit(0);
default:cout<<"wrong choice";
goto X;
}
getch();
}
SAMPLE OUTPUT
1.Insertion
2.Deletion
3.Exit
Enter The Choice
1
Enter the element
1
The queue is
1_ _ _ _
Enter The Choice
1
Enter the element
2
The queue is
1 2_ _ _
Enter The Choice
1
Enter the element
3
The queue is
123__
Enter The Choice
1
Enter the element
4
The queue is
1234_
Enter The Choice 1
Enter the element
5
The queue is
12345
Enter The Choice
1
Overflow
Enter the choice
2
The queue is
_2345
Enter the choice
2
The queue is
__345
1
//ALGORITHM TO FIND SUM OF ROWS,
COLUMNS, MAIN & SECOND DIAGONAL
START
STEP 1:read A[10][10],m,n;
STEP 2:display the choices 1.the sum of the main diagonal 2.the sum
of second diagonal 3. the sum of rows 4.the sum of columns 5.exit
STEP 3:if ch=1 go to step 3 if ch=2 go to step 10 if ch=3 go to step
15
if ch=4 go to step 22 else go to step 26
STEP 4:read a[50][50],m,n;
STEP 5:sum <- 0
STEP 6:if (m=n)
{for i<-0 to n by step 5
sum <-sum + A[i][i]
i<-i+1}
STEP 7:display sum
STEP 8:display sum cannot be found
STEP 9:go to step 3
STEP 10:sum<-0
STEP 11:if (m=n)
{for i<-0 ,j<-n-1 to m and 0 by step 10
{sum <-sum + A[i][j]
i<-i+1
j<-j+1
}}
STEP 12:display sum
STEP 13:display sum cannot be found
STEP 14:go to step 3
STEP 15:sum<-0
STEP 16:for i<-0 to m,j<-0 to n by step 15
{sum=sum + A[i][j]
j<-j+1
i<-i+1}
STEP 17:display sum
STEP 18:display sum cannot be found
STEP 19:display sum
STEP 20:display sum cannot be found
STEP 21:go to step 3
STEP 22:sum<-0
STEP 23:for j<-0 to n ,for i<-0 to m by step 22
{sum=sum + A[i][j]
j<-j+1
i<-i+1}
STEP 24:display sum
STEP 25:display sum cannot be found
STEP 26:exit
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
int x[2][2],m,n,i,j,ch;
clrscr();
cout<<" 1.main diagonal"
<<"2.seconddiagonal"
<<"3.rows"
<<"4.columns"
<<"5.exit";
y:
cout<<"enter the choice"<<endl;
cin>>ch;
if(ch==5)
{ exit(0);}
cout<<"enter the no of rows and columns"<<endl;
cin>>m>>n;
cout<<"enter the matrix"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{ cin>>x[i][j];}}
cout<<"the matrix is"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{ cout<<x[i][j]<<" ";}
cout<<endl;}
switch (ch)
{
case 1:
void maind(int[][2],int,int);
maind(x,m,n);
goto y;
case 2:
void secondd(int[][2],int,int);
secondd(x,m,n);
goto y;
case 3:
void rows(int[][2],int,int);
rows(x,m,n);
goto y;
case 4:
void column(int[][2],int,int);
column(x,m,n);
goto y;
case 5:exit(0);
default:cout<<"wrong choice";
goto y;
}
getch( );}
//function 1
void maind(int x[][2],int m,int n)
{
int i,j,s=0;
if(m==n)
{
for(i=0;i<m;i++)
{ s=s+x[i][i];}
cout<<"sum of main diagonal is"<<endl;
cout<<s<<endl;}
else
{ cout<<" not possible";}}
//function 2
void secondd(int x[][2],int m,int n)
{
int i,j,s=0;
if(m==n)
{ for(i=0,j=n-1;i<m;i++,j--)
{ s=s+x[i][j];}
cout<<"sum of second diagonal is"<<endl;
cout<<s<<endl;
}
else
{ cout<<"not possible";}}
//function 3
void rows(int x[][2],int m,int n)
{ int i,j,s=0;
for(i=0;i<m;i++)
{ s=0;
for(j=0;j<n;j++)
{ s=s+x[i][j];}
cout<<"sum of row "<<i+1<<" ="<<s<<endl;
}}
//function 4
void column(int x[2][2],int m,int n)
{ int i,j,s=0;
for(j=0;j<n;j++)
{ s=0;
for(i=0;i<m;i++)
{ s=s+x[i][j];}
cout<<"sum of column "<<j+1<<" ="<<s<<endl;
}}
SAMPLE OUTPUT
1.main diagonal
2.second diagonal
3.rows
4.columns
5.exit
enter the choice
1
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
//function 1
void area(float r)
{float area_c;
area_c=(3.14*r*r);
cout<<"Area of the circle="<<area_c<<endl;
}
//function 2
void area(int l,int h)
{float area_r;
area_r=l*h;
cout<<"Area of the rectangle = "<<area_r<<endl;
}
//function 3
void area(float a,float b,float c)
{float s,area_t ;
s=(a+b+c)/2;
area_t=pow((s*(s-a)*(s-b)*(s-c)),0.5);
cout<<"Area of the triangle = "<<area_t<<endl;
}
void main()
{
clrscr();
int ch;
float r,a,b,c,l,h;
cout<<"1.Area of circle"<<endl<<"2.Area of
rectangle"<<endl;cout<<"3.Area of triangle"<<endl<<"4.Exit"<<endl;
X:
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the radius of the circle"<<endl;
cin>>r;
void area (float);
area(r);
goto X;
case 2:cout<<"Enter the sides of rectangle"<<endl;
cin>>l>>h;
void area (float,float);
area (l,h);
goto X;
case 3:cout<<"Enter the sides of triangle"<<endl;
cin>>a>>b>>c;
void area (float,float,float);
area (a,b,c);
goto X;
case 4:exit(0);
default:cout<<"Wrong Choice"<<endl;
goto X;}
getch();}
SAMPLE OUTPUT
1.Area of circle
2.Area of rectangle
3.Area of triangle
Enter the choice
1
Enter the radius of circle
10
Area of the circle = 314
Enter the choice
2
Enter the sides of rectangle
3
4
Area of the rectangle =12
Enter the choice
3
Enter the sides of triangle
3
4
5
The area of triangle = 6
//ALGORITHM TO ENTER & VIEW
PUBLICATIONS OF A COMPANY
START
STEP 1:Define a base class publication with private members
title[20],price
STEP 2:Display choices 1.Book 2.Tape 3.Exit
STEP 3:Read ch
STEP 4:Read title[20],price
STEP 5:if ch=1 goto step 6
else if ch=2 goto step 10
else goto step 14
STEP 6:Define a derived class book inherited in public mode from
publication with private member pagecount
STEP 7:Read pagecount
STEP 8:Display title[20],price.pagecount
STEP 9:goto step 2
STEP 10:Define a derived class tape inherited in public mode with
private member playtime
STEP 11:Read playtime
STEP 12:Display title[20],price,playtime
STEP 13:goto step2
STEP 14:exit
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
//base class
class publication
{
char title[20];
float price;
public:
void getdata()
{cout<<"enter the details"<<endl;
cout<<"title :";
gets (title);
cout<<"price :";
cin>>price;
}
void putdata()
{cout<<"the details"<<endl;
cout<<"title:";
puts(title);
cout<<"price :"<<price<<endl;
}
};
//derived class
class book:public publication
{
int pagecount;
public:
void getdata()
{
publication::getdata();
cout<<"pagecount :";
cin>>pagecount;
}
void putdata()
{cout<<endl;
publication::putdata();
cout<<"pagecount :";
cout<<pagecount<<endl;
}
};
//derived class
class tape:public publication
{
int playtime;
public:
void getdata()
{
publication::getdata();
cout<<"playtime :";
cin>>playtime;
}
void putdata()
{
cout<<endl;
publication::putdata();
cout<<"playtime :"<<playtime<<endl;
}
};
void main()
{
clrscr();
int ch;
book b;
tape t;
cout<<"1.book"<<endl<<"2.tape"<<endl<<"3.exit"<<endl;
X:
cout<<"enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:b.getdata();
b.putdata();
goto X;
case 2:t.getdata();
t.putdata();
goto X;
case 3:exit(0);
default:cout<<"wrong choice";
goto X;
}
}
SAMPLE OUTPUT
1.book
2.tape
3.exit
enter the choice
1
enter the details
title :WINGS OF FIRE
price :230
pagecount :250
the details
title:WINGS OF FIRE
price :230
pagecount :250
enter the choice
2
enter the details
title :MALAYALAM SONGS
price :60
playtime :60
the details
title:MALAYALAM SONGS
price :60
playtime :60
//ALGORITHM TO DO CONVERSIONS
//Decimal to Binary
START
STEP 1: read d
STEP 2: assign sum<-0,j<-0
STEP 3:while(d>0)
{
a<-d%2
a<-a*pow(10,j)
sum<-sum+a
j++
d<-d/2
}
STEP 4: print sum
STOP
//Binary to Decimal
START
STEP 1: read b
STEP 2: assign i<-0,s<-0,h<-0
STEP 3: while(b>0)
{
h<-b%2
s<-s+h*pow(2,i);
i++;
b<-b/10;
}
STEP 4: print s
STOP
//Octal to Binary
START
STEP 1: read oc
STEP 2:assign p<-0,r<-0,j<-0,i<-0,s<-0,sum<-0
STEP 3: while(oc>0)
{
P<-oc%10
while(p>0)
{
r<-p%2
s<-s+r*pow(10,j)
j<-j+1
p<-p/2
}
sum<-sum+s*pow(1000,i)
i<-i+1
oc<-oc/10
}
STEP 4: print sum
STOP
//Binary to Octal
START
STEP 1: read b
STEP 2:assign x<-0,i<-0,j<-0,d<-0,n<-0,a<-0
STEP 3: while(b>0)
{
a<-b%1000
while(a>0)
{
n<-a%10
d<-d+n*pow(2,j)
j<-j+1
a<-a/10
}
x<-x+d*pow(10,i)
i<-i+1
b<-b/1000
}
STEP 4: print x
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int j=0,i=0,d=0,x=0,s=0,a,p,r,n,oc;
int ch;
long sum=0,b;
clrscr();
cout<<"1.binary to octal"<<endl<<"2.octal to
binary"<<endl<<"3.binary to decimal"<<endl<<"4.decimal to
binary"<<endl<<"5.exit"<<endl;
z:
cout<<"enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the binary no"<<endl;
cin>>b;
while(b>0)
{a=b%1000;
while(a>0)
{n=a%10;
d=d+n*pow(2,j);
j=j+1;
a=a/10;
}
x=x+d*pow(10,i);
i=i+1;
b=b/1000;
}
cout<<"the octal value is"<<x;
goto z;
SAMPLE OUTPUT
1.binary to octal
2.octal to binary
3.binary to decimal
4.decimal to binary
5.exit
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{char c,r;
ifstream ("lower.txt");
ifstream("upper.txt");
ifstream ("others.txt");
ofstream f1("lower.txt");
ofstream f2("upper.txt");
ofstream f3("others.txt");
if(f1&&f2&&f3)
{
clrscr();
cout<<"Enter the character"<<endl;
cout<<"Press | to stop entering"<<endl;
cin.get(c);
while((c=getchar())!='|')
{
if (islower(c))
{f1.put(c);
f1<<" ";}
if(isupper(c))
{f2.put(c);
f2<<" ";}
else
{f3.put(c);
f3<<" ";}
}
f1.close();
f2.close();
f3.close();
}
else
{cout<<"Files not opened"<<endl;}
ifstream f4("lower.txt");
ifstream f5("upper.txt");
ifstream f6("others.txt");
f4.seekg(0);
f5.seekg(0);
f6.seekg(0) ;
if(f4&&f5&&f6)
{cout<<"The details of the file UPPER"<<endl;
while(!f5.eof())
{f4.get(c);}
cout<<"The details of the file LOWER"<<endl;
while(!f4.eof())
{f4.get(c);}
cout<<"The details ofthe file OTHERS"<<endl;
while(!f6.eof())
{ f6.get(c);}
}
f4.close();
f5.close();
f6.close();
else
{cout<<"Not opened"<<endl;}
getch();
}
SAMPLE OUTPUT
Enter the character
Press | to stop entering
A
B
C
D
E
a
b
c
d
e
1
2
3
4
5
|
The details of the file UPPER
ABCDE
The details of the file LOWER
abcde
The details of the file OTHERS
12345
//ALGORITHM TO
// To reverse each word of the string
START
STEP 1:Read st[100]
STEP 2:p<-0, k<-0, i<-0
STEP 3:l<-strlen(st)
STEP 4:for i<-0 to l by step 3
if(st[i]=’ ‘)||(st[i]=’/0’)
{for j<-(i-1) to p by step 2
{sr[k]<-st[j]
k<-k+1
j<-j+1}
sr[k]<-‘ ‘
k<-k+1
p<-i+1
}i<-i+1
}
STEP 5:Display (sr,l)
STOP
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
char st[100],sr[100],ts[100];
int p=0,k=0,i=0,b=0,l,a,ch,j,c=0;
clrscr();
case 4:exit(0);
default:cout<<"wrong choice"<<endl;
goto z;
}
getch();
}
SAMPLE OUTPUT
enter the string
OLD IS GOLD
1.to reverse each word of the string
2.to make the first word in the last position
3.to count the no of vowels
4.exit
enter the choice
1
DLO SI DLOG
enter the choice
2
GOLD IS OLD
enter the choice
3
the no of vowels is 3
START
STEP 1: Define class travelplan with members long integer
plancode,
character place,integer no-travellers,no-buses.
STEP 2: Assign plancode<-'1001',place<-'Agra',no-travellers<-'5',
no-buses<-'1'.
STEP 3: Read plancode.
STEP 4: Read place.
STEP 5: Read no of travellers(no-travellers).
STEP 6: If no-travellers less than 20
assign no-buses<-1.
STEP 7: If no-travellers equal to or greater than 20 and less than 40
assign no-buses<-2.
STEP 8: If no-travellers greater than 40 or equal to 40
assign no-buses<-3.
STEP 9: Display plancode.
STEP 10: Display place.
STEP 11: Display no-travellers.
STEP 12: Display no-buses.
STOP.
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
//class
class travelplan
{long plancode;
char place[10];
int noofpeople;
int noofbus;
public:
travelplan();
void newplan();
void showplan();
}travel;
void travelplan::travelplan()
{cout<<"enter the plancode"<<endl;
cin>>plancode;
cout<<endl;
cout<<"enter the place"<<endl;
gets(place);
cout<<endl;
cout<<"enter the no of travellers"<<endl;
cin>>noofpeople;
cout<<endl;
}
void travelplan::newplan()
{if(noofpeople<20)
{noofbus=1;}
else if((noofpeople>=20)&&(noofpeople<40))
{noofbus=2;}
else if(noofpeople>=40)
{noofbus=3;}
}
void travelplan::showplan()
{cout<<"Plan code : "<<plancode<<endl;
cout<<"Place : ";
puts(place);
cout<<endl;
cout<<"No of travellers : "<<noofpeople<<endl;
cout<<"No of bus : "<<noofbus<<endl;
}
void main()
{travel.newplan();
travel.showplan();
getch();
}
SAMPLE OUTPUT
enter the plancode
234
No of travellers : 90
No of bus : 3
//ALGORITHM TO DO SORTING
// Selection sort
START
STEP 1:Read A[50],i,j,p,small
STEP 2:Read the value of n
STEP 3:Read the array
STEP 4:For i<-0 to n by step 1
STEP 5:{small<-A[i]
p<-i;}
STEP 6:For j<-i to n by step 1
{if small>A[j]
small<-A[j]
p<j}
STEP 7: For k<-p to i by step -1
{A[k]<-A[k-1]}
STEP 8:A[i]<-small
STEP 9:Display the array
STOP
// Insertion sort
START
STEP 1:Assign A[10],i,n,j,small,x
STEP 2:Read the value of n
STEP 3:Read the array
STEP 4:Assign A[0]<- -32767
STEP 5:For i<-1 to n by step 1
STEP 6:For j<-0 to i by step 1
STEP 7:If A[i]<A[j]
STEP 8:A[i]<-x
STEP 9:For k<-i to j by step -1
STEP 10:A[k]<-A[k-1]
STEP 11:A[j]<-x
STEP 12:A[i+1]<-small
STEP 13:Display the array
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int ch,j,i,n,x,A[100],p=0,k,small=0;
clrscr();
cout<<"1.Exchange selection Sort"<<endl<<"2.Selection
Sort"<<endl<<"3.Insertion sort"<<endl<<"4.Exit"<<endl;
z:
cout<<"enter the choice"<<endl;
cin>>ch;
cout<<endl;
switch(ch)
{
case 1: cout<<"enter the limit of the array"<<endl;
cin>>n;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
{cin>>A[i];}
for(i=0;i<n;i++)
{small=A[i];
p=i;
for(j=i;j<n;j++)
{
if(small>A[j])
{small=A[j];
p=j;}
}
A[p]=A[i];
A[i]=small;}
cout<<"sorted array is"<<endl;
for(i=0;i<n;i++)
cout<<A[i]<<endl;
goto z;
case 2: cout<<"enter the limit of the array"<<endl;
cin>>n;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
{cin>>A[i];}
for(i=0;i<n;i++)
{small=A[i];
p=i;
for(j=i;j<n;j++)
{
if(small>A[j])
{small=A[j];
p=j;}
}
for(k=p;k>i;k--)
{A[k]=A[k-1];}
A[i]=small;
}
cout<<"sorted array is"<<endl;
for(i=0;i<n;i++)
{cout<<A[i]<<endl;}
goto z;
case 3 :cout<<"enter the limit of the array"<<endl;
cin>>n;
cout<<"enter the elements"<<endl;
for(i=1;i<=n;i++)
{cin>>A[i];}
A[0]=-32767;
for(i=1;i<=n;i++)
{ small=A[i];
for(j=1;j<i;j++)
{if(A[i]<A[j])
{for(k=i;k>j;k--)
{A[k]=A[k-1];}
A[j]=small;}}}
cout<<"sorted array is"<<endl;
for(i=1;i<=n;i++)
{cout<<A[i]<<endl;}
goto z;
case 4: exit(0);
default:cout<<"wrong choice";
goto z;
}
getch();
}
SAMPLE OUTPUT
1.Exchange selection Sort
2.Selection Sort
3.Insertion sort
4.Exit
enter the choice
1
enter the limit of the array
5
enter the elements
5
4
3
2
1
sorted array is
1
2
3
4
5
enter the choice
2
//Deletion
START
STEP 1:if(front=NULL)
{Print"UNDERFLOW"}
STEP 2:else
{save=front
front=front->LINK
delete save}
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
//structure
struct queue
{
int info;
queue *next;
};
queue *front=NULL,*rear=NULL,*ptr=NULL,*save=NULL;
void main()
{int ch;
clrscr();
cout<<"1.Insertion"<<endl<<"2.Deletion"<<endl<<"3.Exit"<<endl
;
X:
cout<<endl;
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:ptr=new queue;
if(ptr==NULL)
{cout<<"overflow"<<endl;}
else
{cout<<"enter the information"<<endl;
cin>>ptr->info;
ptr->next=NULL;
if((front==NULL)&&(rear==NULL))
{front=ptr;
rear=ptr;
}
else
{rear->next=ptr;
rear=ptr;
}
}
//Display
cout<<"the details are"<<endl;
ptr=front;
while(ptr!=NULL)
{cout<<ptr->info<<" ";
ptr=ptr->next;
} }
goto X;
case 2:if(front==NULL)
{cout<<"Underflow"<<endl;}
else
{save=front;
front=front->next;
delete save;
//Display
cout<<"the details are"<<endl;
ptr=front;
while(ptr!=NULL)
{cout<<ptr->info<<" ";
ptr=ptr->next;
}}
goto X;
case 3:exit(0);
default:cout<<"wrong choice";
goto X;
}
getch();}
SAMPLE OUTPUT
1.Insertion
2.Deletion
3.Exit
Enter the choice
1
enter the information
1
the details are
1
Enter the choice
1
enter the information
2
the details are
12
Enter the choice
1
enter the information
3
the details are
123
Enter the choice
1
enter the information
4
the details are
1234
Enter the choice
1
enter the information
5
the details are
12345
Enter the choice
2
the details are
2345
Enter the choice
2
the details are
345
//Deletion
START
STEP 1:if(top= -1)
{"underflow"
exit from program}
else
{print stack[top]
top<-top+1}
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{int top=-1,stack[5],ch;
clrscr();
cout<<"1.Insertion"<<endl<<"2.Deletion"<<endl<<"3.Exit"<<endl
;
X:
cout<<"enter the choice";
cin>>ch;
switch(ch)
{
case 1:if(top==4)
{cout<<"overflow";}
else
{cout<<"enter the element";
top++;
cin>>stack[top];
}
cout<<"the stack is"<<endl;
for(int i=0;i<=top;i++)
{cout<<stack[i]<<" ";}
cout<<endl;
goto X;
case 2:if(top==-1)
{cout<<"underflow";}
else
{top--;}
cout<<"the stack is"<<endl;
for (i=0;i<=top;i++)
{cout<<stack[i]<<" ";
}cout<<endl;
goto X;
case 3:exit(0);
default:cout<<"wrong choice";
}
getch();
}
SAMPLE OUTPUT
1.Insertion
2.Deletion
3.Exit
Enter The Choice
1
Enter the element
1
The stack is
1
Enter The Choice
1
Enter the element
2
The stack is
12
Enter The Choice
1
Enter the element
3
The stack is
123
Enter The Choice
1
Enter the element
4
The stack is
1234
Enter The Choice
1
Enter the element
5
The stack is
12345
Enter The Choice
1
Overflow
Enter the choice
2
The stack is
1234
Enter the choice
2
The stack is
123
//Deletion
START
STEP 1:if(top=null)
{print "stack empty,underflow"}
STEP 2:else
{print top->info
top<-top->link}
STOP
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct stack
{ int info;
stack *next;
};
stack *top=NULL,*ptr=NULL, *save=NULL;
void main()
{clrscr();
int ch;
cout<<"1.Insertion"<<endl<<"2.Deletion"<<endl<<"3.Exit"<<endl
;
X:
cout<<"Enter The Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:ptr=new stack;
if(ptr==NULL)
{cout<<"overflow";}
else
{cout<<"enter the information";
cin>>ptr->info;
ptr->next=NULL;
if(top==NULL)
{top=ptr;}
else
{ptr->next=top;
top=ptr;}}
cout<<"the details are"<<endl;
ptr=top;
while(ptr!=NULL)
{
cout<<ptr->info<<"->";
ptr=ptr->next;}
cout<<endl;
goto X;
case 2:if(top==NULL)
{cout<<"underflow";}
else
{save=top;
top=top->next;
delete save;}
cout<<"the detais are"<<endl;
ptr=top;
while(ptr!=NULL)
{cout<<ptr->info<<"->";
ptr=ptr-> next;} cout<<endl;
goto X;
case 3:exit(0);
default:cout<<"wrong choice";
}getch();
}
SAMPLE OUTPUT
1.Insertion
2.Deletion
3.Exit
Enter The Choice
1
Enter the information
1
The details are
1
Enter The Choice
1
Enter the information
2
The details are
21
Enter The Choice
1
Enter the information
3
The details are
321
Enter The Choice
1
Enter the information
4
The details are
4321
Enter The Choice
1
Enter the information
5
The details are
54321
Enter the choice
2
The details are
4321
Enter the choice
2
The details are
321
//ALGORITHM TO CALCULATE
PERCENTAGE OF A STUDENT
START
STEP 1:Declare a structure named student having members roll of
int type,name[5] of char type cls[5] of char type,
marks[5] of float type
STEP 2:Declare a structure named result having members r of int
type, percent of int type and grade of char type
STEP 3:student st, result R
STEP 4:Read roll,name[5],cls[5],marks[5]
STEP 5:Open a binary file named stud.dat
STEP 6:Write the object st into the file stud
STEP 7: Open a binary file named result.dat
STEP 8: for int i<-0 to 5 by step 1
{sum<-sum+st.marks[i];
1<i+1}
R.percent<-sum/5;
if(R.percent>=75)
{R.grade<-‘A';}
if((R.percent<75)&&(R.percent>=60))
{R.grade<-'B';}
if((R.percent<60)&&(R.percent>=50))
{R.grade<-‘C';}
if((R.percent<50)&&(R.percent>=40))
{R.grade='D';}
if(R.percent<40)
{R.grade='F';}
R.r=st.roll;
STEP 9:Write the object R into result.dat
STEP 10:Display R.roll, R.grade,R.percent
//PROGRAM
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
struct student
{ int roll;
char name[20];
char cls[5];
float marks[5];
}st;
struct result
{ int r;
int percent;
char grade;
}R;
void main()
{ clrscr();
ofstream f("stud.dat",ios::binary);
ofstream f3("result.dat",ios::binary);
if(f&&f3)
{
cout<<"enter the details"<<endl;
cout<<" roll : ";
cin>>st.roll; cout<<endl;
cout<<"name : ";
gets(st.name);cout<<endl;
cout<<"class : ";
gets(st.cls);
cout<<endl;
cout<<"enter the marks"<<endl;
for(int j=0;j<5;j++)
{ cin>>st.marks[j];cout<<endl;}
f.write((char *)&st,sizeof(st));
}
else
{ cout<<"not opened";}
float sum=0;
f.close();
ifstream f1("stud.dat",ios::binary);
f1.seekg(0);
if(!f1)
{ cout<<"not opened";}
else
{
while(f1.read((char*)&st,sizeof(st)))
{ sum=0;
for(int i=0;i<5;i++)
{ sum=sum+st.marks[i];}
R.percent=sum/5;
if(R.percent>=75)
{ R.grade='A';}
if((R.percent<75)&&(R.percent>=60))
{ R.grade='B';}
if((R.percent<60)&&(R.percent>=50))
{ R.grade='C';}
if((R.percent<50)&&(R.percent>=40))
{ R.grade='D';}
if(R.percent<40)
{ R.grade='F';}
R.r=st.roll;
f3.write((char*)&R,sizeof(R));
cout<<"Roll : "<<R.r<<endl;
cout<<"percent : "<<R.percent<<endl;
cout<<"grade : "<<R.grade;
}}
f1.close();
f3.close();
getch( );
}
SAMPLE OUTPUT
enter the details
roll : 14
name : SHAUN
class : 5
enter the marks
88
85
76
94
97
Roll : 14
percent : 88
grade : A