Computer Science Practical File Printed
Computer Science Practical File Printed
VIDYALAYA
COMPUTER SCIENCE
PRACTICAL NOTEBOOK
AISSCE 2018-19
2018-2019
ACKNOWLEDGEMENT
Signature of Candidate
CERTIFICATE
___________________
Signature of teacher
(MR. PRAMOD KUMAR)
INDEX
s.no Program or query
1. Function overloading
2. Class and object
3. Constructor and destructor
4. Multiple inheritance
5. Multilevel inheritance
6. Data handling(average word size)
7. Data handling(count number of spaces)
8. Data handling(frequency of each character in file)
9. Swapping using pointer
10. pointer
11. Linear search
12. Binary search
13. Bubble sorting
14. Selection sorting
15. Insertion sorting
16. Addition and multiplication of two matrix
17. Middle row and middle column (2-D array)
18. Binary file using structure
19. Menu based stack
20. Menu based queue
21. Structured query language
1 . Function Overloading
#include<iostream.h>
#include<conio.h>
void amount(float princ,int time,float rate)
#include<conio.h>
#include<process.h>
class item
{int itemcode[50];float it_price[50];
public:
void initialize(void);
float largest(void);
float sum(void);
void display(void);
};
void item::initialize(void)
{for(int i=0;i<5;i++)
{cout<<"\nItem No.: "<<i+1; cout<<"\nEnter item code : ";
cin>>itemcode[i];
cout<<"\nEnter item Price :";
cin>>it_price[i];
cout<<"\n";
}
}
float item::largest(void)
{
float large=it_price[0];
for(int i=1;i<5;i++)
{
if(large<it_price[i])
large=it_price[i];
}
return large;
}
float item::sum(void)
{float sum=0;
for(int i=0;i<5;i++)
sum+=it_price[i];
return sum;
}
void item::display(void)
{cout<<"\nCode Price\n";
for(int i=0;i<5;i++)
{cout<<"\n"<<itemcode[i];
cout<<" "<<it_price[i];
}
cout<<"\n";
}
void main()
{
item order;
order.initialize();
float total,biggest;
int ch=0;
clrscr();
do
{
cout<<"\nMain Menu.\n";
cout<<"\n1.Display Largest Price.";
cout<<"\n2.Display Sum of Prices.";
cout<<"\n3.Display Item List.";
cout<<"\n4.Exit";
cout<<"\nEnter Your Choice(1-3)";
cin>>ch;
switch(ch)
{
case 1:biggest = order.largest();
cout<<"The Largest Price is "<<biggest<<"\n";
break;
case 2:total=order.sum();
cout<<"The Sum of Price is "<<total<<"\n";
break;
case 3:order.display();
break;
case 4:exit(0);
break;
default:cout<<"\nWrong Choice!!\n";
break;
}
}while(ch!=4);
getch();
}
3 . Constructor and Destructor
#include<iostream.h>
#include<conio.h>
class stu
{ private: char name[20],add[20];
int roll,zip;
public: stu ( );//Constructor
~stu( );//Destructor
void read( ); void disp( ); };
stu :: stu( )
{ cout<<"This is Student Details "<<endl;
}
cout<<"Address is : "<<add<<endl;
cout<<"Zipcode is : "<<zip; }
stu :: ~stu( )
{ cout<<"Student Detail is Closed "; }
void main( )
{ stu s;
clrscr( );
s.read ( );
s.disp ( );
getch( );
}
4 . Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class base1
{ protected: int a;
public: base1(int x)
{a=x; cout<<"Constructing
Base 1\n";} ~base1()
{cout<<"Destructing Base
1\n";} }; class base2
{ protected: int b;
public:
base2(int y)
{b=y; cout<<"Constructing Base 2\n";}
~base2()
{cout<<"Destructing Base 2\n";} };
void main()
{ clrscr();
derived ob(14,15,16);
ob.show();
getch();
}
5 . Multilevel Inheritance
#include <iostream.h>
#include<conio.h>
class base
{ public: int x;
void getdata()
public:
void indata()
{ cout << "\nEnter value of z= "; cin
>> z; } void product()
{ cout << "\nProduct= " << x * y * z; } };
void main()
{clrscr();
derive2 a;
a.getdata();
a.readdata();
a.indata();
a.product();
getch();
}
6 . Data file Handeling (calculate average
word size )
#include<fstream.h>
#include<conio.h>
void calculate()
{
fstream tfile;
clrscr();
tfile.open("Report.txt",ios::in);
char arr[20];
char ch;
int i=0,sum=0,n=0;
while(tfile)
{
tfile.get(ch);
arr[i] = ch;
i++;
if (( ch == ' ') || (ch == '.'))
{
i--;
sum = sum + i;
i = 0;
n++;
}
}
cout<<" Average word size is "<<(sum/n);
}
void main()
{
calculate();
getch();}
7 . Data File Handeling (count number of
spaces )
#include<fstream.h>
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
void display()
{
ifstream afile;
afile.open("NOTES.TXT");
char ch;
int c = 0;
while(afile)
{
afile.get(ch);
if (ch == ' ' )
c++;
}
cout<<"The number of blank spaces : "<<c;
}
void main()
{
clrscr();
display();
getch();
}
8 . Data File Handeling ( find the frequency
of characters in file)
# include<fstream.h>
# include<stdio.h>
# include<conio.h>
void main()
{clrscr();
afile.close();
for (i=0;i<26;i++)
{ cout<<"Frequency of "<<char(i+'A')<<" is "<<fre[i]<<"\t"; }
getch();
}
9 . Pointers (swapping using pointers in
function )
#include<iostream.h>
#include<conio.h>
void swap(int &, int &);
void main()
{
clrscr();
int a, b;
getch();
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
10 . Pointers
#include<iostream.h>
#include<conio.h>
char*match(char,char*);
void main()
{ clrscr();
char string[80],*cp,ch;
cout<<"\nEnter a string\t";
cin.getline(string,80);
cout<<"\nEnter a character to be searched for\t";
cin>>ch;
cp=NULL;
cp=match(ch,string);
if(*cp)
{cout<<"\n";
for(;(*cp!='\0');cp++)
cout<<*cp;
}
else
cout<<"\nNO MATCH FOUND\n";
cout<<"\n";
getch();
}
char*match(char c,char *s)
{while((c!=*s)&&(*s))
s++;
return(s);
}
11 . Linear search
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
int MAX_SIZE;
cout<<"\nEnter the size\t ";
cin>>MAX_SIZE;
int arr_search[60], i, element;
cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_search[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
linear_search(arr_search, element,MAX_SIZE);
getch();
}
void linear_search(int fn_arr[], int element,int
MAX_SIZE) { int i;
for (i = 0; i < MAX_SIZE; i++) {
if (fn_arr[i] == element) {
cout << "\nLinear Search : Element : " << element << " : Found : Position : " <<
i + 1 << ".\n"; break;}}
if(i == MAX_SIZE)
cout << "\nSearch Element : " << element << " : Not Found \n";
}
12 . Binary Search
#include<iostream.h>
#include<conio.h>
int Bsearch(int[],int,int);
void main()
{clrscr();
int AR[50],item,n,index,i;
cout<<"enter desired array size";
cin>>n;
cout<<"enter elements ";
for(i=0;i<n;i++)
cin>>AR[i];
cout<<"enter the elents to be searched for";
cin>>item;
index=Bsearch(AR,n,item);
if (index==-1)
{ cout<<"sorry element not found"; }
else
{ cout<<"element found at "<<index<<" index and "<<index+1<<"
position"; } 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; }
return -1;}
13 . Bubble Sorting
#include<iostream.h>
#include<conio.h>
void bs(int[],int);
void main()
{clrscr();
int ar[100],it,n,in,i;
cout<<endl;
getch();}
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
if(ar[j]>ar[j+1])
{ ar[j]=ar[j]+ar[j+1]-(ar[j+1]=ar[j]);
} } }
14 . Selection Sorting
#include<iostream.h>
#include<conio.h>
void ss(int[],int);
void main()
{clrscr();
int ar[100],it,n,in,i;
cout<<endl;
getch();}
for(i=0;i<n-1;i++)
{sm=ar[i];po=i;
{ for(j=i+1;j<n;j++)
if(ar[j]<sm)
{sm=ar[j]; po=j;}
{ ar[i]=ar[i]+ar[po]-(ar[po]=ar[i]); }
}
}}
15. Insertion Sorting
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void is(int[],int);
void main()
{clrscr();
int ar[100],n,i;
cout<<"\n\t\n\t PROGRAM FOR INSERTION
SORTING\n"; cout<<"\n how many elements you
want in array\n"; cin>>n;
cout<<"\nEnter the elements\n";
for(i=1;i<=n;i++)
cin>>ar[i];
is(ar,n);
cout<<"\n\nsorted data is\n";
for(i=1;i<=n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();}
void is(int ar[],int n)
{ int i,j,tem;
ar[0]=INT_MIN;
for(i=1;i<=n;i++)
{ tem=ar[i];
j=i-1;
while(tem<ar[j])
{ ar[j+1]=ar[j];
j--; }
ar[j+1]=tem;
} }
16. 2-D Array (Sum of 2 matrix and
Multiplication of 2 matrices)
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
void main()
{clrscr();
char cv;
do{clrscr();
int ch;
int a[20][20],b[20][20],c[20][20],i,j,m,n,p,q,k;
cout<<"\t\n Main Menu\n";
cout<<"\n1.Sum of 2 matrx\n";
cout<<"\n2.product of 2 matrix\n";
cout<<"\n3.EXIT\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Input rows & columns of matrix 1\n";
cin>>m>>n;
cout<<"\n Input rows & columns of matrix 2\n";
cin>>p>>q;
if((m==p)&&(n==q))
cout<<"\n Matrix can be added\n";
else{
cout<<"\n Matrix can't be added\n";
sleep(20);
exit(0);
}
cout<<"\n Input matrix -1\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n Input matrix -2\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<<"\n The sum of matrix is\n";
for(i=0;i<m;i++)
{cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
break;
case 2:
cout<<"\n Input rows & columns of matrix 1\n";
cin>>m>>n;
cout<<"\n Input rows & columns of matrix 2\n";
cin>>p>>q;
if(n==p)
{
cout<<"\n Input matrix -1\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n Input matrix -2\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
cout<<"\n Product of two matrices:\n";
for(i=0;i<m;++i)
{cout<<"\n";
for(j=0;j<q;++j)
{c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
cout<<c[i][j]<<" ";
}}}
else{
cout<<"\n Matrices are not compatible for multiplication\n";
}
break;
case 3:exit(0);
break;
} while(cv=='y'||cv=='Y');
getch();}
17. MIDDLE ROW AND COLUMN OF 2-D ARRAY
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
const M = 10;
const N = 10;
void display_RowCol(int Array[M][N], int r, int c)
{
clrscr();
int row = r / 2;
int col = c / 2; // Finding the middle row
cout << "Middle Row : ";
for(int j=0; j<c; j++)
cout << Array[row][j] << " ";cout << endl;
// Finding the middle column
cout << "Middle Column : ";
for(j=0; j<c; j++)
cout << Array[j][col] << " ";
getch();
}
void main()
{clrscr();
int Array[M][N];
int i, j;
int r, c;
cout << "Enter total no. of rows: ";
cin >> r;
cout << "Enter total no. of columns: ";
cin >> c;
#include<stdio.h>
#include<process.h>
#include<conio.h>
void create();
void display();
void roll_search();
struct stud
{int rno;
char name[50],adr[50];
long tel;
}s;
void create()
{ ofstream
do{
cout<<"\n Enter the Roll No. : ";cin>>s.rno;
cout<<"\n Enter Name : ";gets(s.name);
cout<<"\n Enter Address : ";gets(s.adr);
cout<<"\n Enter Telephone : ";cin>>s.tel;
fout.write((char*)&s,sizeof(s));
fout.close(); }
void display()
{ ifstream fin("stud.dat",ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
cout<<"\n Roll No. : " <<s.rno<<endl;
cout<<"\n Name : "<<s.name;
cout<<"\n Address : "<<s.adr;
cout<<"\n Telephone : "<<s.tel<<endl;
} fin.close(); }
void roll_search()
{ ifstream
fin("stud.dat",ios::binary); int x;
cout<<"\n1.Create\n2.Display\n3.Search Roll
cin>>x;
switch(x)
{
case 1:create();
break;
case 2:display();
getch();
break;
case 3:roll_search();
getch();
break;
case 4:exit(0);
break;
default:cout<<"\n WRONG CHOICE !!!!\n";break;
}
getch();
} while(ch!=4);}
18. Menu Based Stack
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#define max_size 50
int stack[max_size],top=-1;
void push();
void pop();
void peep();
void display();
void main()
{clrscr();
int choice;
do{
cout<<"\n";
cout<<"\n\n--------STACK OPERATIONS-----------\n";
cout<<"\t1.Push\n";
cout<<"\t2.Pop\n";
cout<<"\t3.Peep\n";
cout<<"\t4.Display\n";
cout<<"\t5.Exit\n";
cout<<"---------------------------------------";
cout<<"\nEnter your choice:\t";
cin>>choice;
switch(choice)
{case 1:push();break;
case 2:pop();break;
case 3:peep();break;
case 4:display();break;
case 5:exit(0);break;
default:cout<<"\nInvalid choice:\n";
break;
}
}while(choice!=5);
getch();
}
void push()
{
int item;
if(top==(max_size-1))
{
cout<<"\nStack Overflow:";
}
else
{cout<<"Enter the element to be inserted:\t";
cin>>item;
top=top+1;
stack[top]=item;
}
}
void pop()
{
int item;
if(top==-1)
{
cout<<"Stack Underflow:";
}
else
{
item=stack[top];
top=top-1;
cout<<"\nThe poped element:\t"<<item;
}
}
void peep()
{
if(top==-1)
{
cout<<"\nStack is empty:";
}
else
{
cout<<"The topmost element of the stack is "<<stack[top];
}
}
void display()
{
int i;
if(top==-1)
{
cout<<"\nStack is Empty:";
}
else
{
cout<<"\nThe stack elements are:\n";
for(i=top;i>=0;i--)
{
cout<<"\n"<<stack[i];
}
}
}
20. Menue Based Queue
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define max_size 50
int queue[max_size],front=-1,rear=-1;
void insert();
void del();
void display();
int main()
{clrscr();
int choice;
do{
cout<<"\n\n--------QUEUE OPERATIONS-----------\n";
cout<<"1.Insert\n2.Delete\n3.Display\n4.Exit\n";
choice:\t";
cin>>choice;
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:cout<<"\nInvalid choice:\n";
break;
}
}while(choice!=4);
return 0;
}
void insert()
{
int item;
if(rear==(max_size-1))
{
cout<<"\nQueue Overflow:";
}
else
{
cout<<"Enter the element to be inserted:\t";
cin>>item;
rear=rear+1;
queue[rear]=item;
if(front==-1)
front=0; } }
void del()
{
int item;
if(front==-1)
{cout<<"\nQueue Underflow:";}
else
{item=queue[front];
cout<<"\nThe deleted element: \t"<<item;
if(front==rear){
front=-1;
rear=-1;
}
else
{
front=front+1; } }
}
void display()
{ int i; if(front==-1)
{cout<<"\nQueue is
Empty:";}
else
{ cout<<"\nThe queue elements
are:\n" ; for(i=front;i<=rear;i++)
{ cout<<"\t"<<queue[i]; }
}
}
STRUCTURED QUERY LANGUAGE
Consider the following tables Product and Client. Write SQL
commands for the statement (i) to (iv) and give outputs for SQL
queries (v) to (viii)
1. mysql> select*from mmaster where MName like 's%' or MName like '%a';
8. mysql> select MId,M_Name from mmaster where M_Price between 3000 and 5000;
Product
P_ID Manufacturer Price
Name
TP01 TalcomPowder LAK 40
FW05 Face Wash ABC 45
BS01 Bath Soap ABC 55
SH06 Shampoo XYZ 120
FW12 Face Wash XYZ 95
Table: CLIENT
(ii) To display the details of Products whose Price is in the range of 50 to 100(Both values
included).
Ans: Select all from product where Price between 50 and 100
(iii) To display the ClientName, City from table Client, and ProductName and Price from
table Product, with their corresponding matching P_ID.
Ans: ( The above question may consist DISTINCT City. If it is DISTINCT City, the following
is the answer)
City
-----
Delhi
Mumbai
Bangalore
Ans:
Ans:
ClientName ManufacturerName
CosmeticShop ABC
TotalHealth ABC
LiveLife XYZ
PrettyWoman XYZ
Dreams LAK
ProductName Price*4
TalcomPoweder 160
FaceWash 180
BathSoap 220
Shampoo 480
Face Wash 380
Consider the following tables Item and Customer. Write SQL commands for
the statement (i) to (iv) and give outputs for SQL queries (v) to (viii)
Table: ITEM
Table: CUSTOMER
(ii) To display the details of Item whose Price is in the range of 35000 to 55000 (Both
values included).
Ans: Select all from Item Where Price>=35000 and Price <=55000
(iii) To display the CustomerName, City from table Customer, and ItemName and Price
from table Item, with their corresponding matching I_ID.
(iv) To increase the Price of all Items by 1000 in the table Item.
Ans: City
Delhi
Mumbai
Bangalore
Ans:
Ans:
CustomerName ManufacturerName
N.Roy PQR
H.Singh XYZ
R.Pandey COMP
C.Sharma PQR
K.Agarwal
ABC
(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer = ‘ABC’;
Ans:
ItemName Price*100
PersonalComputer 3500000
Laptop
550000
Consider the following tables Consignor and Consignee. Write SQL command for the
statements(i)to(iv) And give outputs for the SQL quries (v) to ( viii).
TABLE : CONSIGNOR
TABLE : CONSIGNEE
(ii) To display the cneeID, cnorName, cnorAddress, CneeName, CneeAddress for every
Consignee.
Ans:
CneeCity
Mumbai
New Delhi
Kolkata
Ans:
senderCity
New Delhi
Mumbai
Ans: CneeID
CneeName
MU05 Rahul
Kishore
KO19
A P Roy
Consider the following tables. Write SQL command for the
statements (i)to(iv)and give outputs for the SQL quries (v) to (viii).
TABLE : SENDER
Sender Sender
SenderID SenderName
Address City
2,ABC New
ND01 R jain
Appts Delhi
12,
MU02 H sinha Mumbai
Newton
27/
New
MU1 5 S haj A,Park
Delhi
Street
122-
ND5 0 T Prasad Mumbai
K,SDA
TABLE :RECIPIENT
CnorName
R singhal
Amit Kumar
CneeName
Rahul Kishore
S mittal
CneeName CneeAddress
16/j,Moore
P Dhingra
Enclave
13,Block
B P jain
d,a,viha
Ans:-------