CS Practical File
CS Practical File
Output
ARRAY-2
Q: Write a program to transfer contents from two arrays to another array such that the even
places of the resultant array should get contents from first array and odd places from the second.
#include<iostream.h>
#include<conio.h>
void merge(int X[], int Y[], int n)
{
int Z[20], j=0, k=0;
for(int i=0; i<n; i++)
{
if(i%2==0)
{
Z[i]=X[j];
j++;
}
else
{
Z[i]=Y[k];
k++;
}
}
cout<<"\nResultant array: ";
for(i=0; i<n; i++)
cout<<Z[i]<<" ";
}
void main()
{
clrscr();
int A[20], B[20], size;
cout<<"\nEnter size of the arrays: ";
cin>>size;
cout<<"\nEnter elements in first array:\n";
for(int z=0; z<size; z++)
cin>>A[z];
cout<<"\nEnter elements in second array:\n";
for(z=0; z<size; z++)
cin>>B[z];
merge(A,B,size);
getch();
}
OUTPUT
STURCTURE
Q: Write a program to illustrate a use of structure
#include<iostream.h>
#include<conio.h>
int i;
struct zoo
{
int id;
char name[20];
int age;
}z[3];
void show(zoo z[])
{
for(i=0; i<3; i++)
{
if(z[i].age>10)
{
cout<<endl<<"ID: "<<z[i].id;
cout<<"\nName: "<<z[i].name;
cout<<"\nAge: "<<z[i].age;
}
}
}
void main()
{
clrscr();
cout<<"Enter the details of 3 animals: "<<endl;
for(int i=0; i<3; i++)
{
cout<<"Enter ID: ";
cin>>z[i].id;
cout<<"Enter Name: ";
cin>>z[i].name;
cout<<"Enter Age: ";
cin>>z[i].age;
cout<<endl;
}
cout<<"Details of the animals aged above 10 years: ";
show(z);
getch();
}
Output
FUNCTION OVERLOADING
Q: Write a program to calculate the area of a circle, square and rectangle using function overload
#include<iostream.h>
#include<conio.h>
float area(double r)
{ cout<<3.14*r*r;
}
void area(int l, int w)
{ cout<<l*w;
}
void area(int b)
{ cout<<b*b;
}
void main()
{
clrscr();
area(5); cout<<endl; area(3.4); cout<<endl; area(2,5);
getch();
}
OUTPUT
STRING - 1
Q: Write a program to reverse the characters of a string
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{ clrscr();
int i, length;
char temp, str[20];
cout<<"Enter your string: ";
gets(str);
length=strlen(str);
for(i=0; i<length/2; i++)
{ temp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=temp;
}
cout<<"\nReversed string is: "<<str;
getch();
}
OUTPUT
STRING – 2
Q: Write a program to display the no. of vowels in a string
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
int v=0;
char A[30];
cout<<"\nEnter string: ";
gets(A);
for(int i=0; i<strlen(A); i++)
{
if(A[i]=='a' || A[i]=='A')
v++;
else if(A[i]=='e' || A[i]=='E')
v++;
else if(A[i]=='i' || A[i]=='I')
v++;
else if(A[i]=='o' || A[i]=='O')
v++;
else if(A[i]=='u' || A[i]=='U')
v++;
}
cout<<"\nNo of vowels: "<<v;
getch();
}
OUTPUT
CLASSES AND OBJECTS
Q: Write a program to define a class Resort which accepts room no, name, charges, and days to
calculate amount
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class resort
{
int rno, days;
char name[20];
float charges, amount;
float compute()
{
float amount=charges*days;
if(amount>11000)
amount=1.02*days*charges;
return amount;
}
public:
void getinfo()
{
cout<<"Enter room no.: ";
cin>>rno;
cout<<"Enter name: ";
gets(name);
cout<<"Enter charges: ";
cin>>charges;
cout<<"Enter no. of days: ";
cin>>days;
}
void dispinfo()
{
cout<<"\nRoom no.: "<<rno;
cout<<"\nName: "<<name;
cout<<"\nCharges: "<<charges;
cout<<"\nNo. of days: "<<days<<endl;
cout<<"\nAmount: "<<compute();
}
};
void main()
{
clrscr();
resort r;
r.getinfo();
r.dispinfo();
getch();
}
Output
CONSTRUCTOR DESTRUCTOR
Q: Write a program to show types of constructors
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class user
{ int uid;
char uname[20];
char status;
public:
user()
{
cout<<"Default constructor\n";
}
user(int x,char * y,char z)
{
cout<<"Parameterised constructor\n";
uid=x;
status=z;
strcpy(uname,y);
}
user(user &s)
{
cout<<"Copy constructor\n";
uid=s.uid;
status=s.status;
strcpy(uname,s.uname);
}
~user()
{
cout<<"Destructing user\n";
}
void input()
{
cout<<"Enter uid\n";
cin>>uid;
cout<<"Enter uname\n";
gets(uname);
cout<<"Enter status\n";
cin>>status;
}
void output()
{
cout<<"Uid "<<uid<<endl;
cout<<"Uname "<<uname<<endl;
cout<<"status "<<status<<endl;
}
};
void main()
{
clrscr();
user a,b(1,"Daksh",'G'),c(b);
a.output();
b.output();
c.output();
getch();
}
Output
TEXT FILE - 1
Q: Write a program to read and display four letter words from a text file.
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
ifstream f;
f.open("story.txt");
char word[20];
while(!f.eof())
{
f>>word;
if(strlen(word)==4)
cout<<word<<" ";
}
getch();
f.close();
}
Output
TEXT FILE - 2
Q: Write a program to count and display no. of times "that" occurs in a text file.
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void input()
{ ofstream f("test.txt");
char ch[50];
cout<<"Enter data\n\n";
gets(ch);
f<<ch;
}
void main()
{ clrscr();
input();
ifstream f("test.txt");
int n=0;
char ch[10];
while(!f.eof())
{
f>>ch;
if(!strcmpi("that",ch))
n++;
}
cout<<"\nNumber of times word that occurs is "<<n;
getch();
}
Output
BINARY FILE – 1
Q: Write a program to input, display and search for details in a binary file
#include<fstream.h>
#include<process.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
class Toy
{
int tid;
char tname[20];
float price;
public:
void input()
{
cout<<"Enter toy ID: ";
cin>>tid;
cout<<"Enter toy name: ";
gets(tname);
cout<<"Enter price: ";
cin>>price;
}
void output()
{
cout<<"\nDetails: ";
cout<<"\nToy ID: "<<tid;
cout<<"\nToy name: "<<tname;
cout<<"\nPrice: "<<price;
}
char * acc_name()
{
return tname;
}
};
void enter()
{
fstream f("toy.dat",ios::binary|ios::app);
Toy t;
cout<<endl;
t.input();
f.write((char*)&t,sizeof(t));
f.close();
}
void show()
{
ifstream f("toy.dat",ios::binary);
Toy t;
while(f.read((char*)&t,sizeof(t)))
{
t.output();
cout<<endl;
}
f.close();
}
void search()
{ char name[20];
cout<<"Enter toy name: ";
gets(name);
ifstream f("toy.dat",ios::binary);
Toy t;
while(f.read((char*)&t,sizeof(t)))
{ if(!strcmpi(t.acc_name(),name))
{
t.output();
cout<<endl;
}
}
f.close();
}
void main()
{ clrscr();
while(1)
{
int choice;
cout<<"\nTo input toy details, press 1";
cout<<"\nTo show toy details, press 2";
cout<<"\nTo search a toy, press 3";
cout<<"\nTo exit, press 0";
cout<<"\nEnter choice: ";
cin>>choice;
switch(choice)
{
case 1: enter();
break;
case 2: show();
break;
case 3: search();
break;
case 0:
default: exit(0);
}
}
}
OUTPUT
BINARY FILE – 2
Q: Write a program to read, write display and modify student records from binary file.
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class student
{
int rno;
char sname[20];
int marks;
public:
void getdata()
{
cout<<"Enter roll no.: ";
cin>>rno;
cout<<"Enter name: ";
gets(sname);
cout<<"Enter marks: ";
cin>>marks;
}
void showdata()
{
cout<<"Roll no: "<<rno<<endl;
cout<<"Name: "<<sname<<endl;
cout<<"Marks: "<<marks<<endl;
}
int getroll()
{
return rno;
}
void crno(int r)
{
rno=r;
}
void cname(char *n)
{
strcpy(sname,n);
}
void cmarks(int m)
{
marks=m;
}
};
void write()
{
fstream f("student.dat",ios::binary|ios::app);
student s;
s.getdata();
f.write((char*)&s,sizeof(s));
f.close();
}
void modify()
{
fstream f("student.dat",ios::binary|ios::out|ios::in);
student s;
int r;
int pos;
cout<<"Enter roll no. to be modified\n";
cin>>r;
while(!f.eof())
{
pos=f.tellg();
f.read((char*)&s,sizeof(s));
if(s.getroll()==r)
{
menu:
int choice;
char c;
cout<<"What do you want to modify\n";
cout<<"1.roll no. \n 2.name \n 3.marks\n";
cin>>choice;
if(choice==1)
{
int roll;
cout<<"Enter new roll no.\n";
cin>>roll;
s.crno(roll);
}
else if(choice==2)
{
char name[20];
cout<<"Enter new name\n";
gets(name);
s.cname(name);
}
else if(choice==3)
{
int marks;
cout<<"Enter new marks\n";
cin>>marks;
s.cmarks(marks);
}
cout<<"Do you want to modify this record again(Y\N)\n";
cin>>c;
if(c=='Y' || c=='y')
goto menu;
f.seekg(pos);
f.write((char*)&s,sizeof(s));
}
}
f.close();
}
void read()
{
ifstream f("student.dat",ios::binary);
student s;
while(f.read((char*)&s,sizeof(s)))
s.showdata();
f.close();
}
void main()
{
clrscr();
int ch;
char choice;
do
{
cout<<"\n1 - ADD A MEMBER";
cout<<"\n2 - SHOW ALL MEMBERS";
cout<<"\n3 - MODIFY A MEMBER";
cout<<"\nENTER YOUR CHOICE ";
cin>>ch;
switch(ch)
{
case 1: write();
break;
case 2: read();
break;
case 3: modify();
break;
default: cout<<"\nWRONGH CHOICE";
}
cout<<"\nDO YOU WANT TO CONITNUE?";
cin>>choice;
}while(choice=='Y'||choice=='y');
getch();
}
OUTPUT
INHERITANCE
Q: Write a program showing inheritance of parameterised constructor and destructor
#include<fstream.h>
#include<conio.h>
class a
{
int ano;
public:
a(int x)
{
ano=x;
cout<<"Value of a "<<ano<<endl;
}
~a()
{
cout<<"Destructing a\n";
}
};
class b
{
int bno;
public:
b(int x)
{
bno=x;
cout<<"Value of b"<<bno<<endl;
}
~b()
{
cout<<"Destructing b\n";
}
};
class c:private a, private b
{
int cno;
public:
c(int a1,int b1,int c1):b(a1),a(b1)
{
cno=c1;
cout<<"Value of c"<<cno<<endl;
}
~c()
{
cout<<"Destructing c\n";
}
};
void main()
{
clrscr();
c ob(1,2,3);
getch();
}
Output
MULTIPLE INHERITANCE
Q: Write a program showing multiple inheritance
#include<iostream.h>
#include<conio.h>
class A
{ int a1;
protected:
int a2;
public:
void enter()
{
cout<<"Enter a1 and a2\n";
cin>>a1>>a2;
}
void show()
{
cout<<"a1 "<<a1<<endl<<"a2 "<<a2<<endl;
}
};
class B
{ int b1;
protected:
int b2;
public:
void enter()
{ cout<<"Enter b1 and b2\n";
cin>>b1>>b2;
}
void show()
{ cout<<"b1 "<<b1<<endl<<"b2 "<<b2<<endl;
}
};
class C:private A,private B
{
int c1;
protected:
int c2;
public:
void enter()
{ A::enter();
B::enter();
cout<<"Enter c1 and c2\n";
cin>>c1>>c2;
}
void show()
{
A::show();
B::show();
cout<<"c1 "<<c1<<endl<<"c2 "<<c2<<endl;
}
};
void main()
{
clrscr();
C c;
c.enter();
c.show();
getch();
}
OUTPUT
POINTERS – 1
Q: Write a program to print character array using a pointer
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char a[]="Daksh Kant";
char *ptr=a;
for(int i=0; ptr[i]!='\0';)
{
cout<<ptr;
ptr++;
cout<<endl;
}
getch();
}
OUTPUT
Pointers-2
Program to illustrate pointers
#include<iostream.h>
#include<conio.h>
void Translate(char *P1, char *P2)
{
char *Q;
Q=P2++;
P2=++P1;
P1=++Q;
cout<<P1<<"*"<<P2<<endl;
}
void main()
{
char A1[]="One", A2[]="Two";
Translate(A1,A2);
cout<<A1<<"&"<<A2<<endl;
}
Output
LINEAR SEARCH
Program to illustrate Linear Search:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int A[20], size, temp, found=0;
cout<<"Enter the size of your array: ";
cin>>size;
cout<<"\nEnter elements: ";
for(int i=0; i<size; i++)
{
cin>>A[i];
}
cout<<"\nYour array: ";
for(i=0; i<size; i++)
{
cout<<A[i]<<" ";
}
cout<<"\n\nEnter the element you want to search: ";
cin>>temp;
for(i=0; i<size; i++)
{
if(temp==A[i])
{
cout<<"\nElement found at position "<<i+1;
found=1;
}
}
if(found==0)
{
cout<<"\nYour element does not exist";
}
getch();
}
OUTPUT
BINARY SEARCH
Program to illustrate Binary Search:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int A[5], first=0, last=4, mid, temp;
cout<<"Enter 5 elements in ascending/descending order: \n";
for(int i=0; i<5; i++)
{
cin>>A[i];
}
cout<<"\n\nYour array: ";
for(i=0; i<5; i++)
{
cout<<A[i]<<" ";
}
cout<<"\nEnter element to find: ";
cin>>temp;
do
{
mid=(first+last)/2;
if(A[mid]==temp)
{
cout<<"\nElement found at position "<<mid+1;
break;
}
else if(A[mid]<temp)
{
first=mid+1;
}
else
{
last=mid-1;
}
}while(first<=last);
if(first>last)
{
cout<<"\nElement could not be found";
}
getch();
}
OUTPUT
SELECTION SORT
Program to illustrate Selection Sort:
#include<iostream.h>
#include<conio.h>
void asc(int A[], int size)
{
int i, j, temp;
for(i=0; i<size-1; i++)
{
for(j=i+1; j<size; j++)
{
if(A[i]>A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
cout<<"\nSorted elements: ";
for(i=0; i<size; i++)
{
cout<<A[i]<<" ";
}
}
void desc(int A[], int size)
{
int i, j, temp;
for(i=0; i<size-1; i++)
{
for(j=i+1; j<size; j++)
{
if(A[i]<A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
cout<<"\nSorted elements:"<<endl;
for(i=0; i<size; i++)
{
cout<<A[i]<<" ";
}
}
void main()
{
clrscr();
int A[20], size, choice;
char ch;
cout<<"Enter the size of your array: ";
cin>>size;
cout<<"\nEnter elements: "<<endl;
for(int i=0; i<size; i++)
{
cin>>A[i];
}
do
{
cout<<"\n1 for ASCENDING ORDER";
cout<<"\n2 for DESCENDING ORDER";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: asc(A, size);
break;
case 2: desc(A, size);
break;
default: cout<<"\Enter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ch;
}while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
BUBBLE SORT
Program to illustrate Bubble Sort:
#include<iostream.h>
#include<conio.h>
void asc(int A[], int size)
{
int i, j, temp;
for(i=0; i<size-1; i++)
{
for(j=i+1; j<size; j++)
{
temp=A[i+1];
if(A[i+1]<A[i])
{
A[i+1]=A[i];
A[i]=temp;
}
}
}
cout<<"\nSorted elements: ";
for(i=0 ;i<size; i++)
{
cout<<A[i]<<" ";
}
}
void desc(int A[], int size)
{
int i, j, temp;
for(i=0; i<size-1; i++)
{
for(j=i+1; j<size; j++)
{
temp=A[i+1];
if(A[i+1]>A[i])
{
A[i+1]=A[i];
A[i]=temp;
}
}
}
cout<<"\nSorted elements: ";
for(i=0 ;i<size; i++)
{
cout<<A[i]<<" ";
}
}
void main()
{
clrscr();
int A[20], size, i, choice;
char ch;
cout<<"Enter size of your array: ";
cin>>size;
cout<<"\nEnter elements: ";
for(i=0; i<size; i++)
{
cin>>A[i];
}
do
{
cout<<"\n1 for ASCENDING ORDER";
cout<<"\n2 for DESCENDING ORDER";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: asc(A, size);
break;
case 2: desc(A, size);
break;
default: cout<<"\Enter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ch;
}while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
INSERTION SORT
Program to illustrate Insertion Sort:
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[20], size, i, j, temp;
cout<<"Enter size of your array: ";
cin>>size;
cout<<"\nEnter elements: ";
for(i=0; i<size; i++)
cin>>A[i];
for(i=1; i<size; i++)
{
temp=A[i];
j=i-1;
while(temp<A[j] && j>=0)
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=temp;
}
cout<<"\nSorted elements: ";
for(i=0 ;i<size; i++)
{
cout<<A[i]<<" ";
}
getch();
}
OUTPUT
STATIC STACK
Program to illustrate a Static Stack:
#include<iostream.h>
#include<conio.h>
struct book
{
int bno;
};
void push(book B[], int &top)
{
if(top==3)
{
cout<<"\nOverflow";
}
else
{
top++;
cout<<"\nEnter book no: ";
cin>>B[top].bno;
}
}
void pop(book B[], int &top)
{
if(top==-1)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\nDeleted data is: "<<B[top].bno;
top--;
}
}
void show(book B[], int &top)
{
if(top==-1)
{
cout<<"\nStack is empty";
}
else
{
for(int i=top; i>-1; i--)
{
cout<<B[i].bno<<" ";
}
}
}
void main()
{
clrscr();
book B[4];
int top=-1, choice;
char ch;
do
{
cout<<"\n1 to PUSH";
cout<<"\n2 to POP";
cout<<"\n3 to SHOW";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: push(B,top);
break;
case 2: pop(B,top);
break;
case 3: show(B,top);
break;
default: cout<<"\nEnter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ch;
}
while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
DYNAMIC STACK
Program to illustrate Dynamic Stack:
#include<iostream.h>
#include<conio.h>
struct book
{
int bno;
book *ptr;
};
class bookstack
{
book *top;
public:
bookstack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void bookstack::push()
{
book *temp=new book;
cout<<"\nEnter book number: ";
cin>>temp->bno;
temp->ptr=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->ptr=top;
top=temp;
}
}
void bookstack::pop()
{
book *temp;
if(top==NULL)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\nDeleted data is "<<top->bno;
temp=top;
top=top->ptr;
delete temp;
}
}
void bookstack::show()
{
if(top==NULL)
{
cout<<"\nStack is empty";
}
else
{
book* temp;
temp=top;
while(temp!=NULL)
{
cout<<temp->bno;
cout<<endl;
temp=temp->ptr;
}
}
}
void main()
{
clrscr();
bookstack b;
int choice;
char ch;
do
{
cout<<"\n1 to PUSH";
cout<<"\n2 to POP";
cout<<"\n3 to SHOW";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: b.push();
break;
case 2: b.pop();
break;
case 3: b.show();
break;
default: cout<<"\nEnter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ch;
}while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
STATIC QUEUE
Program to illustrate Static Queue:
#include<iostream.h>
#include<conio.h>
void add(int d[], int &first,int &last)
{
if(first==3)
{
cout<<"Overflow";
return;
}
else
{
int r;
cout<<"\nEnter number: ";
cin>>r;
if(first==-1)
{
first++;
last++;
}
else
{
last++;
}
d[last]=r;
}
}
void remove(int d[], int &first, int &last)
{
if(first==-1)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\nDeleted data is "<<d[first];
if(first==last)
{
first=-1;
last=-1;
}
else
{
first++;
}
}
}
void show(int d[], int &first, int &last)
{
if(first==-1)
{
cout<<"\nQueue is empty";
}
else
{
for(int i=first; i<=last; i++)
{
cout<<d[i]<<" ";
}
}
}
void main()
{
clrscr();
int d[4], first=-1, last=-1, choice;
char ch;
do
{
cout<<"1 to ADD";
cout<<"\n2 to DELETE";
cout<<"\n3 to SHOW";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: add(d, first, last);
break;
case 2: remove(d, first, last);
break;
case 3: show(d, first, last);
break;
default: cout<<"\nEnter a valid choice";
}
cout<<"Do you want to continue? Y/N ";
cin>>ch;
}while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
DYNAMIC QUEUE
Program to illustrate Dynamic Queue:
#include<iostream.h>
#include<conio.h>
struct book
{
int bno;
book *ptr;
};
class bookstack
{
book *top;
public:
bookstack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void bookstack::push()
{
book *temp=new book;
cout<<"\nEnter book number: ";
cin>>temp->bno;
temp->ptr=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->ptr=top;
top=temp;
}
}
void bookstack::pop()
{
book *temp;
if(top==NULL)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\nDeleted data is "<<top->bno;
temp=top;
top=top->ptr;
delete temp;
}
}
void bookstack::show()
{
if(top==NULL)
{
cout<<"\nStack is empty";
}
else
{
book* temp;
temp=top;
while(temp!=NULL)
{
cout<<temp->bno;
cout<<endl;
temp=temp->ptr;
}
}
}
void main()
{
clrscr();
bookstack b;
int choice;
char ch;
do
{
cout<<"\n1 to PUSH";
cout<<"\n2 to POP";
cout<<"\n3 to SHOW";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: b.push();
break;
case 2: b.pop();
break;
case 3: b.show();
break;
default: cout<<"\nEnter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ch;
}while(ch=='y' || ch=='Y');
getch();
}
OUTPUT
CIRCULAR QUEUE
Program to illustrate circular queue:
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int S=4;
int data[S];
int front=-1;
int rear=-1;
void add()
{
if((front==0) && (rear==S-1) || (front==rear+1))
{
cout<<"\nOverflow";
return;
}
else
{
int ele;
cout<<"\nEnter element: ";
cin>>ele;
if(rear==-1)
front=rear=0;
else if(rear==S-1)
rear=0;
else
rear++;
data[rear]=ele;
}
}
void remove()
{
if(front==-1)
{
cout<<"\nUnderflow";
return;
}
else
{
cout<<"\nDeleted data is "<<data[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(rear<front && front==S-1)
front=0;
else
front++;
}
}
void show()
{
if(front==-1)
cout<<"\nQueue is empty";
else
{
if(rear>=front)
{
for(int i=front; i<=rear; i++)
cout<<data[i]<<" ";
}
else
{
for(int i=0; i<=rear; i++)
cout<<data[i]<<" ";
}
}
}
void main()
{
clrscr();
int choice;
char ans;
do
{
cout<<"\n1 to ADD";
cout<<"\n2 to DELETE";
cout<<"\n3 to SHOW";
cout<<"\n0 to EXIT";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: add();
break;
case 2: remove();
break;
case 3: show();
break;
case 0: exit(0);
break;
default: cout<<"\nEnter a valid choice";
}
cout<<"\nDo you want to continue? Y/N ";
cin>>ans;
}while(ans=='y' || ans=='Y');
getch();
}
OUTPUT
MySQL-1
Display all records from the employee table
mysql> select * from emp;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
| 7369 | Smith | Clerk | 7902 | 1980-12-17 | 800 | NULL | NULL |
| 7499 | Allen | Salesman | 7698 | 1981-02-20 | 1600 | 300 | 30 |
| 7521 | Ward | Salesman | 7698 | 1981-02-22 | 1250 | 500 | 30 |
| 7566 | Jones | Manager | 7839 | 1981-04-02 | 2975 | NULL | 20 |
| 7654 | Martin | Salesman | 7698 | 1981-09-28 | 1250 | 1400 | 30 |
| 7698 | Blake | Manager | 7839 | 1981-05-01 | 2850 | NULL | 30 |
| 7782 | Clark | Manager | 7839 | 1981-06-09 | 2450 | NULL | 10 |
| 7788 | Scott | Analyst | 7566 | 1983-12-09 | 3000 | NULL | 20 |
| 7839 | King | President | NULL | 1981-11-17 | 5000 | NULL | 10 |
| 7844 | Turner | Salesman | 7698 | 1981-09-08 | 1500 | 0 | 30 |
| 7876 | Adams | Clerk | 7788 | 1983-01-12 | 1100 | NULL | 20 |
| 7900 | James | Clerk | 7698 | 1981-12-03 | 950 | NULL | 30 |
| 7902 | Ford | Analyst | 7566 | 1981-12-03 | 3000 | NULL | NULL |
| 7934 | Miller | Clerk | 7782 | 1982-01-23 | 1300 | NULL | NULL |
+-------+--------+-----------+------+------------+------+------+--------+
14 rows in set (0.00 sec)
//Display all employee no. and name of all employees.
mysql> select empno,ename from emp;
+-------+--------+
| empno | ename |
+-------+--------+
| 7369 | Smith |
| 7499 | Allen |
| 7521 | Ward |
| 7566 | Jones |
| 7654 | Martin |
| 7698 | Blake |
| 7782 | Clark |
| 7788 | Scott |
| 7839 | King |
| 7844 | Turner |
| 7876 | Adams |
| 7900 | James |
| 7902 | Ford |
| 7934 | Miller |
+-------+--------+
14 rows in set (0.00 sec)
// To list all names, date of joining and salary of all employees.
mysql> select ename,hiredate,sal from emp;
+--------+------------+------+
| ename | hiredate | sal |
+--------+------------+------+
| Smith | 1980-12-17 | 800 |
| Allen | 1981-02-20 | 1600 |
| Ward | 1981-02-22 | 1250 |
| Jones | 1981-04-02 | 2975 |
| Martin | 1981-09-28 | 1250 |
| Blake | 1981-05-01 | 2850 |
| Clark | 1981-06-09 | 2450 |
| Scott | 1983-12-09 | 3000 |
| King | 1981-11-17 | 5000 |
| Turner | 1981-09-08 | 1500 |
| Adams | 1983-01-12 | 1100 |
| James | 1981-12-03 | 950 |
| Ford | 1981-12-03 | 3000 |
| Miller | 1982-01-23 | 1300 |
+--------+------------+------+
14 rows in set (0.00 sec)
//To list all the employee names and the salary incremented by 300.
mysql> select ename,sal+300 from emp;
+--------+---------+
| ename | sal+300 |
+--------+---------+
| Smith | 1100 |
| Allen | 1900 |
| Ward | 1550 |
| Jones | 3275 |
| Martin | 1550 |
| Blake | 3150 |
| Clark | 2750 |
| Scott | 3300 |
| King | 5300 |
| Turner | 1800 |
| Adams | 1400 |
| James | 1250 |
| Ford | 3300 |
| Miller | 1600 |
+--------+---------+
14 rows in set (0.00 sec)
// To list the employees with their annual salaries.
mysql> select ename,sal*12 from emp;
+--------+--------+
| ename | sal*12 |
+--------+--------+
| Smith | 9600 |
| Allen | 19200 |
| Ward | 15000 |
| Jones | 35700 |
| Martin | 15000 |
| Blake | 34200 |
| Clark | 29400 |
| Scott | 36000 |
| King | 60000 |
| Turner | 18000 |
| Adams | 13200 |
| James | 11400 |
| Ford | 36000 |
| Miller | 15600 |
+--------+--------+
14 rows in set (0.00 sec)
//Display name and salary where commission is null.
mysql> select ename,sal from emp where comm is NULL;
+--------+------+
| ename | sal |
+--------+------+
| Smith | 800 |
| Jones | 2975 |
| Blake | 2850 |
| Clark | 2450 |
| Scott | 3000 |
| King | 5000 |
| Adams | 1100 |
| James | 950 |
| Ford | 3000 |
| Miller | 1300 |
+--------+------+
10 rows in set (0.00 sec)
// To list the distinct department number from the table.
mysql> select distinct(deptno) from emp;
+--------+
| deptno |
+--------+
| NULL |
| 30 |
| 20 |
| 10 |
+--------+
4 rows in set (0.00 sec)
// To list the unique job from the table.
mysql> select distinct(job) from emp;
+-----------+
| job |
+-----------+
| Clerk |
| Salesman |
| Manager |
| Analyst |
| President |
+-----------+
5 rows in set (0.00 sec)
//To list the salary where salary is less than commission.
mysql> select sal from emp where sal<comm;
+------+
| sal |
+------+
| 1250 |
+------+
1 row in set (0.00 sec)
// To list the names starting with ‘S’.
mysql> select ename from emp where ename like 'S%';
+-------+
| ename |
+-------+
| Smith |
| Scott |
+-------+
2 rows in set (0.00 sec)
//To display employee name and salary of those employees who don’t have their salaries in the
range 25000 to 40000.
mysql> select ename,sal from emp where sal<25000 OR sal>40000;
+--------+------+
| ename | sal |
+--------+------+
| Smith | 800 |
| Allen | 1600 |
| Ward | 1250 |
| Jones | 2975 |
| Martin | 1250 |
| Blake | 2850 |
| Clark | 2450 |
| Scott | 3000 |
| King | 5000 |
| Turner | 1500 |
| Adams | 1100 |
| James | 950 |
| Ford | 3000 |
| Miller | 1300 |
+--------+------+
14 rows in set (0.00 sec)
// To list the name and salary of employee whose salary is greater than or equal to 2200.
mysql> select ename,sal from emp where sal>=2200;
+-------+------+
| ename | sal |
+-------+------+
| Jones | 2975 |
| Blake | 2850 |
| Clark | 2450 |
| Scott | 3000 |
| King | 5000 |
| Ford | 3000 |
+-------+------+
6 rows in set (0.00 sec)
// To display names of employees with names having an ‘L’.
mysql> select ename from emp where ename like '%L%';
+--------+
| ename |
+--------+
| Allen |
| Blake |
| Clark |
| Miller |
+--------+
4 rows in set (0.00 sec)
//To display the name and deptno of all emloyeesfrom dept no 20 or 30 in alphabetical order
mysql> select ename,deptno from emp where deptno=20 OR deptno=30 order by ename;
+--------+--------+
| ename | deptno |
+--------+--------+
| Adams | 20 |
| Allen | 30 |
| Blake | 30 |
| James | 30 |
| Jones | 20 |
| Martin | 30 |
| Scott | 20 |
| Turner | 30 |
| Ward | 30 |
+--------+--------+
9 rows in set (0.00 sec)
//To list the name & salary of all employees who earn more than 1200 and are in deptno10 or 40.
mysql> select ename,sal from emp where sal>1200 AND deptno=10 OR deptno=40;
+-------+------+
| ename | sal |
+-------+------+
| Clark | 2450 |
| King | 5000 |
+-------+------+
2 rows in set (0.00 sec)
//To list name and hiredate of all the employees who were hired in 1981.
mysql> select ename,hiredate from emp where hiredate like '1981%’;
+--------+------------+
| ename | hiredate |
+--------+------------+
| Allen | 1981-02-20 |
| Ward | 1981-02-22 |
| Jones | 1981-04-02 |
| Martin | 1981-09-28 |
| Blake | 1981-05-01 |
| Clark | 1981-06-09 |
| King | 1981-11-17 |
| Turner | 1981-09-08 |
| James | 1981-12-03 |
| Ford | 1981-12-03 |
+--------+------------+
10 rows in set (0.00 sec)
//To list name and salary of all employees who earn a commission.
mysql> select ename,sal from emp where comm is not NULL;
+--------+------+
| ename | sal |
+--------+------+
| Allen | 1600 |
| Ward | 1250 |
| Martin | 1250 |
| Turner | 1500 |
+--------+------+
4 rows in set (0.00 sec)
// To list the names of all employees with ‘a’ as the second letter in their names.
mysql> select ename from emp where ename like '_a%';
+--------+
| ename |
+--------+
| Ward |
| Martin |
| James |
+--------+
3 rows in set (0.00 sec)
// To list the name and job of all employees who work in dept no 20 and their manager is 7788.
mysql> select ename,job from emp where deptno=20 AND mgr=7788;
+-------+-------+
| ename | job |
+-------+-------+
| Adams | Clerk |
+-------+-------+
1 row in set (0.00 sec)
//Add a new record to the table e
mysql> insert into e
-> values(123,'Bill','Turner','123456789',12230);
Query OK, 1 row affected (0.03 sec)
// Add a new column Ph_no of decimal(7,2) type to the table e.
mysql> alter table e
-> add(Ph_no decimal(7,2));
Query OK, 1 row affected (0.19 sec)
Records: 1 Duplicates: 0 Warnings: 0
// To display all records from the table e.
mysql> select *from e;
+--------+------------+-----------+-----------+--------+-------+------+
| emp_id | First_name | Last_name | User_Id | Salary | Ph_no | job |
+--------+------------+-----------+-----------+--------+-------+------+
| 123 | Bill | Turner | 123456789 | 12230 | NULL | NULL |
+--------+------------+-----------+-----------+--------+-------+------+
1 row in set (0.00 sec)
// To delete the column Ph_no from the table e.
mysql> alter table e
-> drop Ph_no;
Query OK, 2 rows affected (0.13 sec)
Records: 2 Duplicates: 0 Warnings: 0
//To delete all rows from the table e.
mysql> delete from e;
Query OK, 1 row affected (0.13 sec)
// To delete the table e.
mysql> drop table e;
Query OK, 0 rows affected (0.03 sec)
MySQL-2
MySQL-3
mysql> create database a;
Query OK, 1 row affected (0.13 sec)
mysql> USE A;
Database changed
mysql> CREATE TABLE DVD(DCODE CHAR (4) PRIMARY KEY, DTITLE VARCHAR(20), DTYPE VA
RCHAR(10));
Query OK, 0 rows affected (0.16 sec)
mysql> INSERT INTO DVD VALUES('F101','HENRY MARTIN','FOLK');
Query OK, 1 row affected (0.33 sec)
mysql> INSERT INTO DVD VALUES('C102','DHRUPAD','CLASSICAL');
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO DVD VALUES('C101','THE PLANETS','CLASSICAL');
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO DVD VALUES('F102','UNIVERSAL SOLDIERS','FOLK');
Query OK, 1 row affected (0.34 sec)
mysql> INSERT INTO DVD VALUES('R102','A DAY IN LIFE','ROCK');
Query OK, 1 row affected (0.33 sec)
mysql> CREATE TABLE MEMBER(MID INT PRIMARY KEY,NAME VARCHAR(20), DCODE CHAR(4),I
SSUE_DATE DATE,FOREIGN KEY (DCODE) REFERENCES DVD(DCODE));
Query OK, 0 rows affected (0.14 sec)
mysql> INSERT INTO MEMBER VALUES(101,'AGAM SINGH','R102','2017-11-30');
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO MEMBER VALUES(103,'ARTH JOSEPH','F102','2016-12-13');
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO MEMBER VALUES(102,'NISHA HANS','C101','2017-07-24');
Query OK, 1 row affected (0.33 sec)
mysql> SELECT * FROM DVD;
+-------+--------------------+-----------+
| DCODE | DTITLE | DTYPE |
+-------+--------------------+-----------+
| C101 | THE PLANETS | CLASSICAL |
| C102 | DHRUPAD | CLASSICAL |
| F101 | HENRY MARTIN | FOLK |
| F102 | UNIVERSAL SOLDIERS | FOLK |
| R102 | A DAY IN LIFE | ROCK |
+-------+--------------------+-----------+
5 rows in set (0.11 sec)
mysql> SELECT * FROM MEMBER;
+-----+-------------+-------+------------+
| MID | NAME | DCODE | ISSUE_DATE |
+-----+-------------+-------+------------+
| 101 | AGAM SINGH | R102 | 2017-11-30 |
| 102 | NISHA HANS | C101 | 2017-07-24 |
| 103 | ARTH JOSEPH | F102 | 2016-12-13 |
+-----+-------------+-------+------------+
mysql> SELECT NAME,ISSUE_DATE FROM MEMBER ORDER BY ISSUE_DATE;
+-------------+------------+
| NAME | ISSUE_DATE |
+-------------+------------+
| ARTH JOSEPH | 2016-12-13 |
| NISHA HANS | 2017-07-24 |
| AGAM SINGH | 2017-11-30 |
+-------------+------------+
3 rows in set (0.30 sec)
mysql> SELECT DTITLE,DTYPE FROM DVD WHERE DTYPE='FOLK';
+--------------------+-------+
| DTITLE | DTYPE |
+--------------------+-------+
| HENRY MARTIN | FOLK |
| UNIVERSAL SOLDIERS | FOLK |
+--------------------+-------+
2 rows in set (0.09 sec)
mysql> SELECT * FROM DVD ORDER BY DTITLE;
+-------+--------------------+-----------+
| DCODE | DTITLE | DTYPE |
+-------+--------------------+-----------+
| R102 | A DAY IN LIFE | ROCK |
| C102 | DHRUPAD | CLASSICAL |
| F101 | HENRY MARTIN | FOLK |
| C101 | THE PLANETS | CLASSICAL |
| F102 | UNIVERSAL SOLDIERS | FOLK |
+-------+--------------------+-----------+
5 rows in set (0.00 sec)
mysql> SELECT DTITLE FROM DVD WHERE DTYPE NOT IN('CLASSICAL');
+--------------------+
| DTITLE |
+--------------------+
| HENRY MARTIN |
| UNIVERSAL SOLDIERS |
| A DAY IN LIFE |
+--------------------+
3 rows in set (0.00 sec)
mysql> SELECT DTYPE,COUNT(DTYPE) FROM DVD GROUP BY DTYPE;
+-----------+--------------+
| DTYPE | COUNT(DTYPE) |
+-----------+--------------+
| CLASSICAL | 2 |
| FOLK | 2 |
| ROCK | 1 |
+-----------+--------------+
3 rows in set (0.30 sec)
mysql> SELECT NAME,ISSUE_DATE FROM MEMBER WHERE ISSUE_DATE LIKE '2017%';
+------------+------------+
| NAME | ISSUE_DATE |
+------------+------------+
| AGAM SINGH | 2017-11-30 |
| NISHA HANS | 2017-07-24 |
+------------+------------+
2 rows in set, 1 warning (0.00 sec)
mysql> SELECT DISTINCT DTYPE FROM DVD;
+-----------+
| DTYPE |
+-----------+
| CLASSICAL |
| FOLK |
| ROCK |
+-----------+
3 rows in set (0.00 sec)
mysql> SELECT MIN(ISSUE_DATE),MAX(ISSUE_DATE) FROM MEMBER;
+-----------------+-----------------+
| MIN(ISSUE_DATE) | MAX(ISSUE_DATE) |
+-----------------+-----------------+
| 2016-12-13 | 2017-11-30 |
+-----------------+-----------------+
1 row in set (0.31 sec)
mysql> ALTER TABLE MEMBER MODIFY NAME VARCHAR(25);
Query OK, 3 rows affected (0.50 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM MEMBER WHERE NAME LIKE 'A%';
+-----+-------------+-------+------------+
| MID | NAME | DCODE | ISSUE_DATE |
+-----+-------------+-------+------------+
| 101 | AGAM SINGH | R102 | 2017-11-30 |
| 103 | ARTH JOSEPH | F102 | 2016-12-13 |
+-----+-------------+-------+------------+
2 rows in set (0.00 sec)
mysql> SELECT D.DCODE,NAME,DTITLE FROM DVD D, MEMBER M WHERE D.DCODE=M.DCODE;
+-------+-------------+--------------------+
| DCODE | NAME | DTITLE |
+-------+-------------+--------------------+
| C101 | NISHA HANS | THE PLANETS |
| F102 | ARTH JOSEPH | UNIVERSAL SOLDIERS |
| R102 | AGAM SINGH | A DAY IN LIFE |
+-------+-------------+--------------------+
3 rows in set (0.00 sec)
mysql> UPDATE DVD SET DTITLE='A UNIVERSAL SOLDIER' WHERE DCODE='F102';
Query OK, 1 row affected (0.34 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> ALTER TABLE DVD ADD PRICE INT;
Query OK, 5 rows affected (0.28 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> UPDATE DVD SET PRICE=150;
Query OK, 5 rows affected (0.08 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> ALTER TABLE DVD DROP PRICE;
Query OK, 5 rows affected (0.52 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> DELETE FROM MEMBER WHERE MID=102;
Query OK, 1 row affected (0.37 sec)