CS File 12th
CS File 12th
Write a C++ program that uses an area( ) function for the calculation of area of
triangle or a rectangle or a square. Number of sides (3 for triangle, 2 for
rectangle and 1 for square) suggest about the shape for which area is to be
calculated.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void area(float);
void area(float,float);
void area(float,float,float);
void main()
{clrscr();
int a;
float b,c,d,e;
cout<<"\nEnter the desired option:";
cout<<"\n1. Square";
cout<<"\n2. Rectangle";
cout<<"\n3. Triangle\n";
cin>>a;
switch(a)
{
case (1): cout<<"\nYou selected Square!";
cout<<"\nEnter the side:";
cin>>b;
area(b);
break;
case (2): cout<<"\nYou selected Rectangle!";
cout<<"\nEnter the sides:";
cin>>b;
cin>>c;
Function Overloading
area(b,c);
break;
case (3): cout<<"\nYou selected Triangle!";
cout<<"\nEnter the sides:";
cin>>b;
cin>>c;
cin>>d;
area(b,c,d);
break;
default : cout<<"Enter a valid choice:";
break; }
getch();
}
void area (float b)
{
float e;
e = (b*b);
cout<<"\nThe area is:"<<e;
}
void area (float b, float c)
{
float e;
e = (b*c);
cout<<"\nThe area is:"<<e;
}
void area (float b, float c, float d)
{
float s,e;
s=((b+c+d)/2);
e = sqrt(s*(s-b)*(s-c)*(s-d));
cout<<"\nThe area is:"<<e;
}
Output:
Enter the desired option:
1. Square
2. Rectangle
3. Triangle
2
You selected Rectangle!
Enter the sides: 2
3
The area is: 6
Q2. Write a C++ program that uses a function to check whether a given number is
divisible by another number or not. However, if the second number is missing,
the function checks whether the given number is prime or not ?
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void num(int, int);
void num(int);
void main()
{clrscr();
int a,b,c;
cout<<"\nEnter the desired option:";
cout<<"\n1. Check Divisibilty";
cout<<"\n2. Check for Prime\n";
cin>>a;
switch(a)
{case (1): cout<<"You selected to check for divisibilty!";
cout<<"\nEnter the numbers:";
cin>>b;
cin>>c;
num(b,c);
break;
case (2): cout<<"\nYou selected the check for prime!";
cout<<"\nEnter the number:";
cin>>b;
num(b);
break;
default : cout<<"Enter a valid choice:";
break; }
getch();
}
void num(int b, int c)
{
int r;
r = (b%c);
cout<<"The result is:"<<r;
}
void num (int b)
{
int e;
int j=b/2;
for (int i=2; i<=j; i++)
{ if ( b%i== 0 )
e=0;
else (e=1);
}
if (e==0)
cout<<"It is not a prime number:";
else if (e==1)
cout<<"It is a prime number:";
}
Output:
Enter the desired option:
1. Check Divisibility
2. Check for Prime
2
You selected the check for prime!
Enter the number: 5
It is not a prime number
Q3. Write a C++ program using function overloading, the function volume( ) returns
the relevant volume and has three versions:-
1. For cubes volume that takes one float side of the cube.
2. For cylinders volume that takes float radius and float height of the
cylinder.
3. For rectangular boxs volume that takes float length, float breadth and
float height of the box.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void volume(float);
void volume(float,float);
void volume(float,float,float);
void main()
{clrscr();
int a;
float l,b,h,r;
cout<<"\nEnter the desired option:";
cout<<"\n1. Cube's volume";
cout<<"\n2. Cylinder's volume";
cout<<"\n3. Rectangular Box's volume\n";
cin>>a;
switch(a)
{
case (1): cout<<"\nYou selected Cube's Volume!";
cout<<"\nEnter the side:";
cin>>l;
volume(l);
break;
case (2): cout<<"\nYou selected Cylinder's Volume!";
cout<<"\nEnter the radius and height:";
cin>>h;
cin>>r;
volume(h,r);
break;
case (3): cout<<"\nYou selected Rectangular Box's
Volume!";
cout<<"\nEnter the length, breadth and
height:";
cin>>l;
cin>>b;
cin>>h;
volume(l,b,h);
break;
default : cout<<"Enter a valid choice:";
break;
}
getch();
}
void volume(float l)
{
float e;
e = (l*l*l);
cout<<"The result is:"<<e;
}
void volume (float h, float r)
{
float e;
e = (3.14*r*r*h);
cout<<"The result is:"<<e;
}
void volume (float l, float b, float h)
{
float e;
e = (l*b*h);
cout<<"The result is:"<<e;
}
Output:
Enter the desired option:
1. Cube's volume
2. Cylinder's volume
3. Rectangular Box's volume
3
You selected Rectangular Box's Volume!
Enter the length, breadth and height: 3
4
5
The result is: 60
Q1. Define a class RESORT in C++ with the following descriptions:
Private Members:
Rno Room Number
Name Customer Name
Charges Per Day Charges
Days Days Of Stay
COMPUTE() A function to calculate amount
Public Members:
Getinfo() A function to enter content
Displayinfo() A function to display contents and
evoke COMPUTE()
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Resort
{
private: int rno;
char name[20];
int charges;
int days;
float compute()
{ int amount;
amount = (days*charges);
if (amount>11000)
amount = (1.02*amount);
return (amount);
Classes and Objects
}
public: void getinfo()
{
cout<<"Enter the Room number:";
cin>>rno;
cout<<"Enter the Customer name:";
cin>>name;
cout<<"Enter the Per day charges:";
cin>>charges;
cout<<"Enter the Days of stay:";
cin>>days;
}
void dispinfo()
{
cout<<"\nThe Room number is:"<<rno;
cout<<"\nThe Customer's name is:"<<name;
cout<<"\nThe Charges per day are:"<<charges;
cout<<"\nThe Days of stay are:"<<days;
cout<<"\nThe Amount is:"<<compute();
}
}
main()
{
clrscr();
Resort r1;
r1.getinfo();
r1.dispinfo();
getch();
}
Output:
Enter the Room number: 121
Enter the Customer name: Archit goel
Enter the Per day charges: 10000
Enter the Days of stay: 3
The Room number is: 121
The Customer's name is: Archit goel
The Charges per day are: 10000
The Days of stay are: 3
The Amount is: 30600
Q2. Define a class in C++ with the following descriptions:
A data member TrainNumber of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function CALFUEL( ) to calculate the value of Fuel as per the following
criteria
Distance Fuel
<=1500 250
more than 1500 and <=3000 1000
more than 3000 2500
Public Members:
A function FEEDINFO ( ) to allow user to enter values for the contents
and call function CALCFUEL ( ) to calculate the quantity of fuel.
A function SHOWINFO ( ) to display contents.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class travel
{
private: int tno;
char dest[20];
float dis;
float fuel;
float calfuel()
{ if (dis<=1500)
fuel = 250;
if (dis>1500 && dis<=3000)
fuel = 1000;
if (dis>3000)
fuel = 2500;
return(fuel);
}
public: void feedinfo()
{
cout<<"Enter the Train number:";
cin>>tno;
cout<<"Enter the Destination name:";
cin>>dest;
cout<<"Enter the distance:";
cin>>dis;
cout<<"The fuel required is:"<<calfuel();
}
void showinfo()
{
cout<<"\nThe Train number is:"<<tno;
cout<<"\nThe Destination's name is:"<<dest;
cout<<"\nThe Distance is:"<<dis;
}
}
main()
{
clrscr();
travel t1;
t1.feedinfo();
t1.showinfo();
getch();
}
Output:
Enter the Train number: 1023
Enter the Destination name: Mumbai
Enter the Distance: 1233
The fuel required is: 250
The Train number is: 1023
The Destinations name is: Mumbai
The Distance is: 1233
Q3. Define a class TESTMATCH in C++ with the following descriptions:
Private Members:
TestCode of type integer
Description of type string
NoOfCandidates of type integer
CenterReqd (number of centers required) of type integer
A member function CALCULATECNTR ( ) to calculate and return the number of
centers as (NoOfCandidates/100 + 1)
Public Members:
A function GETDATA ( ) to allow user to enter values for the contents and
call function CALCULATECNTR ( ) to calculate the number of centers.
A function PUTDATA ( ) to display contents.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class testmatch
{
private: int tcode;
char des[20];
int noc;
int cr;
float calculatecntr()
{
float a;
a = (noc/100) + 1 ;
return(a);
}
public: void getdata()
{ cout<<"Enter the Test Code:";
cin>>tcode;
cout<<"Enter the Description:";
cin>>des;
cout<<"Enter the No of Candidates:";
cin>>noc;
cout<<"Enter the number of centers required:";
cin>>cr;
cout<<"The Number of Centers
is:"<<calculatecntr(); }
void putdata()
{ cout<<"\n\nTEST MATCH INFORMATION";
cout<<"\nTest Match Code:"<<tcode;
cout<<"\nDescription:"<<des;
cout<<"\nTotal Candidates:"<<noc;
cout<<"\nCenters Required:"<<cr; }
}
main()
{
clrscr();
testmatch t1;
t1.getdata();
t1.putdata();
getch();
}
Output:
Enter the Test Code: 112
Enter the Description: Computers
Enter the No of Candidates: 250
Enter the number of centers required: 5
The Number of Centers is: 3
TEST MATCH INFORMATION
Test Match Code : 112
Description : Computers
Total Candidates : 250
Centers Required : 5
Q4. Define a class Flight in C++ with the following descriptions:
Private Members:
A data member FlightNumber of type integer
A data member Destination of type string
A data member Distance of float type
A data member Fuel of type float
A member function CalFuel ( ) to calculate the value of fuel as per the following criteria
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
more than 2000 2200
Public Members:
A function Feed_Info ( ) to allow user to enter values for the contents and
call function CalFuel ( ) to calculate the quantity of fuel.
A function Show_Fuel ( ) to display contents.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class travel
{
private: int fno;
char dest[20];
float dis;
float fuel;
float calfuel()
{ if (dis<=1000)
fuel = 500;
if (dis>1000 && dis<=2000)
fuel = 1100;
if (dis>2000)
fuel = 2200;
return(fuel);
}
public: void feedinfo()
{
cout<<"Enter the Flight number:";
cin>>fno;
cout<<"Enter the Destination name:";
cin>>dest;
cout<<"Enter the distance:";
cin>>dis;
cout<<"The fuel required is:"<<calfuel();
}
void showfuel()
{
cout<<"\nThe Flight number is:"<<fno;
cout<<"\nThe Destination's name is:"<<dest;
cout<<"\nThe Distance is:"<<dis;
}
}
main()
{
clrscr();
travel t1;
t1.feedinfo();
t1.showfuel();
getch();
}
Output:
Enter the Flight number: 1124
Enter the Destination name: Bangalore
Enter the Distance: 1400
The fuel required is: 1100
The Flight number is: 1124
The Destinations name is: Bangalore
The Distance is: 1400
Q5. Define a class employee in C++ which stores names of employees, his
department, and his salary in public area. In the class two member functions
getdata( ) and displaydata ( ) accept and display the data entries respectively.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
private: char name[20];
char dep[10];
int salary;
public: void getdata()
{
cout<<"Enter the Name of the Employee:";
cin>>name;
cout<<"Enter the Department:";
cin>>dep;
cout<<"Enter the salary:";
cin>>salary;
}
void displaydata()
{ cout<<"\nThe Name of the Employee
is:"<<name;
cout<<"\nThe Department's name is:"<<dep;
cout<<"\nThe Salary is:"<<salary; }
}
main()
{
clrscr();
employee e1;
e1.getdata();
e1.displaydata();
getch();
}
Output:
Enter the Name of the Employee : Archit goel
Enter the Department : Admin
Enter the salary: 20000
The Name of the Employee is: Archit goel
The Department's name is: Admin
The Salary is: 20000
Q6. Define a class STOCK in C++ with the following descriptions:
Private Members:
ICode of type integer
Item of type string
Price of type float
Qtn of type integer
Discount of type float
A member function FindDisc ( ) to calculate the discount as per the following rule:
If Qtn <= 50 Discount is 0
If 50 < Qtn <= 100 Discount is 5
If Qtn > 100 Discount is 10
Public Members:
A function Buy ( ) to allow user to enter values for the contents and call
function FindDisc ( ) to calculate the Discount.
A function ShowAll ( ) to display contents.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stock
{ private: int icode;
char item[10];
float price;
int qtn;
float dis;
float findisc()
{if (qtn<=50)
dis=0;
else if (qtn>50 && qtn<=100)
dis=5;
else if (qtn>100)
dis=10;
return (dis);
}
public: void Buy()
{
cout<<"Enter the Item Code:";
cin>>icode;
cout<<"Enter the Item Name:";
cin>>item;
cout<<"Enter the Price:";
cin>>price;
cout<<"Enter the Quantity of item:";
cin>>qtn;
cout<<"The Discount being offered
is:"<<findisc();
}
void showall()
{
cout<<"\nThe Item Code is:"<<icode;
cout<<"\nThe Item Name is:"<<item;
cout<<"\nThe Price is:"<<price;
cout<<"\nThe Quantity in stock is:"<<qtn;
}
}
main()
{
clrscr();
stock s1;
s1.Buy();
s1.showall();
getch();
}
Output:
Enter the Item Code: 112
Enter the Item Name: Paste
Enter the Price: 10
Enter the Quantity of item: 25
The Discount being offered is: 0
The Item Code is: 112
The Item Name is: Paste
The Price is: 10
The Quantity in stock is: 25
Q1. Define a class Garments in C++ with the following descriptions:
Private Members:
GCode of type string
Gtype of type string
GSize of type integer
GFabric of type string
GPrice of type float
A function Assign ( ) which calculates and assignsd the value of GPrice as follows:
For the value of GFabric COTTON,
GType Gprice(Rs.)
TROUSERS 1300
SHIRT 1100
For GFabric other than COTTON the above mentioned GPrice gets reduced by 10%
Public Members:
A constructor to assign initial values of GCode, Gtype and Gfabric with the
word NOT ALLOWED and GSize and Gprize with 0.
A function Input( ) to input the values of the Data Members GCode,
GType , GSize and GFabric and invoke the Assign( ) function.
A function Display( ) which displays the content of all the data members
for a Garment.
Constructors and
Destructors
Ans.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class garment
{ private: char gcode[25];
char gtype[25];
int gsize;
char gfabric[10];
float gprice;
void assign()
{if (strcmp(gfabric,"Cotton")==0)
{ if (strcmp(gtype,"Trousers")==0)
{gprice=1300;
else if (strcmp(gtype,"Shirt")==0)
gprice=1100;
}
else
{ if(strcmp(gtype,"Trousers")==0)
gprice=1170;
else if(strcmp(gtype,"Shirt")==0)
gprice==990;
}
cout<<"the price is:"<<gprice;
}
public: garment()
{strcpy(gcode,"NOT ALLOWED");
strcpy(gtype,"NOT ALLOWED");
strcpy(gfabric,"NOT ALLOWED");
gsize=0;
gprice=0; }
void input()
{cout<<"\nEnter the Garment Code:";
cin>>gcode;
cout<<"\nEnter the Garment Type:";
cin>>gtype;
cout<<"\nEnter the Garment Size:";
cin>>gsize;
cout<<"\nEnter the Garment Fabric:";
cin>>gfabric;
assign();
}
void display()
{cout<<"\nThe Garment Code is:"<<gcode;
cout<<"\nThe Garment Type:"<<gtype;
cout<<"\nThe Garment Size:"<<gsize;
cout<<"\nThe Garment Fabric:"<<gfabric;
}
};
void main()
{
clrscr();
garment g1;
g1.input();
g1.display();
getch();
}
Output:
Enter the garment code: wer11
Enter the garment type: Shirt
Enter the garment size: 34
Enter the garment fabric: Cotton
The price is:
The garment code is: wer11
The garment type is: Shirt
The garment size is: 34
The garment fabric is: Cotton
Q2. Define a class Play in C++ with the following descriptions:
Private members of play:
Playcode integer
Playtitle 25 character
Duration float
Noofscenes integer
Public members of class Play:
A constructor function to initialize Duration as 45 and
number of scenes as 5.
Newplay( ) function to accept values for Playcode and
Playtitle.
Moreinfo( ) function to assign the values of Duration and
Noofscenes with the help of corresponding values passed as
parameters to this function.
Shoplay( ) function to display all the data members on the
screen.
Ans.
#include<iostream.h>
#include<conio.h>
class play
{
private: int playcode;
char playtitle[25];
float duration;
int noofscenes;
public: play()
{ duration=45;
noofscenes=5; }
void newplay()
{cout<<"Enter the value of playcode:";
cin>>playcode;
cout<<"Enter the play title:";
cin>>playtitle;
}
void moreinfo(int,int)
{int d,no;
cout<<d;
cout<<no; }
void shoplay()
{cout<<"The play title is :"<<playtitle;
cout<<"\nThe play code is :"<<playcode;
cout<<"\nThe duration is :"<<duration;
cout<<"\nThe no of scenes are:"<<noofscenes;
}
};
void main()
{clrscr();
play p1;
int d,no;
cout<<"Enter the duration of the play:";
cin>>d;
cout<<"Enter the number of scenes:";
cin>>no;
p1.newplay();
p1.moreinfo(d,no);
p1.shoplay();
getch();
}
Output:
Enter the duration of the play: 30
Enter the number of scenes: 10
Enter the play code: 112
Enter the play title : SPS
The play title is: SPS
The play code is: 112
The duration is: 45
The no of scenes are: 5
Q1. C
Q1. Create table FURNITURE and ARRIVALS and write SQL commands for
the same.
mysql> create table furniture(no int, inm char(20),typ char(20), dost date, pr int, disc int);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into furniture values(1, 'White Lotus', 'Double Bed', '23-02-02',30000, 25);
Query OK, 1 row affected (0.03 sec)
mysql> insert into furniture values(2, 'Pink Feather', 'Baby Cot', '21-01-02', 7000, 20);
Query OK, 1 row affected (0.02 sec)
mysql> insert into furniture values(3, 'Dolphin', 'Baby Cot', '19-02-02', 9500,20);
Query OK, 1 row affected (0.02 sec)
mysql> insert into furniture values(4, 'Decent', 'Office Table', '01-01-02', 25000, 30);
Query OK, 1 row affected (0.03 sec)
mysql> insert into furniture values(5, 'Comfort Zone', 'Double Bed', '12-01-02', 25000, 25);
Query OK, 1 row affected (0.03 sec)
mysql> insert into furniture values(6, 'Donald', 'Baby Cot', '24-02-02', 6500, 15);
Query OK, 1 row affected (0.03 sec)
mysql> insert into furniture values(7, 'Royal Finish', 'Office Table', '20-02-02', 18000, 30);
Query OK, 1 row affected (0.01 sec)
mysql> insert into furniture values(8, 'Royal Tiger', 'Sofa', '22-02-02', 31000, 30);
Query OK, 1 row affected (0.00 sec)
mysql> insert into furniture values(9, 'Econo Sitting', 'Sofa', '13-12-01', 9500, 25);
Query OK, 1 row affected (0.02 sec)
mysql> insert into furniture values(10, 'Eating Paradise', 'Dining Table', '19-02-02', 11500, 25);
Query OK, 1 row affected (0.03 sec)
mysql> select * from furniture;
Structured
Query Language
+-----+--------------------+----------------+---------------+-------+------+
| no | inm | typ | dost | pr | disc |
+ ----+--------------------+----------------+--------------- +--------+------+
| 1 | White Lotus | Double Bed | 2023-02-02 | 30000 | 25 |
| 2 | Pink Feather | Baby Cot | 2021-01-02 | 7000 | 20 |
| 3 | Dolphin | Baby Cot | 2019-02-02 | 9500 | 20 |
| 4 | Decent | Office Table | 2001-01-02 | 25000 | 30 |
| 5 | Comfort Zone | Double Bed | 2012-01-02 | 25000 | 25 |
| 6 | Donald | Baby Cot | 2024-02-02 | 6500 | 15 |
| 7 | Royal Finish | Office Table | 2020-02-02 | 18000 | 30 |
| 8 | Royal Tiger | Sofa | 2022-02-02 | 31000 | 30 |
| 9 | Econo Sitting | Sofa | 2013-12-01 | 9500 | 25 |
| 10 | Eating Paradise | Dining Table | 2019-02-02 | 11500 | 25 |
+-----+-------------------+-----------------+---------------+-------+-------+
mysql> create table arrivals(no int,inm char(20), typ char(20), dost date, pr int, disc int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into arrivals values(11, 'Wood Comfort', 'Double Bed', '23-03-03', 25000, 25);
Query OK, 1 row affected (0.03 sec)
mysql> insert into arrivals values(12, 'Old Fox', 'Sofa', '20-02-03', 17000, 20);
Query OK, 1 row affected (0.02 sec)
mysql> insert into arrivals values(13, 'Micky', 'Baby Cot', '21-02-03', 7500, 15);
Query OK, 1 row affected (0.02 sec)
mysql> select * from arrivals;
+------+------------------+---------------+--------------+--------+-----+
| no | inm | typ | dost | pr | disc |
+------+------------------+---------------+--------------+--------+-----+
| 11 | Wood Comfort | Double Bed | 2023-03-03 | 25000 | 25 |
| 12 | Old Fox | Sofa | 2020-02-03 | 17000 | 20 |
| 13 | Micky | Baby Cot | 2021-02-03 | 7500 | 15 |
+------+------------------+---------------+--------------+---------+-----+
mysql> select * from furniture where typ='Baby Cot';
+----+----------------+------------+--------------+------+------+
| no | inm | typ | dost | pr | disc |
-----+-----------------+-----------+---------------+------+------+
| 2 | Pink Feather | Baby Cot | 2021-01-02 | 7000 | 20 |
| 3 | Dolphin | Baby Cot | 2019-02-02 | 9500 | 20 |
| 6 | Donald | Baby Cot | 2024-02-02 | 6500 | 15 |
+----+----------------+------------+--------------+-------+-----+
mysql> select inm from furniture where pr>15000;
+----------------+
| inm |
+----------------+
| White Lotus |
| Decent |
| Comfort Zone |
| Royal Finish |
| Royal Tiger |
+----------------+
mysql> select inm , typ from furniture where dot<22-01-02 order by inm;
Empty set, 1 warning (0.03 sec)
mysql> select inm, dost from furniture where disc > 25;
+---------------+---------------+
| inm | dost |
+---------------+---------------+
| Decent | 2001-01-02 |
| Royal Finish | 2020-02-02 |
| Royal Tiger | 2022-02-02 |
+----------- ----+--------------+
mysql> select count(*) from furniture where typ='Sofa';
+----------+
| count(*) |
+----------+
| 2 |
+----------+
mysql> insert into arrivals values(14, 'Velvet Touch', 'Double Bed', '25-03-03', 25000, 30);
Query OK, 1 row affected (0.01 sec)
mysql> select * from arrivals;
+------+-----------------+---------------- +--------------+--------+------+
| no | inm | typ | dost | pr | disc |
+------+-----------------+---------------- +--------------+--------+------+
| 11 | Wood Comfort | Double Bed | 2023-03-03 | 25000 | 25 |
| 12 | Old Fox | Sofa | 2020-02-03 | 17000 | 20 |
| 13 | Micky | Baby Cot | 2021-02-03 | 7500 | 15 |
| 14 | Velvet Touch | Double Bed | 2025-03-03 | 25000 | 30 |
mysql> select count(distinct typ) from furniture;
+---------------------+
| count(distinct typ) |
+---------------------+
| 5 |
+---------------------+
mysql> select avg(disc) from furniture where typ='Baby Cot';
+-----------+
| avg(disc) |
+-----------+
| 18.3333 |
+-----------+
mysql> select sum(pr) from furniture where dost < 12-02-02;
+---------+
| sum(pr) |
+---------+
| NULL |
+---------+
Q2. Create tables Customer & Item and write SQL commands for the same.
Ans.
mysql> create table item(i_id char(8) primary key, inm char(20), mfr char(3), pr int);
Query OK, 0 rows affected (0.13 sec)
mysql> insert into item values('PC01', 'Personal Computer', 'ABC',30000);
Query OK, 1 row affected (0.03 sec)
mysql> insert into item values('LC05', 'Laptop', 'ABC',55000);
Query OK, 1 row affected (0.08 sec)
mysql> insert into item values('PC03', 'Personal Computer', 'XYZ',32000);
Query OK, 1 row affected (0.02 sec)
mysql> insert into item values('PC06', 'Personal Computer', 'COM',37000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into item values('LC03', 'Laptop', 'PQR',57000);
Query OK, 1 row affected (0.02 sec)
mysql> select * from item;
+-------+-----------------------+-------+-------+
| i_id | inm | mfr | pr |
+-------+-----------------------+-------+-------+
| LC03 | Laptop | PQR | 57000 |
| LC05 | Laptop | ABC | 55000 |
| PC01 | Personal Computer | ABC | 30000 |
| PC03 | Personal Computer | XYZ | 32000 |
| PC06 | Personal Computer | COM | 37000 |
+-------+-----------------------+-------+-------+
mysql> create table customer (c_id int primary key, cnm char(15), city char(10), i_id char(5) references
item(i_id));
Query OK, 0 rows affected (0.05 sec)
mysql> insert into customer values(01, 'N Roy', 'Delhi', 'LC03');
Query OK, 1 row affected (0.02 sec)
mysql> insert into customer values(06, 'H Singh', 'Mumbai', 'PC03');
Query OK, 1 row affected (0.01 sec)
mysql> insert into customer values(12, 'R Pandey', 'Delhi', 'PC06');
Query OK, 1 row affected (0.03 sec)
mysql> insert into customer values(15, 'C Sharma', 'Delhi', 'LC03');
Query OK, 1 row affected (0.05 sec)
mysql> insert into customer values(16, 'K Agarwal', 'Bangalore', 'PC01');
Query OK, 1 row affected (0.02 sec)
mysql> select * from customer;
+-----+-------------+------------ +------+
| c_id | cnm | city | i_id |
+-----+-------------+------------ +------+
| 1 | N Roy | Delhi | LC03 |
| 6 | H Singh | Mumbai | PC03 |
| 12 | R Pandey | Delhi | PC06 |
| 15 | C Sharma | Delhi | LC03 |
| 16 | K Agarwal |Bangalore | PC01 |
+----+--------------+-----------+--------+
mysql> select * from customer where city='Delhi';
+------+----------+-------+------+
| c_id | cnm | city | i_id |
+------+----------+-------+------+
| 1 | N Roy | Delhi | LC03 |
| 12 | R Pandey | Delhi | PC06 |
| 15 | C Sharma | Delhi | LC03 |
mysql> select * from item where pr >= 35000 and pr <=55000;
+------+-------------------------- +------- +-------+
| i_id | inm | mfr | pr |
+------+-------------------------- +------- +-------+
| LC05 | Laptop | ABC | 55000 |
| PC06 | Personal Computer | COM | 37000 |
+------+-------------------------- +------- +------- +
mysql> select cnm,city,inm,pr from customer c,item i where c.i_id=i.i_id;
+--------------- +---------------- +------------------------- +-------+
| cnm | city | inm | pr |
+--------------- +---------------- +------------------------- +-------+
| N Roy | Delhi | Laptop | 57000 |
| C Sharma | Delhi | Laptop | 57000 |
| K Agarwal | Bangalore | Personal Computer | 30000 |
| H Singh | Mumbai | Personal Computer | 32000 |
| R Pandey | Delhi | Personal Computer | 37000 |
+--------------- +---------------- +------------------------- +-------+
mysql> update item set pr= pr+1000;
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from item;
+------+-------------------------- +-------+-------+
| i_id | inm | mfr | pr |
+------+-------------------------- +------- +-------+
| LC03 | Laptop | PQR | 58000 |
| LC05 | Laptop | ABC | 56000 |
| PC01 | Personal Computer | ABC | 31000 |
| PC03 | Personal Computer | XYZ | 33000 |
| PC06 | Personal Computer | COM | 38000 |
+------+-------------------------- +-------+-------+
mysql> select distinct city from customer;
+--------------- +
| city |
+--------------- +
| Delhi |
| Mumbai |
| Bangalore |
+--------------- +
mysql> select inm,max(pr),count(*) from item group by inm;
+------------------------ +---------+----------+
| inm | max(pr) | count(*)|
+------------------------ +---------+----------+
| Laptop | 58000 | 2 |
| Personal Computer | 38000 | 3 |
+------------------------ +---------+----------+
mysql> select cnm,mfr from item,customer where item.i_id=customer.i_id;
+--------------- +------+
| cnm | mfr |
+--------------- +------+
| N Roy | PQR |
| C Sharma | PQR |
| K Agarwal | ABC |
| H Singh | XYZ |
| R Pandey | COM |
+--------------- +-------+
mysql> select inm, pr*100 from item where mfr='ABC';
+------------------------ +---------+
| inm | pr*100 |
+------------------------ +---------+
| Laptop | 5600000 |
| Personal Computer | 3100000 |
+------------------------ +---------+
Q3. Create a table GAMES and PLAYERS and also write the SQL commands.
Ans.
mysql> create table games (gcode int primary key, gname char(20), type char(15),no int, pm int, sd date);
mysql> insert into games values(101, Carom Board, indoor, 2, 5000, 2004-01-23);
mysql> insert into games values(102, Badminton, outdoor, 2, 12000, 2003-12-12);
mysql> insert into games values(103, Table Tennis, indoor, 4, 8000, 2004-02-14);
mysql> insert into games values(105, Chess, indoor, 2, 9000, 2004-01-01);
mysql> insert into games values(108, Lawn Tennis, outdoor,4, 25000, 2004-03-19);
mysql> select * from games;
+------+--------------------- +---------------- +----+------+----------------+
| gcode| gname | type | no | pm | sd |
+------+--------------------- +---------------- +----+-------+---------------+
| 101 | carom board | indoor | 2 | 5000 | 2004-01-23 |
| 102 | badminton | outdoor | 2 | 12000 | 2003-12-12 |
| 103 | table tennis | indoor | 4 | 8000 | 2004-02-14 |
| 105 | chess | indoor | 5 | 9000 | 2004-01-01 |
| 108 | lawn tennis | outdoor | 4 | 25000 | 2004-03-19 |
+------+-------------------- +---------------- +----+-------+---------------+
5 rows in set (0.08 sec)
mysql> create table player (pcode int primary key, name char(20), gcode int references games (gcode));
mysql> insert into player values(1, Nabi Ahmed, 101);
mysql> insert into player values(2, Ravi Sahai, 108);
mysql> insert into player values(3, Jatin, 101);
mysql> insert into player values(4 ,Nazneen, 103);
mysql> select * from player;
+--------+--------------+-------+
| pcode | name | gcode |
+--------+--------------+-------+
| 1 | Nabi Anand | 101 |
| 2 | Ravi Sahai | 108 |
| 3 | Jatin | 101 |
| 4 | Nazneen | 103 |
+-------+--------------+-------+
4 rows in set (0.03 sec)
mysql> select gname, name, pm,g.gcode from games g, player p where p.gcode=g.gco
de and type='indoor';
+------------------- +---------------- +------+-------+
| gname | name | pm | gcode |
+------------------- +---------------- +------+-------+
| carom board | Nabi Anand | 5000 | 101 |
| carom board | Jatin | 5000 | 101 |
| table tennis | Nazneen | 8000 | 103 |
+------------------- +---------------- +------+-------+
mysql> select * from games where pm>7000;
+-------+--------------+----------+------+-------+------------+
| gcode | gname | type | no | pm | sd |
+-------+--------------+----------+------+-------+------------+
| 102 | badminton | outdoor | 2 | 12000 | 2003-12-12 |
| 103 | table tennis | indoor | 4 | 8000 | 2004-02-14 |
| 105 | chess | indoor | 5 | 9000 | 2004-01-01 |
| 108 | lawn tennis | outdoor | 4 | 25000 | 2004-03-19 |
+-------+--------------+----------+------+-------+------------+
mysql> select * from games order by sd asc;
+-------+--------------+----------+----+-------+------------+
| gcode | gname | type | no | pm | sd |
+-------+--------------+----------+----+-------+------------+
| 102 | badminton | outdoor | 2 |12000 | 2003-12-12 |
| 105 | chess | indoor | 5 | 9000 | 2004-01-01 |
| 101 | carom board | indoor | 2 | 5000 | 2004-01-23 |
| 103 | table tennis | indoor | 4 | 8000 | 2004-02-14 |
| 108 | lawn tennis | outdoor | 4 | 25000 | 2004-03-19 |
+-------+---------------+----------+----+-------+------------+
5 rows in set (0.00 sec)
mysql> select count(distinct no) from games;
+--------------------+
| count(distinct no) |
+--------------------+
| 3 |
+--------------------+
mysql> select max(sd), min(sd) from games;
+------------+------------+
| max(sd) | min(sd) |
+------------+------------+
| 2004-03-19 | 2003-12-12 |
+------------+------------+
mysql> select name, gname from games g, player p where g.gcode=p.gcode and g.pm>10000;
+------------+-------------+
| name | gname |
+------------+-------------+
| Ravi Sahai | lawn tennis |
+------------+-------------+
Q4. Create a table HOSPITAL and also write the SQL commands.
Ans.
mysql>create database BD;
Database Created
mysql> use BD;
Database changed
mysql>create table Hospital(No int, Name char(11), Age int, Department char(30), Datofdm date,
Chargess int, Sex char(1));
Query OK (0.36 sec)
mysql> describe Hospital;
+------------------- +---------------- +------+-------- +---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------- +---------------- +------+-------- +---------+-------+
| No | int(11) | YES | | NULL | |
| Name | char(18) | YES | | NULL | |
| Age | int(11) | YES | | NULL | |
| Department | char(30) | YES | | NULL | |
| Datofadm | date | YES | | NULL | |
| Charges | int(11) | YES | | NULL | |
| Sex | char(1) | YES | | NULL | |
+------------------- +---------------- +------+-------- +---------+-------+
mysql> insert into Hospital values(1,"Subash",65,"Surgery",20100310, 300,"M");
Query OK, 1 row affected, 1 warning (0.36 sec)
mysql> insert into Hospital values(2,"Rubina",24,"Nuclear Medicine",20100120, 250,"F");
Query OK, 1 row affected, 1 warning (0.11 sec)
mysql> insert into Hospital values(3,"Amit Nair",45,"Orthopedic",20090219, 200,"M");
Query OK, 1 row affected, 1 warning (0.13 sec)
mysql> insert into Hospital values(2,"Rishi Narula",16,"Surgery",19980101, 300,"M");
Query OK, 1 row affected, 1 warning (0.13 sec)
mysql> insert into Hospital values(5,"Shiba",35,"ENT",19980912, 280,"F");
Query OK, 1 row affected, 1 warning (0.14 sec)
mysql> insert into Hospital values(6,"Zeba",15,"ENT",19980224, 300,"F");
Query OK, 1 row affected, 1 warning (0.11 sec)
mysql> insert into Hospital values(7,"Karan Singh",34,"Cardiology",19980120, 350,"M");
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> insert into Hospital values(8,"Shiela",45,"ENT",19980223, 320,"F");
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> insert into Hospital values(9,"Ankit",19,"Cardiology",19980912, 350,"M");
Query OK, 1 row affected, 1 warning (0.17 sec)
mysql> insert into Hospital values(10,"Kavita",31,"Nuclear Medicine",19980119, 450,"F");
Query OK, 1 row affected, 1 warning (0.14 sec)
mysql> update Hospital set no='4' where Name="Rishi Narula";
Query OK, 1 row affected (0.45 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Hospital;
+----+----------------------- +------+-------------------------- +--------------+--------- +-----+
| No | Name | Age | Department | Datofadm | Charges | Sex |
+----+----------------------- +------+-------------------------- +--------------+--------- +-----+
| 1 | Subash | 65 | Surgery | 2010-03-10 | 300 | M |
| 2 | Rubina | 24 | Nuclear Medicine | 2010-01-20 | 250 | F |
| 3 | Amit Nair | 45 | Orthopedic | 2009-02-19 | 200 | M |
| 4 | Rishi Narula | 16 | Surgery | 1998-01-01 | 300 | M |
| 5 | Shiba | 35 | ENT | 1998-09-12 | 280 | F |
| 6 | Zeba | 15 | ENT | 1998-02-24 | 300 | F |
| 7 | Karan Singh | 34 | Cardiology | 1990-01-20 | 350 | M |
| 8 | Shiela | 45 | ENT | 1998-02-23 | 320 | F |
| 9 | Ankit | 19 | Cardiology | 1998-09-12 | 350 | M |
| 10 | Kavita | 31 | Nuclear Medicine | 1998-01-19 | 450 | F |
+----+----------------------- +------+-------------------------- +--------------+---------+------+
mysql> select * from Hospital where Department="ENT" or Department="Cardiology";
+----+----------------------- +------- +--------------- +--------------+--------- +-----+
| No | Name | Age | Department | Datofadm | Charges| Sex |
+- --+----------------------- +------- +--------------- +--------------+----------+-----+
| 5 | Shiba | 35 | ENT | 1998-09-12 | 280 | F |
| 6 | Zeba | 15 | ENT | 1998-02-24 | 300 | F |
| 7 | Karan Singh | 34 | Cardiology | 1990-01-20 | 350 | M |
| 8 | Shiela | 45 | ENT | 1998-02-23 | 320 | F |
| 9 | Ankit | 19 | Cardiology | 1998-09-12 | 350 | M |
+----+----------------------- +------- +--------------- +--------------+----------+-----+
mysql> select name from Hospital where Department="Orthopedic" and Sex="F";
Empty set (0.00 sec)
mysql> select Name,Charges, Age from Hospital where Sex="F";
+----------+-----------+------+
| Name | Charges | Age |
+----------+-----------+------+
| Rubina | 250 | 24 |
| Shiba | 280 | 35 |
| Zeba | 300 | 15 |
| Shiela | 320 | 45 |
| Kavita | 450 | 31 |
+----------+-----------+------+
mysql> select count(*) from Hospital where age>40;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
mysql> select * from Hospital order by datofadm;
+----+----------------------- +-----+---------------------------+----------------+---------+-----+
| No | Name | Age| Department | Datofadm | Charges| Sex |
+----+----------------------- +-----+---------------------------+----------------+---------+-----+
| 7 | Karan Singh | 34 | Cardiology | 1990-01-20 | 350 | M |
| 4 | Rishi Narula | 16 | Surgery | 1998-01-01 | 300 | M |
| 10| Kavita | 31 | Nuclear Medicine | 1998-01-19 | 450 | F |
| 8 | Shiela | 45 | ENT | 1998-02-23 | 320 | F |
| 6 | Zeba | 15 | ENT | 1998-02-24 | 300 | F |
| 5 | Shiba | 35 | ENT | 1998-09-12 | 280 | F |
| 9 | Ankit | 19 | Cardiology | 1998-09-12 | 350 | M |
| 3 | Amit Nair | 45 | Orthopedic | 2009-02-19 | 200 | M |
| 2 | Rubina | 24 | Nuclear Medicine | 2010-01-20 | 250 | F |
| 1 | Subash | 65 | Surgery | 2010-03-10 | 300 | M |
+----+-------------- -------- +-----+---------------------------+----------------+---------+-----+
mysql> insert into Hospital values(11,"Mukul",37,"ENT",'19980225', 300,"M");
mysql> select * from Hospital;
+-----+---------------------- +-----+---------------------------+----------------+----------+----+
| No | Name | Age| Department | Datofadm | Charges | Sex|
+-----+---------------------- +-----+---------------------------+----------------+----------+----+
| 1 | Subash | 65 | Surgery | 2010-03-10 | 300 | M |
| 2 | Rubina | 24 | Nuclear Medicine | 2010-01-20 | 250 | F |
| 3 | Amit Nair | 45 | Orthopedic | 2009-02-19 | 200 | M |
| 4 | Rishi Narula | 16 | Surgery | 1998-01-01 | 300 | M |
| 5 | Shiba | 35 | ENT | 1998-09-12 | 280 | F |
| 6 | Zeba | 15 | ENT | 1998-02-24 | 300 | F |
| 7 | Karan Singh | 34 | Cardiology | 1990-01-20 | 350 | M |
| 8 | Shiela | 45 | ENT | 1998-02-23 | 320 | F |
| 9 | Ankit | 19 | Cardiology | 1998-09-12 | 350 | M |
| 10| Kavita | 31 | Nuclear Medicine | 1998-01-19 | 450 | F |
| 11| Mukul | 37 | ENT | 1998-02-25 | 300 | M |
+-----+---------------------- +-----+---------------------------+-----------------+----------+----+
mysql> select count(distinct department) from Hospital;
+--------------------------------+
| count(distinct department) |
+--------------------------------+
| 5 |
+--------------------------------+
mysql> select count(*) from Hospital;
+----------+
| count(*) |
+----------+
| 11 |
+----------+
mysql> select max(age) from Hospital where sex='F';
+----------+
| max(age)|
+----------+
| 45 |
+----------+
mysql> select avg(Charges) from Hospital;
+----------------+
| avg(Charges) |
+----------------+
| 309.0909 |
+----------------+
mysql> select sum(Charges) from Hospital;
+-----------------+
| sum(Charges) |
+-----------------+
| 3400 |
+-----------------+
1 row in set (0.00 sec)
Q1:Write a program using pointers that accepts an array of integers and prints
the highest
Ans:
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[16], max;
int *p;
p=a;
cout<<"Enter contents: ";
for(int i=0; i<16; i++)
cin>>*(p+i);
for(i=0; i<16; i++)
{max= a[0];
if (a[i]>max)
max= a[i];
}
cout<<"Maximum is: "<<max;
getch();
}
OUTPUT
Enter contents: 1 2 3 4 5 6 7 8 9 0 8 1 2 4 5 7
Maximum is:9
Pointers
Q2: Write a program to using pointers which has a class 'stud' which accepts
a student's name and age and displays it.
ANS:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stud
{ char name[20];
int age;
public:
void getdata()
{ cout<<"\n Enter your name:";
gets(name);
cout<<"\n Enter your age:";
cin>>age;
}
void sdata()
{ cout<<"\n Name:";
puts(name);
cout<<" \n Age:"<<age;
}
};
void main()
{ clrscr();
stud *ptr;
ptr= new stud;
ptr-> getdata();
prt-> sdata();
getch();}
Output:
Enter your name:XYZ
Enter your age:17
Name:XYZ
Age:17
Q3: Write a program using pointers that accepts a string and converts it to
upper case.
ANS:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{ clrscr();
char *str;
str= new char[25];
cout<<"Enter string: ";
gets(str);
for(int i=0; *(str+i)!='\0'; i++)
{ str[i]= toupper(str[i]);
cout<<str[i];
}
getch();
}
Output:
Enter string: Springdales School
SPRINGDALES SCHOOL
Q4: Write a program using pointers that accepts two values and returns the
smaller one of them.
ANS:
#include<iostream.h>
#include<conio.h>
int *small(int &, int &);
void main()
{ clrscr();
int a, b, *res;
cout<<"\n Enter value of a: ";
cin>>a;
cout<<"\n Enter value of b: ";
cin>>b;
res=small(a,b);
cout<<"\n The smaller number is: ";
cout<<*res;
getch();
}
int *small(int &a, int &b)
{ if (a<b)
return &a;
else
return &b;
}
OUTPUT:
Enter value of a:5
Enter value of b:10
The smaller number is:5
Q5: Write a program using pointers(link list) which has a self referential
structure. The program accepts the size of the link list and thus accepts
name and age of people according to it.
ANS:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
struct stud
{
char name[20];
int age;
stud *next; // Self Referential Structure.
};
clrscr();
stud *first, *last, *x;
int i, n;
cout<<"\n Enter the size of the link:";
cin>>n;
if(n<=0)
{cout<<"Error";
return (0);
}
first = new stud;
cout<<"\n Enter data:-";
cout<<"\n \n Name:";
cin>>first->name;
cout<<"\n Age:";
cin>>first->age;
first->next=NULL;
last=first;
for(i=0;i<n-1; i++)
{x=new stud;
cout<<"\n Enter Name:";
cin>>x->name;
cout<<"\n Age:";
cin>>x->age;
x->next=NULL;
last->next=x;
last=x;
}
last = first;
cout<<"\n The link is:";
while(last!=NULL)
{
cout<<last->age;
last=last->next;
}
delete first;
return 0;
getch();
}
OUTPUT:
Enter the size of the link:2
Enter data:-
Enter name:Archit goel
Enter age:17
Enter name:Harsh
Enter age:17
The link list is:
Archit goel 17
Harsh 17
Q1: Write a program which has 2 classes, person and a derived class student.
Both of these classes accept the concerned data from the user and display
it.
ANS:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{ private:
char name[20];
int age;
public:
void readdata();
void dispdata();
};
class student: public person
{ private:
int roll;
int marks;
char cl;
public:
void getdata();
void showdata();
char compute();
};
Inheritance
void person::readdata()
{ cout<<"\n Enter name: ";
gets(name);
cout<<"\n Enter age: ";
cin>>age;
}
void person::dispdata()
{ cout<<"Name is: "<<name<<"\n";
cout<<"Age is: "<<age<<"\n";
}
void student::getdata()
{ readdata();
cout<<"\n Enter roll number: ";
cin>>roll;
cout<<"\n Enter marks: ";
cin>>marks;
cl=compute();
}
char student::compute()
{ char gd;
if (marks>40)
gd= 'P';
else
gd= 'F';
return gd;
}
void student::showdata()
{ dispdata();
cout<<"Roll no. is: "<<roll<<"\n";
cout<<"Marks are: "<<marks<<"\n";
cout<<"Pass/Fail: "<<cl<<"\n";
}
void main()
{ clrscr();
student ob;
ob.getdata();
ob.showdata();
getch();
}
Output:
Enter name:XYZ
Enter age:17
Enter roll number:6
Enter marks:96
Name:XYZ
Age:17
Roll Number:6
Pass/Fail: P
Q1. To search for an element from an array or elements or values
entered by user using binary search.
ANS:
#include<iostream.h>
#include<conio.h>
void main
{
clrscr();
int series[100];
int i,n,pos,x;
int first,last, middle;
cout<<"Enter the size of the list:";
cin>>n;
cout<<"Enter the list:";
for (i=0;i<n;i++)
{ cin>>series[i]; }
first=0;
pos=-1;
last=n-1;
cout<<"Enter the value to be searched;";
cin>>x;
while ((first<=last) && (pos==-1))
{ middle=(first+last)/2;
if ( series[middle]==x )
{pos=middle;}
else
if (series[middle]<x)
{ first = middle+1;}
else
{last=middle-1;}
}
Arrays
if (pos>(-1))
cout<<"The position of the element="<<++pos;
else
cout<<"Unsuccessful search";
getch();
}
Output:
Enter the size of the list: 4
Enter the list: 1
5
7
9
Enter the element to be searched for: 8
Unsuccessful search
Q2. To sort names entered by user in ascending order using bubble
sort.
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
char main()
{
clrscr();
char a[10][10];
char temp[50];
cout<<"Enter 10 names:";
for (int i=0; i<10; i++)
gets (a[i]);
for (i=0; i<10; i++)
{ for (int j=0; j<10-i-1; j++)
{ if (strcmp (a[j] , a[j+1])>0)
{ strcpy (temp,a[j]);
strcpy (a[j], a[j+1]);
strcpy (a[j+1], temp);
}
}
}
for (i=0; i<10; i++)
cout<<a[i]<<"\t";
}
Output:
Enter the names:
Avf
Terd
Ghgf
Njnhdb
Bhjkl
Vhjd
Hjk
Wer
Zuhfj
Qwerty
Avf Bhjkl Ghgf Hjk Njnhdb Qwerty Terd Wer Zuhfj
Q3. To sort integer values entered by user in ascending order using
insertion sort.
Ans.
#include<iostream.h>
#include<conio.h>
main()
{
int a[10];
clrscr();
cout<<"Enter the array";
for (int i=0; i<5; i++)
cin>>a[i];
for(i=0; i<5; i++)
{ int temp = a[i],j=i-1;
while (temp<a[j] && j>=0)
{ a[j+1]=a[j];
j--; }
a[j+1]=temp;
}
cout<<"\nThe sorted array:";
for (i=0; i <5 ; i++)
cout<<a[i]<<"\t";
getch();
}
Output:
Enter the array: 9
5
11
7
2
3
The sorted array: 2 3 5 7 9 11
Q4. To search for a value from the values entered by user using linear
search
Ans.
#include<iostream.h>
#include<conio.h>
void main()
{
int a [10];
int pos, i, value;
cout <<"Please enter the values:";
for (i=0;i<5;i++)
{ cin >>a[i];
}
cout<<"Enter the value to be searched:";
cin>> value;
for (i=0;i<5;i++)
{ if (a[i] == value)
pos= i ;
}
cout<<" the value is at :"<<pos+1;
}
Output:
Please enter the values: 5
8
7
9
11
Enter the value to be searched: 7
The value is at : 3
Q5. To sort a given array using selection sort
Ans.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void selection(int arr[], int l);
void main()
{
clrscr();
int num[10],i;
cout<<"\nPlease enter the values:";
for (i=0; i<10; i++)
cin>>num[i];
selection (num,10);
cout<<"\n the sorted array:";
for (i=0; i<10; i++)
cout<<num[i]<<"\t";
getch();
}
void selection (int arr[], int l)
{
int i,j;
for (i=0; i<l-1; i++)
{ int small = i;
for (j=i+1; j<l; j++)
if (arr[small] > arr[j])
small=j;
if (small!=i)
{ int temp = arr[i];
arr[i]= arr[small];
arr[small]= temp;
}
}
}
Output:
Please enter the values:
1 8 4 12 17 5 10 2 6 7
The Sorted array:
1 2 4 5 6 7 8 10 12 17
Q1. To add lines, display lines, count the number of words and reverse a
line of a text file using a menu driven program.
Ans.
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
void create()
{char ch;
fstream fil;
char lin[80],Q;
fil.open("Stud.TXT", ios:: out);
do
{ cout<<"\n Enter text :";
gets(lin);
fil<<lin<<endl;
cout<<" More (Y/N) ? : ";
cin>>Q;
}
while(toupper(Q) == 'Y');
fil.close();
}
void addatend()
{ fstream fil;
char lin[80], Q;
fil.open(" Stud.TXT" , ios:: app);
Data File
Handling
do
{ cout<<"\n Enter text>>";
gets(lin);
fil<<lin<<endl;
cout<<"\n More(Y/N) ? : ";
cin>>Q;
}
while(Q== 'Y');
fil.close();
}
void display()
{ fstream fil;
char lin[80];
fil.open("Stud.TXT", ios::in);
while(fil.getline(lin,80))
cout<<lin<<endl;
fil.close();
}
int count()
{ fstream fil;
char lin[80];
fil.open("Stud.TXT" , ios::in);
int cnt= 0;
while(fil.getline(lin,80))
cnt++ ;
fil.close();
return cnt;
}
void countword()
{ fstream fil;
char lin[80], ch;
fil.open("Stud.TXT", ios::in);
int cword= 0;
while(!fil.eof())
{ ch= fil.get();
if(ch== ' ' || ch== '\n')
cword++;
}
cout<<"\n No. of words are:"<<cword<<endl;
fil.close();
}
void revlineoffile()
{ fstream fil;
char lin[80];
fil.open("Stud.TXT", ios::in);
while(fil.getline(lin,80))
{ for(int I = strlen(lin)-1; I>=0; I--)
cout<<lin[I];
cout<<endl;
}
fil.close();
}
void main()
{
clrscr();
char ch;
do
{ cout<<"\n T: Number of lines";
cout<<"\n C: Create";
cout<<"\n A: Add Lines";
cout<<"\n D: Display";
cout<<"\n W: Words";
cout<<"\n R: Reverse Line";
cout<<"\n Quit";
cout<<"\n Enter your choice :";
cin>>ch;
switch(ch)
{ case 'C' : create();
break;
case 'A' : addatend();
break;
case 'D' : display();
break;
case 'W' : countword();
break;
case 'R' : revlineoffile();
break;
case 'T' : count();
break;
}
}while(ch!='Q');
}
Output:
T: Number of lines
C: Create
A: Add Lines
D: Display
W: Words
R: Reverse Line
Q: Quit
Enter your choice : C
Enter Text: Welcome to SPS!!
More(Y/N)? : Y
Enter Text: Have a nice day!!
More(Y/N)? :N
T: Number of lines
C: Create
A: Add Lines
D: Display
W: Words
R: Reverse Line
Q: Quit
Enter your choice : D
Welcome to SPS!!
Have a nice day!!
Q2. To add lines, display lines, count the number of words and reverse a
line of a binary file using a menu driven program.
Ans.
# include<iostream.h>
# include<conio.h>
# include<fstream.h>
# include<string.h>
# include<stdio.h>
class stud
{
int rl;
char nm[40];
float m;
public: void gtdt();
void showdt();
void moddata();
int getrollno();
};
void stud::gtdt(void)
{ char ch;
cin.get(ch);
clrscr();
cout<<"\nEnter the data for the student.";
cout<<"\nEnter roll number: ";
cin>>rl;
cout<<"\nEnter name: ";
gets(nm);
cout<<"\nEnter marks: ";
cin>>m;
}
void stud::showdt()
{ cout<<"\t\n"<<rl<<"\t"<<nm<<"\t"<<m;
}
void stud::moddata()
{ cout<<"\nEnter name: ";
gets(nm);
cout<<"\Enter marks: ";
cin>>m;
}
int stud::getrollno()
{ return rl;
}
void search(int rno)
{ stud st1;
int flag;
fstream fin;
fin.open("student.dat",ios::binary|ios::in);
if(fin==NULL)
{ cout<<"\nFILE DOES NOT EXIST!!!";
return ;
}
while(fin.read((char*)&st1,sizeof(stud)))
{ if(rno==st1.getrollno())
{ cout<<"\nThe student details are as follows";
st1.showdt();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n\nRollno does not exist";
fin.close() ;
}
void create()
{ stud st1;
fstream fin("student.dat",ios::binary|ios::out);
char cho;
do
{ st1.gtdt();
fin.write((char*)&st1,sizeof(stud));
cout<<"\n More records Y/N";
cin>>cho;
} while(cho=='y');
fin.close();
}
void append()
{ stud st1;
fstream fin("student.dat",ios::binary|ios::app);
char choice;
do
{ st1.gtdt();
fin.write((char*)&st1,sizeof(stud));
cout<<"\nMore records Y/N";
cin>>choice;
}
while(choice=='y');
fin.close();
}
void delet(int rno)
{ stud st1;
int flag=0;
fstream fin;
fin.open("student.dat",ios::binary|ios::in);
if(fin==NULL)
{ cout<<"\nFILE DOES NOT EXIST!!!";
return;
}
fstream fout("temp.dat",ios::binary|ios::out);
while(fin.read((char*)&st1,sizeof(st1)))
{ if(rno!= st1.getrollno())
fout.write((char*)&st1,sizeof(st1));
else
flag=1;
}
if(flag==0)
cout<<"\nROLL NUMBER DOES NOT EXIST";
else
cout<<"\nRecord deleted.....";
fin.close();
fout.close();
remove("student.dat");
rename("temp.dat","student.dat");
}
void display()
{ stud st1;
fstream fin("student.dat",ios::in|ios::binary);
if(fin==NULL)
{ cout<<"THE FILE DOES NOT EXIST!!!";
return; }
clrscr();
cout<<"\n\n The student details";
while(fin.read((char*)&st1,sizeof(stud)))
st1.showdt();
fin.close();
getch();
}
void modify(int rno)
{
stud st1;
int flag=0, rec=0;
fstream fin;
fin.open("student.dat",ios::in|ios::out|ios::binary);
if(fin==NULL)
{ cout<<"The file dose not exist";
return;
}
while(fin.read((char*)&st1,sizeof(stud)))
{ rec++ ;
if(rno==st1.getrollno())
{ cout<<"The student details as follows:";
st1.showdt();
cout<<"\n Enter new details:";
st1.moddata();
fin.seekg((rec-1)*sizeof(stud),ios::beg);
fin.write((char*)&st1,sizeof(stud));
flag=1;
break;
}
}
if(flag==0)
cout<<"\n Roll no does not exist";
else
cout<<"\n Record is modified";
}
void main()
{ stud stu;
int choice,rn;
do
{ clrscr();
cout<<"\n1. Create";
cout<<"\n2. Append";
cout<<"\n3. Search";
cout<<"\n4. Delete";
cout<<"\n5. Modify";
cout<<"\n6. Display All";
cout<<"\n\n Enter your choice: ";
cin>>choice;
switch(choice)
{ case 1 : cout<<"Enter the details of the student record
to be added";
create();
break;
case 2 : cout<<"Enter the details of the student record
to be added";
append();
break;
case 3 : cout<<"Enter roll no. to be searched";
cin>>rn;
search(rn);
break;
case 4 : cout<<"Enter roll no. to be deleted";
cin>>rn;
delet(rn);
break;
case 5 : cout<<"Enter roll no. to be modified";
cin>>rn;
modify(rn);
break;
case 6 : display();
break;
} getch();
}while(choice>=1 && choice<=6);
}
Output:
1. Create
2. Append
3. Search
4. Delete
5. Modify
6. Display all
Enter your choice: 1
Enter the data for student
Enter roll no.:10
Enter name: Archit goel
Enter marks:88
More records(Y/N):n
1. Create
2. Append
3. Search
4. Delete
5. Modify
6. Display all
Enter your choice: 6
The student details :
10 Archit goel 88
Q1. To write a program to enter a value in a stack.
Stacks And Queues
Ans.
#include<iostream.h>
#include<conio.h>
const int max = 10;
void push (int s[], int &t)
{ if(t<max-1)
{ cout<<"\n Data:";
cin>>s[++t];
} else
cout<<"\n Stack is full"<<endl;
}
void pop(int s[] , int &t)
{ if(t!= -1)
cout<<s[t--]<<"\n Deleted"<<endl;
else
cout<<"\n Stack is empty"<<endl;
}
void stackdisp(int s[], int t)
{ for(int i = t; i>=0 ; i--)
cout<<s[i]<<endl;
}
void main()
{ int stack[max], top= -1;
char ch;
clrscr();
do
{ cout<<"\n P : Push";
cout<<"\n O : Pop";
cout<<"\n D : Display";
cout<<"\n Q : Quit";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
{ case 'P' : push(stack,top);
break;
case 'O' : pop(stack, top);
break;
case 'D' : stackdisp(stack, top);
break;
}
}while (ch!= 'Q');
}
Output :
P : Push
O : Pop
D : Display
Q : Quit
Enter your choice: P
Data:3
Q2. To enter a value in a stack using dynamic stack.
Ans.
#include<iostream.h>
#include<conio.h>
struct node
{ int data;
node*next;
};
class stack
{ node*top;
public: stack() { top=NULL; }
void push();
void pop();
void disp();
~ stack();
};
void stack::push()
{ node*temp;
cout<<"Data:";
cin>>temp->data;
top=temp->next;
top=temp;
}
void stack::pop()
{ if(top!=NULL)
{ node*temp=top;
cout<<"Deleted"<<top->data;
top=top->next;
delete temp;
}
else
cout<<"Stack is empty";
}
void stack::disp()
{ node*temp=top;
while(temp!=NULL)
{ cout<<temp->data<<endl;
temp=temp->next;
}
}
void stack:: ~ stack()
{ while(top!=NULL)
{ node*temp=top;
top=top->next;
delete temp;
}
}
void main()
{ clrscr();
stack st;
char ch;
do
{ cout<<"Enter P/O/D/Q: ";
cin>>ch;
switch(ch)
{ case 'P' : st.push();
break;
case 'O' : st.pop();
break;
case 'D' : st.disp();
break;
}
}while (ch!='Q');
}
Output:
Enter P/O/D/Q: P
Data: 10
Enter P/O/D/Q: P
Data: 11
Enter P/O/D/Q: O
Deleted 11
Q3. Write a program using Queues
Ans.
#include<iostream.h>
#include<conio.h>
const int max = 10;
void qinsert (int q[], int &r, int f)
{ if((r+1)% max!= f)
{ r= (r+1)% max;
cout<<"\n Data:";
cin>>q[r];
}
else
cout<<"\n Queue is full "<<endl;
}
void qdelete(int q[], int r, int f)
{ if(r!= f)
{ f= (f+1)% max;
cout<<q[f]<<"\n Deleted"<<endl;
}
else
cout<<"\n Queue is empty"<<endl;
}
void qdisp(int q[], int r,int f)
{ int cn= f;
while(cn!= r)
{ cn= (cn+1)% max;
cout<<q[cn]<<endl;
}
}
void main()
{ clrscr();
int que[max], rear=0, front=0;
char ch;
do
{ cout<<"\n I : Insert";
cout<<"\n D : Delete";
cout<<"\n O : Display";
cout<<"\n Q : Quit";
cout<<"\n Enter choice: ";
cin>>ch;
switch(ch)
{ case 'I' : qinsert(que, rear, front);
break;
case 'D' : qdelete(que , rear, front);
break;
case 'O' : qdisp(que, rear, front);
break;
}
}while(ch!= 'Q');
}
Output:
I : Insert
D : Delete
O : Display
Q : Quit
Enter choice : I
Data : 4
5
1
I : Insert
D : Delete
O : Display
Q : Quit
Enter choice: O
4
5
1
I : Insert
D : Delete
O : Display
Q : Quit
Enter choice: D
1 Deleted
Q4. Write a program using Dynamic Queues
Ans.
#include<iostream.h>
#include<conio.h>
struct node
{ int data;
node*next;
};
class queue
{ node*rear,*front;
public: queue() {rear=NULL; front=NULL; }
void qinsert();
void qdelete();
void qdisp();
~ queue();
};
void queue::qinsert()
{ node*temp;
temp=new node;
cout<<"data:";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{ rear=temp;
front=temp;
}
else
{ rear->next=temp;
rear=temp;
}
}
void queue::qdelete()
{ if(front!=NULL)
{ node*temp=front;
cout<<"deleted"<<front->data<<"\n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL;
}
else
cout<<"Queue empty";
}
void queue::qdisp()
{ node*temp=front;
while(temp!=NULL)
{ cout<<temp->data<<endl;
temp=temp->next;
}
}
void queue::~queue()
{ while(front!=NULL)
{ node*temp=front;
front=front->next;
delete temp;
}
}
void main()
{ clrscr();
queue q;
char ch;
do
{ cout<<"enter I/D/O/Q: ";
cin>>ch;
switch(ch)
{ case 'I' :q. qinsert();
break;
case 'D' :q.qdelete();
break;
case 'O' :q.qdisp();
break;
}
}while(ch!='Q');
}
Output:
Enter I/D/O/Q: I
Data: 5
Enter I/D/O/Q: I
Data: 4
Enter I/D/O/Q: D
Deleted 4
Enter I/D/O/Q: O
5