KENDRIYA VIDYALAYA
LAB MANUAL
COMPUTER SCIENCE
Index
S . No Content Page no
1 string is Palindrome or not 3
2 Matrices Multiplication 4
3 Class implementation 5
4 calculate employee salary using class implementation 7
5 Function Overloading to calculate A ^B. 13
6 Count alphabet in text file 15
7 Count blank space in text file 16
8 Append data in Binary file 17
9 Read from Binary File 18
10 Linear search 20
11 Binary Search 21
12 Bubble Sort 23
13 Selection Sort 26
14 Insertion in Beginning of Linked List 29
15 Deletion in Linked List 30
16 Static Implementation of Stack 32
17 Dynamic Implementation of Stack 34
18 Static Implementation of Queue 37
19 Dynamic Implementation of Queue 39
20 SQL query 40
21 SQL query 40
22 SQL query 40
23 SQL query 40
24 SQL query 40
25 K map 41
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char string[80], c;
cout<<"\n Enter a string(max.79 character):";
cin.getline(string,80);
for(int len=0;string[len]!='\0';++len);
int i,j,flag=1;
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(string[i]!=string[j])
{
flag=0;
break;
}
}
if(flag)
cout<<"It Is Palindrome\n";
else
cout<<"It Is Not A Palindrome\n";
getch();
return 0;
}
______________________
Program 2 . C++ program of Matrices Multiplication
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{clrscr();
int a[10][10],b[10][10],c[10][10],m,n,i,j,p,q;
cout<<"\nEnter the row and column of matrix-A:";
cin>>m>>n;
cout<<"\nEnter the row and column of matrix-B:";
cin>>p>>q;
if(n==p)
cout<<"Matrices can be multiplied\n";
else
{ cout<<"Matrices cannot be multiplied\n";
getch();
exit(0);
}
cout<<"\nInput matrix-A:\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\nInput matrix-B:\n";
for(i=0;i<p;i++)
{ for(j=0;j<q;j++)
cin>>b[i][j];
}
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{ c[i][j]=0;
for(int ip=0;ip<n;++ip)
c[i][j]+=(a[i][ip]*b[ip][j]);
}
cout<<"\nThe product of matrices is:\n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<q;j++)
cout<<" "<<c[i][j];
}
getch();
}
{
cout<<"x="<<x<<"\n"<<"y="<<y<<"\n";
}
};
void main()
{
clrscr();
num obj1,obj2;
obj1.init();
obj2.init();
obj1.read(15,11);
obj1.display();
obj2.display();
getch();
}
_______________________________
void display()
{ total=pay*12;
cout<<"\nEntered date is:\nname="<<name<<"\nmonthly payement="<<pay<<"\nannual
income="<<total;
}
};
void main()
{ clrscr();
emp t1;
t1.getdata();
t1.display();
getch();
}
__________________________________________
Program 5 C++ Program to add two matrices
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{clrscr();
int a[10][10],b[10][10],c[10][10],m,n,i,j,p,q;
cout<<"\nEnter the row and column of matrix-A:";
cin>>m>>n;
cout<<"\nEnter the row and column of matrix-B:";
cin>>p>>q;
if((m==p)&&(n==q))
cout<<"Matrices can be added\n";
else
{ cout<<"Matrices cannot be added\n";
getch();
exit(0);
}
cout<<"\nInput matrix-A:\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\nInput matrix-B:\n";
for(i=0;i<p;i++)
{ for(j=0;j<q;j++)
cin>>b[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"\nThe sum of matrices is:\n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
getch();
}
___________________________________________________________
Program 6. C++ program to Multiple Inheritance
/* Program to define the classes PERSON, GAME and STUDENT & to access the essential data using
multiple inheritance.*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
};
void person::outdata() // since the function contains loop so it is not made inline
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
class game {
char game_name[20];
public:
void input()
{
cout<<"\n\nEnter the game name : ";
cin.get();cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.enter();
A.display();
getch();
}
_______________________________________________________
Program 7. C++ program using Function Overloading to calculate A ^B.
/* WAP USING FUNCTION OVERLOADING TO CALCULATE A^B */
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{clrscr();
int P,Q;
float R,S;
cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n";
cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";
cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";
cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch();
}
____________________________________
Program 8. C++ Program to count total no of alphabet in text file .
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
void display()
{
ifstream fin;
fin.open("STORY.TXT");
char ch;
int c=0;
while(fin)
{
fin.get(ch);
if(isalpha(ch))
c++;
}
cout<<"The number of alphabets are:"<<c;
fin.close();
}
void main()
{ clrscr();
display();
getch();
}
___
Program 9. C++ Program to count total no of blank space in text file .
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
void display()
{
ifstream fin;
fin.open("NOTE.TXT");
char ch;
int c=0;
while(fin)
{
fin.get(ch);
if(ch==' ')
c++;
}
cout<<"The number of blank spaces are:"<<c;
fin.close();
}
void main()
{ clrscr();
display();
getch();
}
_____________
Program 10. To append data in Binary File
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class stu
{ int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{
cout<<"Rollno:"; cin>>rollno;
cout<<"Name:"; cin>>name;
cout<<"Class:"; cin>>Class;
cout<<"Marks:"; cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<name<<",rollno"<<rollno<<"has"<<marks
<<"%marks and"<<grade<<"grade."<<endl;
}
int getrno()
{
return rollno;
}
}s1;
void main()
{
ofstream fo("stu.dat",ios::app);
char ans='y';
while(ans=='y')
{
s1.getdata();
fo.write((char*)&s1,sizeof(s1));
cout<<"Record added to file.\n";
cout<<"Want to enter more record?(y/n):";
cin>>ans;
}
fo.close();
getch();
}
__________
Program 11- Read from Binary File
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
struct customer
{ char name[51];
float balance;
};
void main()
{
clrscr();
customer savac;
strcpy(savac.name,"Tina Marshall");
savac.balance=21310.75;
ofstream fout;
fout.open("Saving",ios::out|ios::binary);
if(!fout)
{
cout<<"file cannot be opened.\n";
}
fout.write((char*)&savac,sizeof(customer));
fout.close();
ifstream fin;
fin.open("Saving",ios::in|ios::binary);
fin.read((char*)&savac,sizeof(customer));
cout<<savac.name;
cout<<"has the balance amount of Rs."<<savac.balance<<"\n";
fin.close();
getch();
}
__________________
}
cout<<"\nEnter element to be search for:";
cin>>item;
index=Lsearch(AR,n,item);
if(index==-1)
cout<<"\nSorry!! given element not found.\n";
else
cout<<"\nElement found at index:"<<index<<",Position:"<<index+1;
getch();
}
int Lsearch(int AR[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(AR[i]==item)
return i;
}
getch();
return -1;
}
_______________________
cin>>item;
index=Bsearch(AR,n,item);
if(index==-1)
cout<<"\nSorry!! given element not found.";
else
cout<<"\nElement found at index:"<<index<<",Positon:"<<index+1;
getch();
}
int Bsearch(int AR[],int size,int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==AR[mid])
return mid;
else
if(item>AR[mid])
beg=mid+1;
else
last=mid-1;
}
getch();
return -1;
}
__________________________________________________
Program 14. Bubble Sort
#include<iostream.h>
#include<conio.h>
void BubbleSort(int [],int);
void main()
{clrscr();
int AR[50],item,n,index;
cout<<"Enter the size of array(max.50):";
cin>>n;
cout<<"\nEnter the elements of array:\n";
for(int i=0;i<n;i++)
cin>>AR[i];
BubbleSort(AR,n);
cout<<"\nThe sorted array is as shown below:\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
getch();
}
void BubbleSort(int AR[],int size)
{
int tmp,ctr=0;
for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1);j++)
{
if(AR[j]>AR[j+1])
{ tmp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=tmp;
}
}
cout<<"Array after iteration-"<<++ctr<<"-is:";
for(int k=0;k<size;k++)
cout<<AR[k]<<" ";
cout<<endl;
}
getch();
}
__________________________________________
Program 15- Program to sort an array using Selection Sort
#include<iostream.h>
#include<conio.h>
void SelSort(int [],int);
void main()
{ clrscr();
int AR[50],item,n,index;
cout<<"Enter the size of array(max.50):";
cin>>n;
cout<<"\nEnter the elements of array:";
for(int i=0;i<n;i++)
cin>>AR[i];
SelSort(AR,n);
cout<<"\nThe sorted array is shown below:";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
getch();
}
void SelSort(int AR[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{ small=AR[i];
for(int j=i+1;j<size;j++)
{ if(AR[j]<small)
{small=AR[j];
pos=j;
}
}
tmp=AR[i];
AR[i]=AR[pos];
AR[pos]=tmp;
cout<<"\nArray after pass-"<<i+1<<"-is:";
for(j=0;j<size;j++)
cout<<AR[j]<<" ";
}
getch();
}
__________________--
Program 16:- Inserting element in beginning of list .
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{
int info;
Node * next;
}*start,*newptr,*save,*ptr;
Node * Create_New_Node(int);
void Insert_Beg(Node*);
void Display(Node*);
void main()
{ start=NULL;
int inf;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter information for the new node....";
cin>>inf;
cout<<"\n Creating New Node!! Press Enter to continue...";
getch();
newptr=Create_New_Node(inf);
if(newptr!=NULL)
{ cout<<"\n\nNew Node Created Successfully. Press Enter to continue....";
getch();
}
else
{
cout<<"\nCannot create new node!!! Aborting!!\n";
getch();
exit(1);
}
cout<<"\n\nNow inserting this node in the beginning of list....\n";
cout<<"Press Enter to continue....\n";
getch();
Insert_Beg(newptr);
cout<<"\nNow the list is:\n";
Display(start);
cout<<"\n Press Y toenter more node, N to exit...";
cin>>ch;
}
}
Node * Create_New_Node(int n)
{
ptr = new Node;
ptr -> info = n;
ptr -> next = NULL;
return ptr;
}
void Insert_Beg(Node* np)
{
if(start==NULL)
start = np;
else
{
save = start;
start = np;
np -> next = save;
}
}
void Display(Node* np)
{
while(np!=NULL)
{
cout<<np -> info <<"->";
np = np -> next;
}
cout<<"!!!!\n";
}
_______________________________
Program 17 - Delete element from Linked List
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{
int info;
Node * next;
}*start,*newptr,*save,*ptr,*rear;
Node * Create_New_Node(int);
void Insert(Node*);
void Display(Node*);
void DelNode();
void main()
{ start=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter information for the new node....";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\nCannot create new node!!! Aborting!!\n";
getch();
exit(1);
}
Insert(newptr);
}
cout<<"!!!!\n";
}
_____________________________________________
Program 18- Static Implementation of Stack using Array
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Push(int [],int&,int);
void Display(int [], int);
const int size=50;
void main()
{
int Stack[size],item,top=-1,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter item for insertion:";
cin>>item;
res=Push(Stack,top,item);
if(res==-1)
{
cout<<"OVERFLOW!!! ABORTING!!\n";
exit(1);
}
cout<<"\n The Stack now is:\n";
Display(Stack,top);
cout<<"\nWant to insert more elements?(y/n):";
cin>>ch;
}
}
int Push(int Stack[],int & top,int ele)
{
if(top==size-1)
return -1;
else
{ top++;
Stack[top]=ele;
}
getch();
return 0;
}
void Display(int Stack[], int top)
{
cout<<Stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
cout<<Stack[i]<<endl;
getch();
}
_____________________________________________
Program 19- Dynamic Implementation Stack Using Linked List
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{
int info;
Node * next;
}*top,*newptr,*save,*ptr;
Node * Create_New_Node(int);
void Push(Node*);
void Display(Node*);
void main()
{
int inf;
char ch='y';
top=NULL;
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter information for the new node....";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\nCannot create new node!!! Aborting!!\n";
exit(1);
}
Push(newptr);
cout<<"\nNow linked-stack is:\n";
Display(top);
cout<<"\n Press Y toenter more node, N to exit...";
cin>>ch;
}
}
Node * Create_New_Node(int n)
{
ptr = new Node;
ptr -> info = n; ptr -> next = NULL;
return ptr;
}
void Push(Node* np)
{
if(top==NULL) top = np;
else
{
save = top; top = np;
np -> next = save;
}
}
void Display(Node* np)
{
while(np!=NULL)
{
cout<<np -> info <<"->";
np = np -> next;
}
cout<<"!!!!\n";
}
_________________________________________________
Program 20- Program to implement queue statically ( array )
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Insert_in_Q(int [],int);
void Display(int [],int,int);
const int size=50;
int Queue[size],front=-1,rear=-1;
void main()
{
int item,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter item for insertion:";
cin>>item;
res=Insert_in_Q(Queue,item);
if(res==-1)
{
cout<<"OVERFLOW!!! ABORTING!!\n";
exit(1);
}
cout<<"\n Now the Queue(Front to Rear) is:\n";
Display(Queue,front,rear);
____________________________________
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{
int info;
Node * next;
}*front,*newptr,*save,*ptr,*rear;
Node * Create_New_Node(int);
void Insert_End(Node*);
void Display(Node*);
void main()
{ front=rear=NULL;
int inf;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter information for the new node....";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\nCannot create new node!!! Aborting!!\n";
exit(1);
}
Insert_End(newptr);
cout<<"\nNow the Queue(front to rear) is:\n";
Display(front);
cout<<"\n Press Y toenter more node, N to exit...";
cin>>ch;
}
}
Node * Create_New_Node(int n)
{
ptr = new Node;
ptr -> info = n; ptr -> next = NULL;
return ptr;
}
void Insert_End(Node* np)
{
if(front==NULL) front = rear = np;
else
{
rear -> next = np;
rear = np;
}
}
void Display(Node* np)
{
while(np!=NULL)
{
cout<<np -> info <<"->";
np = np -> next;
}
cout<<"!!!!\n";
}
________
Q1. Write SQL command for (a) to (f) and write output of SQL command given in (g) with the help of table
shown below:
F= ab+ac+a’d+a’b’c’
------------------------ *****************-----------------------***************---