C++ Program File: Pulkit Mogha
C++ Program File: Pulkit Mogha
Program
File
Contains programs showcasing major C++ concepts like file Pulkit Mogha
handling, sorting, function overloading, stacks and queues, D.L.F. Public
conversion, matrices, series as well as SQL programming. School
XII A
C++
PROGRAM
FILE
Index
Program 1
REVERSING A STRING BY WORD OR BY SENTENCE
Program 2
BUBBLE SELECTION SORT
Program 3
LINEAR AND BINARY SEARCH
Program 4
CONVERT BINARY TO DECIMAL OR OCTAL & VICE-VERSA
Program 5
COUNT THE NUMBER OF RECORDS IN BINARY FILES
Program 6
SUM OF ROW, COLUMN OR DIAGONAL
Program 7
MULTIPLICATION OF MATRIX
Program 8
MERGING ARRAYS
Program 9
KEEPING RECORDS OF PUBLICATIONS
Program 10
QUEUE AS AN ARRAY
Program 11
BUBBLE SORT SALARY
Program 12
STACK IN ARRAYS
Program 13
STRUCTURED QUERY LANGUAGE
Program 14
TELEPHONE DIRECTORY
Program 15
PROGRESS REPORT OF STUDENTS
Program 16
STACK AS LINKED LIST
Program 17
QUEUE AS LINKED LIST
Program 18
CIRCULAR QUEUE
Program 19
MULTIPLICATION OF TWO MATRIX
Program 20
FIBONNACCI SERIES
PROGRAM 1
REVERSING A STRING BY WORD OR BY SENTENCE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
clrscr();
int ch,i,j,k=0,p=0,l;
char st[50],sr[50];
cout<<"Enter Your Choice : "<<endl<<"(1)Each Word Reversing"<<endl<<"(2)Whole
Sentence Reversing"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
for(i=0;i<=l;i++)
{
if((st[i]==' ')||(st[i]=='\0'))
{
for(j=i-1;j>=p;j--)
{
sr[k]=st[j];
k++;
}
sr[k]=' ';
k++;
p=i+1;
}
}
for(i=0;i<p;i++)
cout<<sr[i];
break;
case 2:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
p=l-1;
for(i=l-1;i>=-1;i--)
{
if((st[i]==' ')||(i == -1))
{
for(j=i+1;j<=p;j++)
{
sr[k]=st[j];
k++;
}
sr[k]=' ';
k++;
p=i-1;
}
}
for(i=0;i<=l;i++)
cout<<sr[i];
break;
default:
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}
PROGRAM 2
BUBBLE SELECTION SORT
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch;
int i,j,x,k,z,l,m,n,o,p,a[50],small;
q:
cout<<"Enter the choice 1:Selection 2:Bubble 3:Exchange Selection 4:Insertion
5:Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}
for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
for(k=p;k>j;k--)
{
a[k]=a[k-1];
}
a[j]=small;
}
cout<<"Result"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;
case 2:
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if (a[i]>a[i+1])
{
x=a[i+1];
a[i+1]=a[i];
a[i]=x;
}
}
}
cout<<"Result"<<endl;
for (i=0;i<n;i++)
{
cout<<a[i];
cout<<endl;
}
break;
case 3 :
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}
for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
a[p]=a[j];
a[j]=small;
}
cout<<"Result"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;
case 4 :
int m=-32767;
cout<<"enter the no. of elements"<<endl;
cin>>n;
cout<<"enter the array"<<endl;
for(i=1;i<=n;i++)
{cin>>a[i];}
a[0]=m;
for(i=1;i<=n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
p=a[i];
for(k=i-1;k>=j;k--)
{a[k+1]=a[k];}
a[j]=p;
}
}
}
for(i=1;i<=n;i++)
{cout<<a[i];}
goto q;
case 5:
exit(0);
default:
cout<<"Wrong choice";
goto q;
}
getch();
}
PROGRAM 3
LINEAR AND BINARY SEARCH
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch,l,u,mid,c=0,i,n,e,a[50];
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<endl<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cout<<endl;
cin>>a[i];
}
cout<<"Enter the element to be searched for"<<endl;
cin>>e;
cout<<"Enter Your Choice 1:Linear 2:Binary Search";
cin>>ch;
switch(ch)
{
case 1 :
for(i=0;i<n;i++)
{
if(a[i]==e)
cout<<endl<<"Element is at "<<i+1<<" Position";
c=c+1;
}
if (c==0)
cout<<"Element Not Present";
break;
case 2:
c=0;
l=0;
u=n-1;
while(l<u)
{
mid=(l+u)/2;
if (a[mid]==e)
{
cout<<"Element is at "<<mid+1<<" Position "<<endl;
c++;
break;
}
if(e<a[mid])
u=mid-1;
if (e>a[mid])
l=mid+1;
}
if (c==0)
cout<<"Element not Present"<<endl;
break;
default :
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}
PROGRAM 4
CONVERT BINARY TO DECIMAL OR OCTAL & VICE-VERSA
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
clrscr();
int aa,g,h,s=0,ii=0,dd,b,ch;
men :
cout<<endl<<"Enter the choice - "<<endl<<endl<<"1:Binary to
Decimal"<<endl<<endl<<"2:Decimal to Binary"<<endl;
cout<<endl<<
"3:Binary to Octal"<<endl<<endl<<"4:Octal to
Binary"<<endl<<endl<<"5:exit"<<endl<<endl;;
cin>>ch;
switch(ch)
{
case 1:
s=0;
ii=0;
cout<<"Enter the Binary Digit"<<endl;
cin>>b;
while(b>0)
{
aa=b%10;
s=s+aa*(pow(2,ii));
ii=ii+1;
b=b/10;
}
cout<<"Answer ="<<s<<endl;
goto men;
case 2:
s=0;
ii=0;
cout<<"Enter the Decimal"<<endl;
cin>>dd;
while(dd>0)
{
aa=dd%2;
aa=aa*pow(10,ii);
s=s+aa;
ii=ii+1;
dd=dd/2;
}
cout<<"Answer ="<<s<<endl;
goto men;
case 3:
int oc,sum,p,r,n,a,i=0,x=0,d=0,j,o;
cout<<"ENTER THE BINARY DIGIT :"<<endl;
cin>>b;
while(b>0)
{
a=b%1000;
j=0;
d=0;
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<<"Answer ="<<x;
goto men;
case 4:
cout<<"Enter the Octel"<<endl;
cin>>oc;
i=0;
j=0;
sum=0;
s=0;
while(oc>0)
{
p=oc%1000;
while(p>0)
{
r=p%2;
s=s+r*pow(10,j);
j++;
p=p/2;
}
sum=sum+(s*pow(1000,i));
i++;
oc=oc/10;
j=0;
}
cout<<"Binary ="<<sum;
goto men;
case 5:
exit(0);
default:
cout<<"Wrong choice"<<endl;
}
getch();
PROGRAM 5
COUNT THE NUMBER OF RECORDS IN BINARY FILES
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>
struct stud
{
char name[50];
int rno;
};
void main()
{
clrscr();
stud st;
int i;
ofstream gpj("abcabc.dat",ios::binary|ios::trunc);
if(!gpj)
{
cout<<"File Cannot Be Created"<<endl;
}
else
{
int n;
cout<<"How many Students U Want to Enter ???? "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
gpj.write((char*)&st,sizeof(st));
}
gpj.close();
}
int a=0;
ifstream gp("abcabc.dat",ios::binary);
if(!gp)
{
cout<<"File Error"<<endl;
}
else
{
gp.seekg(0);
while(gp.read((char*)&st,sizeof(st)))
{
a++;
}
cout<<"No = "<<a;
gp.close();
}
getch();
}
PROGRAM 6
SUM OF ROW, COLUMN OR DIAGONAL
#include<iostream.h>
#include<conio.h>
#include<process.h>
}
cout<<"Sum of Row "<<i+1<<" = "<<s<<endl;
}
}
s=0;
for(int j=0;j<r;j++)
{
s=s+a[j][i];
}
cout<<"Sum of Column "<<i+1<<" = "<<s<<endl;
}}
void main()
{
clrscr();
int i,s,j,r,c,ch,a[50][50];
x:
cout<<"Entr Array Limit--(Enter only Row as R=C)"<<endl;
cin>>r;
cout<<"Enter Array"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
cin>>a[i][j];
}
}
y:
cout<<endl<<endl<<" Enter Choice :"<<endl<<"Sum of---- 1:main
2:Secondary 3.Rows 4.Columns 5.Re-enter 6.Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
s=sum1(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 2 :
s=sum2(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 3:
row(a,r);
goto y;
case 4:
col(a,r);
goto y;
case 5:
goto x;
case 6:
exit(0);
default :
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}
PROGRAM 7
MULTIPLICATION OF MATRIX
#include<iostream.h>
#include<conio.h>
class matrix
{
int x[10][10];
int m,n;
public:
void input();
void output();
void multiply(matrix,matrix);
};
void matrix::input()
{
cout<<"Enter Row"<<endl;
cin>>m;
cout<<"Enter Column"<<endl;
cin>>n;
cout<<"Matrix"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>x[i][j];
}
}
}
void matrix :: output()
{
for(int i=0;i<m;i++)
{
cout<<endl;
for(int j=0;j<n;j++)
{
cout<<x[i][j]<<" ";
}
}
}
void matrix :: multiply(matrix m1, matrix m2)
{
for(int i=0;i<m1.m;i++)
{
for(int j=0;j<m2.n;j++)
{
x[i][j]=0;
for(int k=0;k<m1.n;k++)
{
x[i][j]=x[i][j] +( m1.x[i][k] * m2.x[k][j]);
m=m1.m;
n=m2.n;
}
}
}
}
void main()
{
clrscr();
matrix m1,m2,m3;
m1.input();
m2.input();
m3.multiply(m1,m2);
m3.output();
getch();
}
PROGRAM 8
MERGING ARRAYS
#include<iostream.h>
#include<conio.h>
int c[100],i,j,k;
class merged
{
int na,nb,a[50],b[50];
public:
void merge();
void display();
void input();
}g;
void merged::merge()
{
int j=0;
int i=na-1;
while((i>0)&&(j<nb))
{
if (b[j]<a[i])
{
c[k]=a[i];
i--;
k++;
}
if (b[j]>a[i])
{
c[k]=b[j];
j++;
k++;
}
if (b[j]==a[i])
{
c[k]=b[j];
j++;
k++;
c[k]=a[i];
i--;
k++;
}
}
if(i==-1)
{
while(j<nb)
{
c[k]=b[j];
j++;
k++;
}
}
if(j==nb)
{
while(i>=0)
{
c[k]=a[i];
i--;
k++;
}
}
}
void merged::display()
{
for(i=0;i<na+nb;i++)
cout<<c[i];
}
void merged::input()
{
cout<<"Enter A Lim"<<endl;
cin>>na;
cout<<"Enter B Limit"<<endl;
cin>>nb;
cout<<"Enter array A"<<endl;
for(i=0;i<na;i++)
cin>>a[i];
cout<<"Enter Array B"<<endl;
for(j=0;j<nb;j++)
cin>>b[j];
}
void main()
{
clrscr();
g.input();
g.merge();
g.display();
getch();
}
PROGRAM 9
KEEPING RECORDS OF PUBLICATIONS
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class publication
{
char title[20];
float price;
public:
void getdata()
{
cout<<"Enter Title"<<endl;
gets(title);
cout<<"Enter Price"<<endl;
cin>>price;
}
void putdata()
{
cout<<"Title"<<endl;
puts(title);
cout<<"Price"<<endl;
cout<<price;
}
};
class book : protected publication
{
int pagecount;
public:
void putdata()
{
publication :: putdata();
cout<<endl<<"Pg Count"<<endl;
cout<<pagecount;
}
void getdata()
{
publication::getdata();
cout<<"Pg Count"<<endl;
cin>>pagecount;
}
};
getch();
}
PROGRAM 10
QUEUE AS AN ARRAY
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch,i,rear=-1,front=-1,queue[10];
x:
cout<<endl<<endl;
cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
if(front==-1)
{front=0;}
rear++;
if(rear<=9)
{cout<<"Enter The Element"<<endl;
cin>>queue[rear];
cout<<"Queue is"<<endl;
for(i=0;i<=rear;i++)
{cout<<queue[i];}
}
else
{cout<<"****************QUEUE OVERFLOW****************";
}
goto x;
case 2:
if(rear==-1)
{rear=0;}
if(front==-1)
{cout<<"*************** UNDER FLOW **********"<<endl;goto x;}
else
if(rear==front)
{queue[front]='\o';
front=-1;
rear=-1;
goto x;
}
else
{queue[front]='\o';
front++;
}
cout<<"Queue is"<<endl;
for(i=0;i<=rear;i++)
{cout<<queue[i];}
goto x;
case 3:
exit(0);
default :
goto x;
}}
PROGRAM 11
BUBBLE SORT SALARY
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct employee
{
int eno;
char name[25];
float salary;
}e[10];
void main()
{
clrscr();
int n;
void bubb(employee,int);
cout<<"Enter No of Employees"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter Employee Name :";
gets(e[i].name);
cout<<endl<<"Enter Employee Code :";
cin>>e[i].eno;
cout<<endl<<"Enter Employee Salary :";
cin>>e[i].salary;
}
bubb(e[10],n);
getch();
}
void bubb(employee e,int n)
{
employee x;
for(int j=0;j<n;j++)
{
if(e[j].salary<e[j+1].salary)
{
for(int i=j;i<n-1;i++)
{
x=e[i];
e[i]=e[i+1];
e[i+1]=x;
}
}
}
for(int i=0;i<n;i++)
{
cout<<endl;
cout<<e[i].name<<endl<<e[i].salary<<endl<<e[i].eno;
}
}
PROGRAM 12
STACK IN ARRAYS
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch,i,top=-1,stack[5];
x:
cout<<endl<<endl;
cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
top++;
if(top<=4)
{
cout<<"Enter The Element"<<endl;
cin>>stack[top];
cout<<"The Stack is"<<endl;
for(i=0;i<=top;i++)
cout<<stack[i];
goto x;
}
else
{
cout<<" ************* Stack OVERFLOW ********** "<<endl;
goto x;}
case 2:
if(top>=0)
{
top--;
cout<<"Stack is"<<endl;
for(i=0;i<=top;i++)
cout<<stack[i];
goto x;
}
else
{
cout<<"************** Stack UNDER FLOW ***********"<<endl;
goto x;
}
case 3:
exit(0);
default :
cout<<"WRONG CHOICE !!!!!!!!!!! "<<endl;
goto x;
}
PROGRAM 13
STRUCTURED QUERY LANGUAGE
TABLE: STUDENT
No: Name Stipen Stream Avgmar Class
d k Grade
SELECT NAME
FROM STUDENT
WHERE STREAM=’NONMEDICAL’;
OUTPUT
NAME:
SABINA
RUBINA
VIKAS
2) List the names of those students who are in class 12 sorted by stipend.
SELECT NAME,STIPEND FROM STUDENT
WHERE CLASS=’12’
ORDER BY STIPEND;
OUTPUT
NAME STIPEND
DIVYA 300.00
MOHAN 300.00
ARUN 350.00
KARAN 400.00
JOHN 400.00
RUBINA 450.00
VIKAS 500.00
OUTPUT
ROBERT
MOHAN
DIVYA
ARUN
JOHN
KARAN
RUBINA
DIVAKAR
SABINA
VIKAS
SELECT COUNT(GRADE)
FROM STUDENT
WHERE GRADE=”A”;
OUTPUT
4
5) To insert a new student in the table and fill the columns with some values
OUTPUT
ONE ROW ADDED
PROGRAM 14
TELEPHONE DIRECTORY
#include<iostream.h>
#include<conio.h>
struct name
{char first[50],mid[50],last[50];};
struct phone
{char area[50],exch[50];
long no;};
class rec
{name n;
phone p;
public :
void reci();
void disp();
};
void rec::reci()
{
clrscr();
cout<<"Enter First Name :";
cin>>n.first;
cout<<endl;
cout<<"Enter Mid Name :";
cin>>n.mid;
cout<<endl;
cout<<"Enter Last Name :";
cin>>n.last;
cout<<endl;
cout<<"Enter Area :";
cin>>p.area;
cout<<endl;
cout<<"Enter Exchange :";
cin>>p.exch;
cout<<endl;
cout<<"Enter No :";
cin>>p.no;
}
void rec::disp()
{
clrscr();
cout<<endl;
cout<<endl;
cout<<"First Name :";
cout<<n.first;
cout<<endl;
cout<<endl;
cout<<"Mid Name :";
cout<<n.mid;
cout<<endl;
cout<<endl;
cout<<"Last Name :";
cout<<n.last;
cout<<endl;
cout<<endl;
cout<<"Area :";
cout<<p.area;
cout<<endl;
cout<<endl;
cout<<"Exchange :";
cout<<p.exch;
cout<<endl;
cout<<endl;
cout<<"No :";
cout<<p.no;
cout<<endl;
cout<<endl;
getch();
}
void main()
{
rec t;
t.reci();
t.disp();
getch();
}
PROGRAM 15
PROGRESS REPORT OF STUDENTS
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>
struct stud
{
public:
int rno;
char name[50];
int cls;
float marks[5];
};
struct res
{
int rnos;
float total;
char grade;
float perc;
};
void main()
{
clrscr();
float tm;
int i,ma;
stud st;
res rt;
ofstream gpj("stud.dat",ios::binary|ios::trunc);
ofstream jpg("result.dat",ios::binary|ios::trunc);
if(!gpj||!jpg)
cout<<"File Cannot Be Created"<<endl;
int n;
if(gpj&&jpg)
{
cout<<"How mamy Students U Want to Enter ???? "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
cout<<"Enter Class"<<endl;
cin>>st.cls;
for(ma=0;ma<5;ma++)
{
cout<<"Enter Marks in subj"<<ma+1<<endl;
cin>>st.marks[ma];
}
gpj.write((char*)&st,sizeof(st));
tm=st.marks[0]+st.marks[2]+st.marks[1]+st.marks[3]+st.marks[4];
rt.perc=tm/5;
if(rt.perc>=75)
rt.grade='A';
if((rt.perc>=60)&&(rt.perc<75))
rt.grade='B';
if((rt.perc>=50)&&(rt.perc<60))
rt.grade='C';
if((rt.perc>=40)&&(rt.perc<50))
rt.grade='D';
if(rt.perc<40)
rt.grade='F';
rt.rnos=st.rno;
jpg.write((char*)&rt,sizeof(rt));
}}
gpj.close();
jpg.close();
int r,k=0;
ifstream god("result.dat",ios::binary);
if(!god)
{
cout<<"File Erroe"<<endl;
}
else
{
god.seekg(0);
cout<<endl<<"ENTER ROLL NO"<<endl;
cin>>r;
while(god.read((char*)&rt,sizeof(rt)))
{
if(r==rt.rnos)
{
k++;
cout<<" Roll no :"<<rt.rnos<<endl<<" Percentage :"<<rt.perc<<endl<<"
Grade :"<<rt.grade;
}
}
if(k==0)
cout<<"Roll no not present"<<endl;
god.close();
}
getch();
}
PROGRAM 16
STACK AS LINKED LIST
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct node
{char name[20];
int age;
node *link;
}*ptr=NULL,*save=NULL;
class stack
{node *top;
public:
stack()
{top=NULL;
}
void stackpush();
void stackpop();
void display();
}st;
void stack::stackpush()
{ptr=new node;
if(ptr==NULL)
{cout<<"Overflow ";
}
else
{cout<<"Enter the name ";
gets(ptr->name);
cout<<"Enter the age ";
cin>>ptr->age;
ptr->link=NULL;
if(top==NULL)
{top=ptr;
}
else
{ptr->link=top;
top=ptr;
}
}
}
void stack::stackpop()
{if(top==NULL)
{cout<<"Underflow ";
}
else
{save=top;
top=top->link;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
}
void stack::display()
{if(top==NULL)
{cout<<"No elements.."<<endl;
}
else
{ptr=top;
while(ptr!=NULL)
{cout<<"\nName ";
puts(ptr->name);
cout<<"Age "<<ptr->age;
ptr=ptr->link;
}
}
}
void main()
{clrscr();
int ch;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:st.stackpush();
goto X;
case 2:st.stackpop();
goto X;
case 3:st.display();
goto X;
default:cout<<"Wrong choice ";
goto X;
case 4:exit(0);
}
getch();
}
PROGRAM 17
QUEUE AS LINKED LIST
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
struct node
{char name[20];
int age;
node *link;
}*ptr=NULL,*save=NULL;
class queue
{node *rear,*front;
public:
queue()
{rear=NULL;
front=NULL;
}
void queins();
void quedel();
void display();
}q;
void queue::queins()
{ptr=new node;
if(ptr==NULL)
{cout<<"Queue overflow ";
}
else
{cout<<"Enter the name ";
gets(ptr->name);
cout<<"Enter the age ";
cin>>ptr->age;
ptr->link=NULL;
if(rear==NULL)
{rear=ptr;
front=ptr;
}
else
{rear->link=ptr;
rear=ptr;
}
}
}
void queue::quedel()
{if(rear==NULL)
{cout<<"Queue underflow ";
}
else
{if(front==rear)
{save=front;
front=NULL;
rear=NULL;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
else
{save=front;
front=front->link;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
}
}
void queue::display()
{if(rear==NULL)
{cout<<"No elements ";
}
else
{ptr=front;
while(ptr!=NULL)
{cout<<"Name ";
puts(ptr->name);
cout<<"Age "<<ptr->age;
ptr=ptr->link;
}
}
}
void main()
{clrscr();
int ch;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:q.queins();
goto X;
case 2:q.quedel();
goto X;
case 3:q.display();
goto X;
case 4:exit(0);
default:cout<<"Wrong choice ";
goto X;
}
getch();
}
PROGRAM 18
CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{int data[10];
int front,rear;
public:
queue()
{front=-1;
rear=-1;
}
void add();
void remove();
void display();
};
void queue::add()
{if((rear+1==front)||(rear==9&&front==0))
{cout<<"Overflow ";
}
else
{if((rear==-1) &&(front==-1))
{rear=0;
front=0;
}
else if(rear==9)
{rear=0;
}
else
{rear++;
}
cout<<"Enter the element ";
cin>>data[rear];
}
}
void queue::remove()
{if(front==-1&&rear==-1)
{cout<<"Underflow ";
}
else
{if(front==9)
{front=0;
}
else if(front==rear)
{front=-1;
rear=-1;
}
else
{front++;
}
}
}
void queue::display()
{int i=0,n=9;
if(rear==-1)
{cout<<"No elements.."<<endl;
}
else
{ if(rear>front)
{for(i=0;i<front;i++)
{cout<<"_";
}
for(i=front;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<n;i++)
{cout<<"_";
}
}
else
{for(i=0;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<front;i++)
{cout<<"_";
}
for(i=front;i<n;i++)
{cout<<data[i];
}
}}
}
void main()
{clrscr();
int ch;
queue queue;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:queue.add();
goto X;
case 2:queue.remove();
goto X;
case 3:queue.display();
goto X;
case 4:exit(0);
}
getch();
}
PROGRAM 19
MULTIPLICATION OF TWO MATRIX
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,na,br,bc,r,c,k,s,a[50][50],b[50][50],e[50][50];
cout<<"Enter limit A"<<endl;
cin>>r>>c;
cout<<"Enter Limit B"<<endl;
cin>>br>>bc;
if(c==br)
{
cout<<"Enter Array A"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter Array B"<<endl;
for(i=0;i<br;i++)
{
for(j=0;j<bc;j++)
{
cin>>b[i][j];
}
}
if(c==br)
{
for(i=0;i<r;i++)
{
for(j=0;j<bc;j++)
{
e[i][j]=0;
for(k=0;k<c;k++)
{
e[i][j] = e[i][j] + a[i][k]*b[k][j];
}
}
}
}
for(i=0;i<r;i++)
{ cout<<endl;
for(j=0;j<bc;j++)
{
cout<<" "<<e[i][j];
}
}
}
else
cout<<"INCORRECT DIMENSION FOR ARRAY"<<endl;
getch();
}
PROGRAM 20
FIBONNACCI SERIES
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int i,x=0,y=1,s;
cout<<x<<endl<<y<<endl;
for(i=3;i<=10;i++)
{
s=x+y;
cout<<s<<endl;
x=y;
y=s;
}
getch();
}
Thank you