cout<<"\n Name: "

advertisement
Computer Science
Practical
Work
(SESSION 2011-2012)
SUBITTED TO:
SUBMITTED BY:
DHEERAJ KAUSHIK
XII-B (I-SHIFT)
MR. M L MEENA
(P.G.T. COMP. SC.)
ROLL NO. : 9151080
Basic concept of OOP’S
 Data Abstraction
 Data Encapsulation
 Inheritance
 Modularity
 Polymorphism
DATA ABSTRACTION:
Abstraction refers to the act of representing essential
features without including the background details or
explanation.
Example:
You are driving a car. You only know the essential features to drive a car
e.g., gear handling, steering handling, use of clutch, accelerator, brakes etc.
But while you are driving do you get into internal details of the car like
wiring, motor working etc.? You just change the gears of apply the brakes
etc. What is happening is hidden from you. This is abstraction where you
only know the essential things to drive a car without including the
background details or explanation. Another example is of ‘switch board’.
You only press certain switches according to your requirement. What is
happening inside, how it is happening inside, how it is happening etc. you
needn’t know. Again, in this abstraction, you only know the essential things
to operate on the switch board without the knowledge the background
details of the switchboard.
DATA ENCAPSULAION:
The wrapping up of data and operation/function (that
operate on the data) into a single entity (called class) is
known as encapsulation.
Example:
In a big company, there are so many departments, sales, account, payroll,
purchase, production etc. each department has its own personnel that
maintain its data. Suppose an employee in the production dept. wants to
know how much raw material has been purchased for the next month. The
production dept. employee would not be allowed to himself go throughthe
purchase dept. files. Rather, he`ll issue a memo to the purchase dept.
requesting the desired information. Then some employee of the purchase
dept. will go through the data files for him and pertain to his requests. This
practice ensures that the data is accessed accurately and that it is not
corrupted by inept outsiders. Therefore, ‘Department data and department
employees are encapsulated into a single entity, the department.’
INHERITANCE:
Inheritance is the capability of one class of things to
inherit capabilities of properties from another class.
Example:
‘Humans’ certain properties, such as the ability to speak, breath, eat, drink,
etc. etc. But these properties are not unique to humans. The class ‘Human’
inherits these properties from the class ‘Mammal’ which again inherits some
of its properties from another “Animal’. The class “car” inherits some of its
properties from the class ‘Automobiles” which inherits some of its
properties from another class ‘Vehicles”. The capability to pass down
properties is a powerful one. It allows us to describe things in an
economical way. ‘Automobiles and ‘Pulled Vehicles’ are subclasses of
‘Vehicles’. ‘Vehicles are base class of ‘Automobiles’ and ‘Pulled Vehicles’.
MODULARITY:
Modularity is the property of a system that has been
decomposed into a set of cohesive and loosely coupled
modules.
Example:
You must have seen a complete music system. Let us assume that our
program represents the music system. The music system comprises of
speakers, cassette-player, record-player, cd-player, tuner etc. Now, these
parts are complete units in themselves, yet they are themselves, yet they
are a subpart of the music system. This is modularity. Similarly, we can
achieve modularity in programs also. A program can be divided into various
modules each representing speakers, cassette-player… etc. See each
module is a complete unit itself yet it works in accordance with other
modules in order to achieve one single goal i.e. music.
POLYMORPHISM:
Polymorphism is the ability for a message or data to be
processed in more than one form.
Example:
‘Human’ is a subclass of ‘Mammal’. Similarly ‘Dog’, ‘Cat’, are also subclasses
of ‘Mammals’. Mammals can see through day-light. So if a message ‘see
through day light’ is passed to all mammals, they will behave alike. Now if a
message ‘see through day light’is passes to all mammals, then humans and
dogs will not be able to see at night whereas cat will be able to see at night
also. Here cats behave differently than other mammals in response to a
message or action.
INDEX
S.No
.
PROGRAMES
1.
WAP USING CLASSES AND OBJECTS TO CREATE CLASS BOOK.
2.
WRITE A MENU DRIVEN PROGRAM TO CREATE CLASS BANK TO
INITIALISE DEPOSIT, WITHDRAW &amp; DISPLAY DEETAILS OF THREE
CUSTOMERS.
WAP YO CREATE CLASS ADMISSION THAT GENERATE TWO RANDOM
ADMISSION NUMBER FROM THE LIST OF GIVEN ADMISSION
NUMBERS.
WAP TO SHOW WORKING OF CONSTRUCTOR AND DESTRUCTOR.
3.
4.
5.
6.
7.
8.
9.
10.
11.
WAP TO MAINTAIN DETAILS OF COLLEGE STUDE3NTS AND PRINT
THEM (INHERITANCE).
WAP TO SHOW CONSTRUCTOR OVERLOADING.
WAP TO CREATE TEXT FILE WHICH PERFORM THE FOLLOWING
ACTIONS
i. COUNT NO. OF WORDS
ii. COUNT NO. OF LINES
iii. COUNT AND DISPLAY WORDS STARTING WITH VOWELS AND
STORE THEM IN A FILE
iv. COUNT AND DISPLAY WORDS STARTING WITH UPPERCASE
ALPHABETS
v. COUNT NO. OF BLANK SPACES
WAP TO CREATE BINARY FILES WITH FIVE RECORDS AND DISPLAY.
WAP TO SEARCH RECORD IN A BINARY FILE.
WAP TO APPEND A RECORD IN A BINARY FILE.
WAP TO INSERT DATA IN A BINARY FILE.
DATE
SIGN.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
WAP TO DELETE A RECORD IN A BINARY FILE.
WAP TO MODIFY A RECORD IN A BINARY FILE.
WRITE A MENU DRIVEN PROGRAM FOR BINARY AND LINEAR
SEARCH IN A ARRAY.
CREATE A MENU DRIVEN INSERT &amp; DELETE AN ELEMENT IN A
SORTED FILE.
WAP TO MERGE TWO ARRAYS A IN SACENDING ORDER, B IN
DESCENDING ORDER INTO THIRD ARRAY C INASCENDING ORDER.
CREATE MENU DRIVEN PROGRAM TO CHOOSE SORTING
TECHNIQUES SELECTION, BUBBLE.
WAP TO CONVERT I-D ARRAY TO 2-D ARRAY.
CREATE MENU DRIVEN PROGRAM FOR PUSH &amp; POP IN STACK AS
LINKED LIST.
WAP TO SHOW INSERTION AND DELETION IN QUEUES.
QURIES OF SQL TABLE 1 STUDENT
QURIES OF SQL TABLE 2 HOSPITAL
QURIES OF SQL TABLE 3 TEACHER
QURIES OF SQL TABLE 4 FURNITURE AND ARRIVALS
QURIES OF SQL TABLE 5 INTERIORS AND NEWONES
Q.1) W.A.P. using classes and objects to create the class book.
#include&lt;iostream.h&gt;
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
class BOOK {int book_no;
char book_title[20];
float price;
float total_cost(int n)
{float total;
total=n*price;
return total;
}
public:
void INPUT()
{cout&lt;&lt;&quot;\n Enter Book No.: &quot;;
cin&gt;&gt;book_no;
cout&lt;&lt;&quot;\n Enter Book Title: &quot;;
gets(book_title);
cout&lt;&lt;&quot;\n Enter Price: &quot;;
cin&gt;&gt;price;
}
void PURCHASE()
{int n;
float TOT;
cout&lt;&lt;&quot;\n Enter no of copies to be purchase: &quot;;
cin&gt;&gt;n;
TOT=total_cost(n);
cout&lt;&lt;&quot;\n Your total cost is: &quot;&lt;&lt;TOT;
}
};
void main()
{ clrscr();
BOOK buy;
buy.INPUT();
buy.PURCHASE();
getch();
}
Output
Q.2) Write menu driven program to create class to initialize, deposit,
withdraw and display the details.
#include&lt;iostream.h&gt;
#include&lt;ctype.h&gt;
#include&lt;conio.h&gt;
class Account{ char name[31];
int acc_no;
char act;
float balance;
public:
};
void initial()
{ cout&lt;&lt;&quot;\n Name: &quot;;
cin&gt;&gt;name;
cout&lt;&lt;&quot;\n Accoutn No.: &quot;;
cin&gt;&gt;acc_no;
cout&lt;&lt;&quot;\n Enter Amount: &quot;;
cin&gt;&gt;balance;
cout&lt;&lt;endl;
}
void deposit(float amt)
{balance+=amt;
cout&lt;&lt;&quot; Amount Deposited&quot;;
}
void withdraw(float amt)
{if((amt-balance&gt;=1000))
{ balance=amt-balance;
cout&lt;&lt;&quot;\n Amount Withdrawn&quot;;
}
else
{cout&lt;&lt;&quot;\n Min Bal should be 1000&quot;;
cout&lt;&lt;&quot; You can withdraw only: &quot;&lt;&lt;balance-1000;
}
}
void display()
{cout&lt;&lt;&quot;\n\n\t\t\t !!Your Account Details!!&quot;;
cout&lt;&lt;&quot;\n\n Account no.:&quot;&lt;&lt;acc_no;
cout&lt;&lt;&quot;\n Accoutn Holder: &quot;;
cout&lt;&lt;name;
cout&lt;&lt;&quot;\n Balance: &quot;&lt;&lt;balance;
}
int getacno()
{ return acc_no;}
void main()
{ clrscr();
Account bank;
int choice;
cout&lt;&lt;&quot;\n Menu&quot;;
cout&lt;&lt;&quot;\n 1.For Deposit&quot;;
cout&lt;&lt;&quot;\n 2.For WithDraw&quot;;
cout&lt;&lt;&quot;\n Enter Choice(1/2): &quot;;
cin&gt;&gt;choice;
switch(choice)
{ case 1:bank.initial();
bank.deposit(100000);
break;
case 2:bank.initial();
bank.withdraw(100000);
break;
default:cout&lt;&lt;&quot;\n !!Wrong Choice!!&quot;;
}
bank.display();
getch();
}
Output
Q.3) WAP to create the class ADMISSION that generates two random
admission nos. from the list of given admission nos.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;stdio.h&gt;
int i;
class ad{ int adno;
char name[10];
float fees;
int clas;
public:
void readdata();
void draw_nos();
int getadno()
{
return adno; }
};
ad A1[5];
void ad::draw_nos()
{
int n1=0,n2=0;
randomize();
n1=random(4);
n2=random(4);
for(i=0;i&lt;3;i++)
if((A1[i].getadno()==n1)||(A1[i].getadno()==n2));
cout&lt;&lt;&quot;\nRandomly generated admission nos are:&quot;&lt;&lt;n1&lt;&lt;&quot; and &quot;&lt;&lt;n2;
}
void ad::readdata()
{
cout&lt;&lt;&quot;\nEnter admission no:&quot;;
cout&lt;&lt;&quot;Name:&quot;;
cout&lt;&lt;&quot;Class:&quot;;
cout&lt;&lt;&quot;Fees:&quot;;
void main()
{clrscr();
for(i=0;i&lt;3;i++)
{
A1[i].readdata();
}
A1[i-1].draw_nos();
getch();
}
cin&gt;&gt;adno;
cin&gt;&gt;name;
cin&gt;&gt;clas;
cin&gt;&gt;fees;
OUTPUT
}
Q.4)WAP to show working of constructor and destructor in a class.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
class A{ public:
A()
{cout&lt;&lt;&quot;\n Constructor A&quot;;}
~A()
{cout&lt;&lt;&quot;\n Destructor A&quot;;}
};
class B{ public:
B()
{cout&lt;&lt;&quot;\n Constructor B&quot;;}
~B()
{cout&lt;&lt;&quot;\n Destructor B&quot;;}
};
class C{ public:
A ob1,ob2;
B ob3;
C()
{cout&lt;&lt;&quot;\n Constructor C&quot;;}
~C()
{cout&lt;&lt;&quot;\n Destructor C&quot;;}
};
void main()
{clrscr();
C oc;
B ob;
A oa;
getch();
}
Output
Q.5)Write a program to maintain the details of college students and
print them using inheritance.
#include&lt;iostream.h&gt;
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
const int len = 25 ;
class person
{
char name[len];
int age;
public:
void readperson(void) ;
void displayperson(void)
{
cout&lt;&lt;&quot;Name : &quot;;
cout.write(name,len);
cout&lt;&lt;&quot;\t Age : &quot; &lt;&lt;age&lt;&lt;&quot;\n&quot;;
}
};
void person::readperson(void)
{
for(int i=0;i&lt;len;i++)
name[i]=' ';
cout&lt;&lt;&quot;Enter name of the person :&quot;;
gets(name);
cout&lt;&lt;&quot;Enter age :&quot;;
cin&gt;&gt;age;
}
class student: public person
{
int rollno;
float average ;
public:
void readstudent(void)
{
readperson();
cout&lt;&lt;&quot;Enter roll no. : &quot;;
cin&gt;&gt;rollno;
cout&lt;&lt;&quot;Enter average marks : &quot;;
cin&gt;&gt;average;
}
void disp_rollno(void)
{
cout&lt;&lt;&quot;Roll no : &quot;&lt;&lt;rollno&lt;&lt;&quot;\n&quot;;
}
float getaverage(void)
{
return average ;
}
};
class gradstudent :public student
{
char subject[len];
char working;
public :
void readit(void);
void displaysubject(void)
{
cout&lt;&lt;&quot;Subject : &quot; ;
cout.write(subject,len);
}
char workstatus(void)
{
return working;
}
};
void gradstudent::readit(void)
{
readstudent();
for(int i=0 ;i&lt;len;i++)
subject[i]=' ';
cout&lt;&lt;&quot;Enter main subject : &quot;;
gets(subject);
cout&lt;&lt;&quot;Working?(y/n) : &quot;;
cin&gt;&gt;working;
}
void main()
{
clrscr();
const int size = 5 ;
gradstudent grad[size];
int year,num_working=0,non_working=0,div1=0,total=0;
float topscore = 0,score, number,wperc,nwperc;
cout&lt;&lt;&quot;Enter year :&quot;;
cin&gt;&gt;year;
char ch=' ';
int i;
for(i=0;i&lt;10;i++)
{
cout&lt;&lt;&quot;Enter details for graduate &quot;&lt;&lt;(i+1)&lt;&lt;&quot; (maximum 10)\n&quot;;
grad[i].readit();
total++;
if(grad[i].workstatus() =='y'||(grad[i].workstatus() == 'Y'))
num_working++;
else
non_working++;
score=grad[i].getaverage() ;
if(score&gt;topscore)
{
topscore=score;
number=i;
}
if(score&gt;=60.0)
div1++;
cout&lt;&lt;&quot;Press y to see report and n to continue&quot;;
cin&gt;&gt;ch;
if(ch=='y'||ch=='Y')
break;
}
i=number;
cout&lt;&lt;&quot;\n&quot;&lt;&lt;&quot;\t\t\t\tReport of the year &quot; &lt;&lt;year&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;\t\t\t\t-----------------------\n&quot;;
cout&lt;&lt;&quot;Working graduates : &quot; &lt;&lt;num_working&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Non working graduates : &quot;&lt;&lt;non_working&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Details of topper \n&quot;;
grad[i].displayperson();
grad[i].displaysubject();
nwperc=((float)non_working/(float)total)*100;
wperc=((float)div1/(float)total)*100;
cout&lt;&lt;&quot;Average marks : &quot;&lt;&lt;grad[i].getaverage()&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;nwperc&lt;&lt;&quot;% of the graduates this year are non working and &quot;&lt;&lt;wperc&lt;&lt;&quot;% are
first divisioners\n&quot;;
getch();
}
OUTPUT
Q.6) WAP to show constructor overloading.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
class Deposit{ long int principal;
int time;
float rate;
float total_amt;
public:
Deposit();
Deposit(long p,int t,float r);
Deposit(long p,int t);
Deposit(long p,float r);
void calc_amt(void);
void display(void); };
Deposit::Deposit()
{principal=time=rate=0.0;}
Deposit::Deposit(long p,int t,float r)
{principal=p;time=t;rate=r;}
Deposit::Deposit(long p,int t)
{principal=p;time=t;rate=0.08;}
Deposit::Deposit(long p,float r)
{principal=p;time=2;rate=r;}
void Deposit::calc_amt(void)
{ total_amt=principal+(principal*time*rate)/100;
}
void Deposit::display(void)
{ cout&lt;&lt;&quot;
Principal Amount: &quot;&lt;&lt;principal;
cout&lt;&lt;&quot;\n
Time Period: &quot;&lt;&lt;time;
cout&lt;&lt;&quot;\n
Rate of interest: &quot;&lt;&lt;rate;
cout&lt;&lt;&quot;\n
Total Amount: &quot;&lt;&lt;total_amt&lt;&lt;&quot;\n&quot;;
}
void main()
{ clrscr();
Deposit D1,D2(2000,2,0.07f),D3(4000,1),D4(3000,0.12f);
D1.calc_amt(); D2.calc_amt();
D3.calc_amt(); D4.calc_amt();
cout&lt;&lt;&quot;\n Object1:\n&quot;; D1.display();
cout&lt;&lt;&quot;\n Object2:\n&quot;; D2.display();
cout&lt;&lt;&quot;\n Object3:\n&quot;; D3.display();
cout&lt;&lt;&quot;\n Object4:\n&quot;; D4.display();
getch();
}
Output
Q.7) Write a menu driven program to create the text file that
i.
ii.
iii.
iv.
v.
Count
Count
Count
Count
Count
the
the
and
the
the
no of words in a file.
no of lines a file.
display the words starting with the vowels.
words starting with uppercase alphabets.
no of spaces.
#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;string.h&gt;
#include&lt;ctype.h&gt;
void file_content()
{ ofstream fout(&quot;xyz.txt&quot;);
char ch[200];
cout&lt;&lt;&quot;\nEnter the content of the file: &quot;;
cin.getline(ch,200);
fout&lt;&lt;ch;
fout.close();
}
void vowel()
{ ifstream fin(&quot;xyz.txt&quot;);
char ch[20];
while(!fin.eof())
{
fin&gt;&gt;ch;
switch(ch[0])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
cout&lt;&lt;&quot;\nWords starting from vowels is: &quot;&lt;&lt;ch&lt;&lt;&quot;\n&quot;;
}
}
}
void word_count()
{ ifstream fin (&quot;xyz.txt&quot;);
char ch;
int count=0;
while (!fin.eof())
{
fin.get(ch);
if(ch==' ')
++count;
}
cout&lt;&lt;&quot;\nNo. of words are: &quot;&lt;&lt;++count&lt;&lt;&quot;\n&quot;;
fin.close();
}
void count_spaces()
{ ifstream fin (&quot;xyz.txt&quot;);
char ch;
int count=0;
while (!fin.eof())
{
fin.get(ch);
if(ch==' ')
++count;
}
cout&lt;&lt;&quot;\nNo. of spaces are: &quot;&lt;&lt;count&lt;&lt;&quot;\n&quot;;
fin.close();
}
void count_lines()
{ char str[100];
int count=0;
ifstream fin(&quot;xyz.txt&quot;);
while(!fin.eof())
{
fin.getline(str,100);
++count;
}
cout&lt;&lt;&quot;\nNo. of lines are: &quot;&lt;&lt;count&lt;&lt;&quot;\n&quot;;
}
void count_uppercase()
{ ifstream fin(&quot;xyz.txt&quot;);
int count=0;
char ch;
while(!fin.eof())
{
fin&gt;&gt;ch;
if(isupper(ch))
++count;
}
fin.close();
cout&lt;&lt;&quot;\nNo. of upper case alpahabets are: &quot;&lt;&lt;count&lt;&lt;&quot;\n&quot;;
}
void main()
{ clrscr();
file_content();
word_count();
count_lines();
vowel();
count_uppercase();
count_spaces();
getch();
}
OUTPUT
Q.8) Write a program to create the binary file and display them.
#include&lt;iostream.h&gt;
#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
class student
{
char name[50];
char grade;
float marks;
public: void getdata(void);
void display(void);
};
void student::getdata(void)
{ char ch;
cin.get(ch);
cout&lt;&lt;&quot;\n&quot;&lt;&lt;&quot;ENTER NAME : - &quot;;
cin&gt;&gt;name;
cout&lt;&lt;&quot;\n&quot;&lt;&lt;&quot;ENTER GRADE : - &quot;;
cin&gt;&gt;grade;
cout&lt;&lt;&quot;\n&quot;&lt;&lt;&quot;ENTER MARKS : - &quot;;
cin&gt;&gt;marks;
cout&lt;&lt;&quot;\n&quot;;
}
void student::display(void)
{cout&lt;&lt;&quot;NAME : - &quot;&lt;&lt;name&lt;&lt;&quot;\t&quot;;
cout&lt;&lt;&quot;GRADE : - &quot;&lt;&lt;grade&lt;&lt;&quot;\t&quot;;
cout&lt;&lt;&quot;MARKS : - &quot;&lt;&lt;marks&lt;&lt;&quot;\t&quot;&lt;&lt;&quot;\n&quot;;}
int main()
{
clrscr();
student arts[5];
fstream filin ;
filin.open(&quot;stu.dat&quot;,ios::in|ios::out);
if(!filin)
{
cout&lt;&lt;&quot;SORRY CAN NOT OPEN THE FILE&quot;&lt;&lt;&quot;\n&quot;;
return 1; }
cout&lt;&lt;&quot;ENTER DETAILS OF THE 2 STUDENTS : - &quot;&lt;&lt;&quot;\n&quot;;
for(int i=0;i&lt;2;i++)
{
arts[i].getdata();
filin.write((char*)&amp;arts[i],sizeof(arts[i]));
}
filin.seekg(0);
cout&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;THE CONTENTS OF THE STU.DAT IS AS SHOWN BELOW : - &quot;&lt;&lt;&quot;\n&quot;;
for(i=0;i&lt;2;i++)
{ filin.read((char*)&amp;arts[i],sizeof(arts[i]));arts[i].display();}
filin.close();
getch();
}
Output
Q.9) Write a program to search a record in a file.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;fstream.h&gt;
class stu
{
int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{
cout&lt;&lt;&quot;ROLL NUMBER&quot;;
cin&gt;&gt;rollno;
cout&lt;&lt;&quot;NAME&quot;;
cin&gt;&gt;name;
cout&lt;&lt;&quot;CLASS&quot;;
cin&gt;&gt;Class;
cout&lt;&lt;&quot;MARKS&quot;;
cin&gt;&gt;marks;
}
void putdata()
{ cout&lt;&lt;”Name-“&lt;&lt;name&lt;&lt;&quot;Roll No-&quot;&lt;&lt;rollno&lt;&lt;”Marks=”&lt;&lt;marks;
}
int getrno()
{ return rollno;
}
} s1;
void main()
{
clrscr();
int rn; char found='n';
ifstream fi(&quot;stu.dat&quot;,ios::in);
cout&lt;&lt;&quot;ENTER ROLL NUMBER TO BE SEARCHED : - &quot;;
cin&gt;&gt;rn;
while (!fi.eof())
{
fi.read((char*)&amp;s1,sizeof(s1));
if(s1.getrno()==rn)
{
s1.putdata();
found='y';
break;
}
}
if(found=='n')
cout&lt;&lt;&quot;ROLL NUMBER NOT FOUND IN THE FILE&quot;&lt;&lt;endl;
fi.close();}
Output
Q.10)WAP to append data from file.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;fstream.h&gt;
#include&lt;stdio.h&gt;
class stu
{ clrscr();
int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{ cout&lt;&lt;&quot;\n Rollno: &quot;; cin&gt;&gt;rollno;
cout&lt;&lt;&quot;\n Name: &quot;; cin&gt;&gt;name;
cout&lt;&lt;&quot;\n Class: &quot;; cin&gt;&gt;Class;
cout&lt;&lt;&quot;\n Marks: &quot;; cin&gt;&gt;marks;
if(grade&gt;=75)
grade='A';
else if(grade&gt;=60) grade='B';
else if(grade&gt;=50) grade='C';
else if(grade&gt;=40) grade='D';
else grade='F';
}
void putdata()
{ cout&lt;&lt;name&lt;&lt;&quot;\n Rollno &quot;&lt;&lt;rollno&lt;&lt;&quot;has&quot;&lt;&lt;marks
&lt;&lt;&quot;% Marks and &quot;&lt;&lt;grade&lt;&lt;&quot;Grade: &quot;&lt;&lt;endl;
}
int getrno()
{ return rollno;
}
} s1;
void main()
{ ofstream fo(&quot;\n stu.dat&quot;,ios::app);
char ans='y';
while(ans=='y')
{ s1.getdata();
fo.write((char*)&amp;s1,sizeof(s1));
cout&lt;&lt;&quot;\n Record added to file.\n&quot;;
cout&lt;&lt;&quot;\n Want to enter more records?(y/n)?: &quot;;
cin&gt;&gt;ans;
}
fo.close();}
Output
Q.11) Write a program to insert data in a file.
#include&lt;iostream.h&gt;
#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;stdio.h&gt;
class stu
{
int rollno;
char name[25];
float marks;
public:
void getdata()
{ cout&lt;&lt;&quot;ROLL NUMBER : - &quot;;
cin&gt;&gt;rollno;
cout&lt;&lt;&quot;NAME : - &quot;;
gets(name);
cout&lt;&lt;&quot;MARKS : - &quot;;
cin&gt;&gt;marks;
}
void putdata()
{ cout&lt;&lt;&quot;\nName-&quot;&lt;&lt;name&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Roll No- &quot;&lt;&lt;name&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Marks= &quot;&lt;&lt;marks&lt;&lt;&quot;\n&quot;;
}
int getrno()
{return rollno;
}
};
stu s1,stud;
void main()
{
clrscr();
ofstream f(&quot;stu.dat&quot;,ios::out|ios::app);
ifstream fi(&quot;stu.dat&quot;,ios::in|ios::app);
ofstream fo(&quot;temp.dat&quot;,ios::out|ios::app);
char last='y';
cout&lt;&lt;&quot;enter the details of students whose record is to be inserted\n&quot;;
s1.getdata();
while(!fi.eof())
{
fi.read((char*)&amp;stud,sizeof(stud));
if(s1.getrno()&lt;=stud.getrno())
{
fo.write((char*)&amp;s1,sizeof(s1));
last='n';
break;
}
else
fo.write((char*)&amp;stud,sizeof(stud));
}
if(last=='y')
fo.write((char*)&amp;s1,sizeof(s1));
else if(!fi.eof())
{
while(!fi.eof())
{ fi.read((char*)&amp;stud,sizeof(stud));
fo.write((char*)&amp;stud,sizeof(stud));
}
}
fi.close();
fo.close();
remove(&quot;stu.dat&quot;);
rename(&quot;temp.dat&quot;,&quot;stu.dat&quot;);
ifstream ko;
ko.open(&quot;stu.dat&quot;,ios::in);
cout&lt;&lt;&quot;file now contains&quot;&lt;&lt;endl;
while(!ko.eof())
{
ko.read((char*)&amp;stud,sizeof(stud));
if(ko.eof())
break;
stud.putdata();
}
ko.close();
getch();
}
Output
Q.12) Write a program to delete data from a file.
#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;stdio.h&gt;
class stu
{
int rollno;
char name[25];
char cls;
float marks;
public:
void getdata()
{
cout&lt;&lt;&quot;ROLL NUMBER : - &quot;;
cin&gt;&gt;rollno;
cout&lt;&lt;&quot;NAME : - &quot;;
gets(name);
cout&lt;&lt;&quot;CLASS : - &quot;;
cin&gt;&gt;cls;
cout&lt;&lt;&quot;MARKS : - &quot;;
cin&gt;&gt;marks;
}
void putdata()
{
cout&lt;&lt;&quot;\nName- &quot;&lt;&lt;name&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Roll No- &quot;&lt;&lt;rollno&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Marks=&quot;&lt;&lt;marks&lt;&lt;&quot;\n&quot;;
}
int getrno()
{ return rollno;
}
}s1,stud;
void main()
{
clrscr();
ifstream fi(&quot;STU.DAT&quot;,ios::in);
ofstream fo(&quot;TEMP.DAT&quot;,ios::out);
int rno;
char found='f',confirm='n';
cout&lt;&lt;&quot;ENTER ROLL NUMBER OF STUDENT WHOSE RECORD IS TO BE
DELETED:-&quot;;
cout&lt;&lt;&quot;\n&quot;;
cin&gt;&gt;rno;
while(!fi.eof())
{
fi.read((char*)&amp;stud,sizeof(stud));
if(s1.getrno()==rno)
{
s1.putdata();
found='t';
fo.write((char*)&amp;s1,sizeof(s1));
}
else
fo.write((char*)&amp;s1,sizeof(s1));
}
if(found=='n')
cout&lt;&lt;&quot;SORRY RECOD NOT FOUND&quot;&lt;&lt;endl;
fi.close();
fo.close();
remove(&quot;STU.DAT&quot;);
rename(&quot;TEMP.DAT&quot;,&quot;STU.DAT&quot;);
fi.open(&quot;STU.DAT&quot;,ios::in);
cout&lt;&lt;&quot;FILE NOW CONTAINS : - &quot;&lt;&lt;&quot;\n&quot;&lt;&lt;endl;
while(!fi.eof())
{
fi.read((char*)&amp;stud,sizeof(stud));
if(fi.eof())
break;
stud.putdata();
}
fi.close();
getch();
}
Output
Q.13) Write a program to modify data in a file.
#include&lt;iostream.h&gt;
#include&lt;fstream.h&gt;
#include&lt;stdio.h&gt;
#include&lt;string.h&gt;
#include&lt;conio.h&gt;
class stu
{
int rollno;
char name[25];
char Class[4];
float marks;
public:
void getdata()
{ cout&lt;&lt;&quot;Enter Name-&quot;;
cin&gt;&gt;name;
cout&lt;&lt;&quot;Enter Roll No-&quot;;
cin&gt;&gt;rollno;
cout&lt;&lt;&quot;Enter Marks &quot;;cin&gt;&gt;marks;
}
int getrno()
{ return rollno ;
}
void modify() ;
};
s1,stud ;
void stu::modify()
{
cout&lt;&lt;&quot;ROLL NUMBER : - &quot;&lt;&lt;rollno&lt;&lt;endl;
cout&lt;&lt;&quot;NAME : - &quot;&lt;&lt;name&lt;&lt;&quot;\t&quot;;
cout&lt;&lt;&quot;CLASS : - &quot;&lt;&lt;Class&lt;&lt;&quot;\t&quot;;
cout&lt;&lt;&quot;MARKS : - &quot;&lt;&lt;marks&lt;&lt;endl;
cout&lt;&lt;&quot;ENTER NEW DETAILS : - &quot;&lt;&lt;endl;
char nm[20]=&quot; &quot;,cl[4]=&quot; &quot;;
float mks ;
cout&lt;&lt;”Enter Name-&quot;;
cin&gt;&gt;nm;
cout&lt;&lt;&quot;Enter Roll No- &quot;;
cin&gt;&gt;rn;
cout&lt;&lt;&quot;Enter Marks= &quot;;
cin&gt;&gt;mks;
if(strcmp(nm,&quot;.&quot;)!= 0)
strcpy(name, nm);
if(strcmp(cl,&quot;.&quot;)!=0)
strcpy(Class,cl);
if(mks!= -1)
{
marks=mks;
if(marks &gt;= 75)
}
}
void main()
{
clrscr();
fstream fio(&quot;stu.dat&quot;, ios::in|ios::out|ios::binary);
int rno; long pos; char found='f';
cout&lt;&lt;&quot;ENTER ROLL NUMBER OF THE STUDENT WHOSE RECORD IS TO
MODIFY:-&quot;;
cout&lt;&lt;&quot;n&quot;;
cin&gt;&gt;rno;
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char*) &amp;s1, sizeof(s1));
if (s1.getrno()==rno)
{
s1.modify();
fio.write((char*) &amp;s1, sizeof(s1));
found='t';
break;
}
}
if(found=='f')
cout&lt;&lt;&quot;SORRY RECORD NOT FOUND&quot;&lt;&lt;&quot;\n&quot;;
fio.seekg(0);
fio.close();
getch();
}
Output
Q.14) Create a menu driven program to show searching operation in 1D array using (I)Lsearch (II) Bsearch
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
int Lsearch(int[],int,int);
int Bsearch(int[],int,int);
void main()
{clrscr();
int n,AR[50],i,item,index;
int ch=0;
cout&lt;&lt;&quot;\n Menu: &quot;;
cout&lt;&lt;&quot;\n 1.Linear Search&quot;;
cout&lt;&lt;&quot;\n 2.Binary Search&quot;;
cout&lt;&lt;&quot;\n Enter Choice(1/2): &quot;;
cin&gt;&gt;ch;
cout&lt;&lt;&quot;\n Enter the Desired array size(Max.50): &quot;;
cin&gt;&gt;n;
cout&lt;&lt;&quot;\n Enter Array elements &quot;;
for(i=0;i&lt;n;i++)
{ cin&gt;&gt;AR[i]; }
cout&lt;&lt;&quot;\n Enter Elements to be searched for: &quot;;
cin&gt;&gt;item;
switch(ch)
{ case 1:{ index=Lsearch(AR,n,item);
if(index==-1)
cout&lt;&lt;&quot;\n Sorry!! Given element could not be found.&quot;;
else
cout&lt;&lt;&quot;\n Element found at index: &quot;&lt;&lt;index;
cout&lt;&lt;&quot;\n Position: &quot;&lt;&lt;index+1&lt;&lt;endl;
break;}
case 2:{ index=Bsearch(AR,n,item);
if(index==-1)
cout&lt;&lt;&quot;\n Sorry!! Given element could not be found.&quot;;
else
cout&lt;&lt;&quot;\n Element found at index: &quot;&lt;&lt;index;
cout&lt;&lt;&quot;\n Position: &quot;&lt;&lt;index+1&lt;&lt;endl;
break;}
default:{cout&lt;&lt;&quot;\n Wrong Choice&quot;;}
break;
}
getch();
}
int Lsearch(int arr[50],int size,int it)
{
for(int j=0;j&lt;size;j++)
{if(arr[j]==it)
return j;
}
return-1;
};
int Bsearch(int arr[50],int size,int it)
{ int beg,last,mid;
beg=0;
last=size-1;
while(beg&lt;=last)
{ mid=(beg+last)/2;
if(it==arr[mid])
return mid;
else if(it&gt;arr[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
};
Output
Q.15)WAP menu driven program to insert &amp; delete an element in a
sorted array.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;process.h&gt;
int FindPos(int[],int,int);
int Lsearch(int[],int,int);
int Insertion(int[],int);
int Deletion(int[],int);
void main()
{clrscr();
int AR[50],N,choice;
cout&lt;&lt;&quot;\n Enter size of array(Max.50): &quot;;
cin&gt;&gt;N;
cout&lt;&lt;&quot;\n Enter array elements is ascending order:\n&quot;;
for(int i=0;i&lt;N;i++)
{ cin&gt;&gt;AR[i];}
cout&lt;&lt;&quot;\n Menu:&quot;;
cout&lt;&lt;&quot;\n 1.Insertion&quot;;
cout&lt;&lt;&quot;\n 2.Deletion&quot;;
cout&lt;&lt;&quot;\n Enter Choice(1/2): &quot;;
cin&gt;&gt;choice;
switch(choice)
{case 1: Insertion(AR,N);
break;
case 2: Deletion(AR,N);
break;
default:cout&lt;&lt;&quot;\n !!Please enter either 1 or 2&quot;;
break;
}
getch();
}
int Insertion(int ar[],int y)
{char ch='y';
int item,index,i;
while(ch=='y'||ch=='Y')
{ cout&lt;&lt;&quot;\n Enter element to be inserted: &quot;;
cin&gt;&gt;item;
if(y==50)
{ cout&lt;&lt;&quot;\n Overflow&quot;;
exit(1);
}
index=FindPos(ar,y,item);
for(i=y;i&gt;index;i--)
{ ar[i]=ar[i-1];}
ar[index]=item;
y=y+1;
cout&lt;&lt;&quot;\n Want to insert more element(Y/N)?: &quot;;
cin&gt;&gt;ch;
}
cout&lt;&lt;&quot;\n Array after insertion is: \n&quot;;
for(i=0;i&lt;y;i++)
{ cout&lt;&lt;ar[i]&lt;&lt;&quot; &quot;;}
}
int FindPos(int AR[],int size,int item)
{int pos;
if(item&lt;AR[0]) pos=0;
else
{ for(int i=0;i&lt;size-1;i++)
{ if(AR[i]&lt;=item&amp;&amp;item&lt;AR[i+1])
{ pos=i+1;
break;
} }
if(i==size-1) pos=size;
}
return pos;}
int Deletion(int AR[],int N)
{ char ch='y';
int item,index,i;
while(ch=='y'||'Y')
{ cout&lt;&lt;&quot;\n Enter element to be deleted: &quot;;
cin&gt;&gt;item;
if(N==0)
{ cout&lt;&lt;&quot;Underflow!!&quot;;
exit(1);
}
index=Lsearch(AR,N,item);
if(index!=-1)
AR[index]=0;
else
cout&lt;&lt;&quot;\n !!Sorry no such element is found&quot;;
for(i=index;i&lt;N;i++)
{ AR[i]=AR[i+1];}
N=N-1;
cout&lt;&lt;&quot;\n Want to delete more(Y/N)?&quot;;
cin&gt;&gt;ch;
cout&lt;&lt;&quot;\n Array after deletion is: \n&quot;;
for(i=0;i&lt;N;i++)
cout&lt;&lt;AR[i]&lt;&lt;&quot; &quot;;
break;
}}
int Lsearch(int AR[],int size,int item)
{ int i;
for(i=0;i&lt;size;i++)
}
{ if(AR[i]==item)
return i;
}
return -1;
Output
Q.16) Write a program to merge two 1-D arrays into third array.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
void Merge(int[],int,int[],int,int[]);
void main()
{ clrscr();
int A[20],B[20],C[40],m,n,mn=0;
cout&lt;&lt;&quot;Enter no of element in first array in acs order&quot;;
cin&gt;&gt;m;
for(int i=0;i&lt;m;i++)
cin&gt;&gt;A[i];
cout&lt;&lt;&quot;Enter no of element in second array in desc order&quot;;
cin&gt;&gt;n;
for(i=0;i&lt;n;i++)
cin&gt;&gt;B[i];
mn=m+n;
Merge(A,m,B,n,C);
cout&lt;&lt;&quot;Resultant array is&quot;;
for(i=0;i&lt;mn;i++)
cout&lt;&lt;C[i];
getch();
}
void Merge(int A[],int m,int B[],int n,int C[])
{ int a,b,c;
for(a=0,b=n-1,c=0;a&lt;m&amp;&amp;b&gt;=0;)
{ if(A[a]&lt;=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
}
Output
Q.17) Write a menu driven program to sort elements using selection
sort and bubble sort.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
void selsort(int[],int);
void bubblesort(int[],int);
void main()
{ clrscr();
int AR[50],ITEM,N,index,n;
cout&lt;&lt;&quot;How many element you want to insert in array?(Max.50): &quot;;
cin&gt;&gt;N;
cout&lt;&lt;&quot;Enter element of array: &quot;;
for(int i=0;i&lt;N;i++)
cin&gt;&gt;AR[i];
cout&lt;&lt;&quot;Menu\n&quot;&lt;&lt;&quot;1.Selection Sort\n&quot;&lt;&lt;&quot;2.Bubble Sort\n&quot;&lt;&lt;&quot;Enter Choice: &quot;;
cin&gt;&gt;n;
switch(n)
{
case 1: selsort(AR,N);
break;
case 2: bubblesort(AR,N);
break;
default:cout&lt;&lt;&quot;Enter Valid Choice&quot;;
break;
}
cout&lt;&lt;&quot;\nThe Sorted Array is shown below:\n&quot;;
for(i=0;i&lt;N;i++)
cout&lt;&lt;AR[i]&lt;&lt;&quot; &quot;;
cout&lt;&lt;endl;
getch();
}
void selsort(int AR[], int size)
{ int small,pos,tmp;
for(int i=0;i&lt;size;i++)
{
small=AR[i];
for(int j=i+1;j&lt;size;j++)
{
if(AR[j]&lt;small)
{ small=AR[j];
pos=j;
}
tmp=AR[i];
AR[i]=AR[pos];
AR[pos]=tmp;
}
cout&lt;&lt;&quot;\nArray after pass:&quot;&lt;&lt;i+1&lt;&lt;&quot; is-\n&quot;;
for(int k=0;k&lt;size;k++)
}
cout&lt;&lt;AR[k]&lt;&lt;&quot; &quot;;
}
void bubblesort(int AR[],int size)
{ int tmp, ctr=0;
for(int i=0;i&lt;size;i++)
{ for(int j=0;j&lt;(size-1)-i;j++)
{ if(AR[j]&gt;AR[j+1])
{ tmp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=tmp;
}
}
cout&lt;&lt;&quot;Array after iteration:&quot;&lt;&lt;++ctr&lt;&lt;&quot; is:\n&quot;;
for(int k=0;k&lt;size;k++)
cout&lt;&lt;AR[k]&lt;&lt;&quot; &quot;;
cout&lt;&lt;endl;
}
}
Output
Q.18) Write a program to convert 1D array into 2D array.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
void main()
{
clrscr();
int ar [20],arr[20][20];
int i,j,size;
cout&lt;&lt;&quot;Enter size of array\n&quot;;
cin&gt;&gt;size;
}
cout&lt;&lt;&quot;Enter 1d-array\n&quot;;
for(i=0;i&lt;size;i++)
{
cin&gt;&gt;ar[i];
}
cout&lt;&lt;&quot;Your array is\n&quot;;
for(i=0;i&lt;size;i++)
{
cout&lt;&lt;ar[i];
cout&lt;&lt;&quot;\t&quot;;
}
cout&lt;&lt;&quot;\n&quot;;
for(i=0;i&lt;size;i++)
for(j=0;j&lt;size;j++)
arr[i][j]=ar[j];
cout&lt;&lt;&quot;Your required 2d array is\n&quot;;
for(i=0;i&lt;size;i++)
{
for(j=0;j&lt;size;j++)
{
cout&lt;&lt;arr[i][j]&lt;&lt;&quot;\t&quot;;
}
cout&lt;&lt;&quot;\n&quot;;
}
getch();
Output
Q.19) Write a program for pushing and popping in a stack implemented
as linked list.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;process.h&gt;
struct node
{
int info;
node*next;
}*top,*newptr,*save,*ptr;
node*create_new_node(int);
void push(node*);
void display(node*);
void pop();
void main()
{
clrscr();
top=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout&lt;&lt;&quot;\n Enter the information&quot;&lt;&lt;&quot;\t&quot;;
cin&gt;&gt;inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout&lt;&lt;&quot;\n cannot create new node!!!&quot;;
exit(1);
}
push(newptr);
cout&lt;&lt;&quot;\n press Y to enter more nodes, n to exit&quot;&lt;&lt;&quot; &quot;;
cin&gt;&gt;ch;
}
clrscr();
do
{
cout&lt;&lt;&quot;\n stack is now:\n&quot;;
display(top);
getch();
cout&lt;&lt;&quot;\n want to pop an element?(y/n)&quot;&lt;&lt;&quot; &quot;;
cin&gt;&gt;ch;
if(ch=='y'||ch=='Y')
pop();
}while(ch=='y'||ch=='Y');
}
node*create_new_node(int n)
{
ptr=new node;
ptr-&gt;info=n;
ptr-&gt;next=NULL;
return ptr;
}
void push(node*np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np-&gt;next=save;
}
}
void pop()
{
if(top==NULL)
cout&lt;&lt;&quot;\n underflow&quot;;
else
{
ptr=top;
top=top-&gt;next;
delete ptr;
}}
void display(node*np)
{
while(np!=NULL)
{
cout&lt;&lt;np-&gt;info&lt;&lt;&quot;-&gt;&quot;;
np=np-&gt;next;
}
cout&lt;&lt;&quot;!!!\n&quot;;
}
Output
Q.20) Write a program to show insertion and deletion from linked
queue.
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
struct node
{
int data;
node*next;
};
node*add(node*rear,int val);
node*delet(node*front);
void show(node*front);
void main()
{
node*front,*rear;
int val,choice;
front=rear=NULL;
clrscr();
do{
cout&lt;&lt;&quot;\nMenu&quot;;
cout&lt;&lt;&quot;\n 1. ADD&quot;;
cout&lt;&lt;&quot;\n 2. Delete&quot;;
cout&lt;&lt;&quot;\n 3. Show&quot;;
cout&lt;&lt;&quot;\n 4. Quit&quot;;
cout&lt;&lt;&quot;\n Enter your choice&quot;&lt;&lt;&quot; &quot;;
cin&gt;&gt;choice;
switch(choice)
{
case 1:cout&lt;&lt;&quot;\n Enter the vlaue to be added&quot;&lt;&lt;&quot; &quot;;
cin&gt;&gt;val;
rear=add(rear,val);
if(front==NULL)
front=rear;
break;
case 2:front=delet(front);
if(front==NULL)
rear=front;
break;
}
}
case 3:show(front);
break;
}
while(choice!=4);
node*add(node*rear,int val)
{
node*x;
x=new node;
x-&gt;data=val;
x-&gt;next=NULL;
if(rear!=NULL)
{
rear-&gt;next=x;
}
rear=x;
return(rear);
}
node*delet(node*front)
{
node*x;
int val;
if(front==NULL)
{
cout&lt;&lt;&quot;\nQueue is empty&quot;;
val=-999;
}
else
{
x=front;
front=front-&gt;next;
val=x-&gt;data;
delete x;
}
cout&lt;&lt;val;
return(front);
}
void show(node*front)
{
node*ptr;
ptr=front;
cout&lt;&lt;&quot;\nThe queue is:&quot;;
while(ptr!=NULL)
{
cout&lt;&lt;ptr-&gt;data&lt;&lt;&quot; &quot;;
ptr=ptr-&gt;next;
}
}
Output
21.) Write SQL command for (a) for (f) and write the output for (g)
on the basis of tables Student.
Table: Student
No.
1.
2.
3.
4.
5.
6.
7.
8.
Name
Pankaj
Shalini
Sanjay
Sudha
Rakesh
Shakeel
Surya
Sikha
Age
24
21
22
25
22
30
34
23
Department
Computer
History
Hindi
History
Hindi
History
Computer
Hindi
Dateofadm
10/01/97
24/03/98
12/12/96
01/07/99
05/09/97
27/06/98
25/02/97
31/07/97
Fee
120
200
300
400
250
300
210
200
Sex
M
F
M
F
M
M
M
F
a) To show all information about all students of the History
Department.
b) To list the names of female students who are of Hindi
department.
c) To list name of all students with their name in ascending order.
d) To display student’s name, fee, age for male students only.
e) To count the number of student with Age&lt;23.
f) To isnert a new row in the STUDENT table with the following
data :
9, “Zaheer”, 36 , “Computer”, {12/03/97}, 230, ‘M’
g) Give the output of following SQL statements :
(i) Select COUNT (distinct departments) from STUDENT ;
(ii) Select MAX (Age) from STUDENT where Sex = ”F” ;
(iii) Select AVG (fee) form STUDENT where Dateofadm
&lt;{01/01/98} ;
(iv) Select SUM (Fee) from STUDENT where Dateofadm
&lt;{01/01/98} ;
Answers)
(a) SELECT * FROM Student
WHERE Depatment = “History”;
(b) SELECT Name FROM Student
WHERE sex =”F” and
Depatment = “History”;
(c) SELECT name FROM Student
ORDER BY Dateofadm;
(d) SELECT Name, Fee, Age
FROM Student
WHERE sex = “M”;
(e) SELECT COUNT (*) FROM Student
WHERE Age&gt;23 ;
(f) INSERT INTO Student
VALUES (9, “Zaheer”, 36 , “Computer”, {12/03/97}, 230, ‘M’) ;
(i) 3
(ii)25
(iii)236
(iv) 1080
22.) Write SQL commands for (a) to (f) and Write to outputs for
(g) on the basis of table HOSPITAL.
Table: HOSPITAL
No.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Name
Sandeep
Ravina
Karan
Tarun
Zubin
Ketaki
Ankita
Zareen
Kush
Shalya
Age
62
22
32
12
30
16
29
45
19
23
Department
Surgery
ENT
Orthopedic
Surgery
ENT
ENT
Cardiology
Gynecology
Cariology
Nuclaer Medicine
Dateofadm
21/01/98
12/12/97
19/02/98
11/01/98
12/01/98
24/02/98
20/02/98
22/05/98
13/01/98
21/02/98
Charges
300
250
200
300
250
250
800
300
800
400
Sex
M
F
M
M
M
F
F
F
M
F
a) To select all information about all patient of the CARIOLOGY
Department.
b) To list the names of female patients who are in ENT
Department.
c) The list names of all patients with date date of admission in
ascending order.
d) To display Patient’s name, Charges, Age for only male patients.
e) To count the number of patients with Age&lt;30.
f) To insert a new row in the HOSpital table with the following
date ;
11, “Mustafa” , 37, “ENT”, {25/02/98}, 250, “M”
g) Give the output of the following SQL statements :
(i) Select COUNT (distict department) from HOSPITAL ;
(ii) Select MAX( Age) from HOSPITAL where Sex =”M”
(iii)Select AVG (Charges) form HOSPITAL where Sex =”F”
(iv)Select SUM ( Charges) from HoSPITAL where Dateofadm&lt;
{10/08/98}
Answers)
(a) SELECT * FROM Hospital
WHERE Depatment = “Cardiology”;
(b) SELECT Name FROM Hospital
WHERE sex =”F” and
Depatment = “Orthopedic”;
(c) SELECT name FROM Hospital
ORDER BY Dateofadm;
(d) SELECT Name, Charges, Age
FROM Hospital
WHERE sex = “M”;
(e) SELECT COUNT (*) FROM Hospital
WHERE Age&lt;30 ;
(f) INSERT INTO Hospital
VALUES (11, “Mustafa”, 37 , “ENT”, {25/02/97}, 250, “M”) ;
(i) 6
(ii)65
(iii)400
(iv) 3850
23.) Write SQL commands for (a) to (f) and write output for (g) on
the basis of Teacher relation given below:
Table: Teacher
No.
1.
2.
3.
4.
5.
6.
7.
8.
Name
Jugal
Sharmila
Sandeep
Sangeeta
Rakesh
Shayam
Shiv Ov
Shalakha
Age
34
31
32
35
42
50
44
33
Department
Computer
History
Maths
History
Maths
History
Computer
Maths
Date of join
10/01/97
24/03/98
12/12/96
01/07/99
05/09/97
27/06/98
25/02/97
31/07/97
Salary
12000
20000
30000
40000
25000
30000
21000
20000
Sex
M
F
M
F
M
M
M
F
a) To select all information about all Teachers of the history
department.
b) To list the names of female Teachers who are in Hindi Department.
c) The list names of all Teachers with date of joining in ascending
order.
d) To display students’ Name, Fees, Age for only male teachers.
e) To count the number of patients with Age&lt;23.
f) To insert a new row in the HOSPITAL table with the following date
;
11, “Raja” , 26, “Computer”, {13/05/95}, 2300, “M”.
g) Give the output of the following SQL statements :
(i) Select COUNT (distict department) from TEACHER ;
(ii) Select MAX( Age) from TEACHERS where Sex =”F”
(iii)Select AVG (Salary) form TEACHERS where
Dateofadm&lt; {10/08/98}
(iv)Select SUM (Salary) from TEACHERS where
Dateofadm&lt; {10/08/98
Answers)
(a) SELECT * FROM Teacher
WHERE Department = “History”;
(b) SELECT Name FROM Hospital
WHERE sex =”F” and
Department = “Hindi”;
(c) SELECT name FROM Teacher
ORDER BY Dateofadm;
(d) SELECT Name, Salary, Age
FROM Teacher
WHERE sex = “M” AND Age&gt;23;
(e) SELECT COUNT(*) FROM Hospital
WHERE Age&lt;23 ;
(f) INSERT INTO Teacher
VALUES (9, “Raja”, 26 , “Computer”, {13/02/95}, 2300, “M”) ;
(i) 3
(ii)35
(iii)23600
(iv) 2300
24.) Write SQL command for (a) for (f) and write the output for (g)
on the basis of tables FURNITURE and ARRIVALS.
Table: FURITURE
NO.
1
2
3
4
5
6
7
8
9
10
ITEMNAME
White Lotus
Pink Feather
Dolphin
Decent
Comfort zone
Donald
Royal Finish
Royal Tiger
Econo sitting
EatingParadise
TYPE
Double Bed
Baby cot
Baby cot
OfficeTable
Double Bed
Baby cot
OfficeTable
Sofa
Sofa
Dining Table
DATEOFSTOCK
23/02/02
20/01/02
19/02/02
01/01/02
12/01/02
24/01/02
20/02/02
22/02/02
13/13/01
19/02/02
Table: ARRIVALS
PRICE
30000
7000
9500
25000
25000
6500
18000
31000
90005
110005
DISCOUNT
25
20
20
30
25
15
30
30
25
25
NO.
11
12
13
ITEMNAME
Wood Comfort
Old Fox
Mickey
TYPE
Double Bed
Sofa
Baby cot
DATEOFSTOCK
23/03/03
20/02/03
21/02/03
PRICE
25000
17000
7500
DISCOUNT
25
20
15
a) To show all info about the Baby cots from the FURNITURE
table.
b) To list the ITEMNAME which are priced at more than 15000
from the FURNITURE table.
c) To list ITEMNAME and TYPE of those items, in which
DATEOFSTOCK is before 22/01/02 from the FURNITURE table
in descending order of ITEMNAME.
d) To display ITEMNAME and DATEOFSTOCK of those items, in
which the DISCOUNT percentage is more than 25 from
FURNITURE table.
e) To count the number of items, whose TYPE is “Sofa” from the
FURNITURE table.
f) To insert a new row in the ARRIVALS table with the following
data:
14, ”Velvet Touch”, Double Bed, (25/03/03), 25000, 30
g) Give the output of the following statement:
(i)
Select COUNT(distinct TYPE) from FURNITURE;
(ii) Select MAX(DISCOUNT) from FURNITURE, ARRIVALS;
(iii) Select AVG(DISCOUNT) from FURNITURE where
TYPE=”Baby cot”.
(iv) Select SUM(PRICE) from FURNITURE where
DATAOFSTOCK &lt; (12/02/02).
Answer)
a) Select* From FURNITURE Where TYPE=”Baby cot”;
b) Select ITEMNAME From FURNITURE Where PRICE &gt; 15000;
c) Select ITEMNAME, TYPE From FURINTURE;
Where DATEOFSTOCK &lt; (22/01/02) Order by ITEMNAME;
d) Select ITEMNAME, DATEOFSTOCK From FURINTURE Where DISCOUNT &gt;
25;
e) Select Count(*) From FURNITURE Where TYPE=”Sofa”;
f) Insert Into ARRIVALS Values(14, ”Velvet Touch”, Double Bed, (25/03/03),
25000, 30)
g) (i) 5
(ii)30
(iii)18.33
(iv)66500.
25.) Write the SQL command for (a) to (f) and write the output for
(g) on the basis of tables INTERIORS and NEWONES.
Table: INTERIORS
NO.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
ITEMNAME
Red rose
Soft touch
Jerry’s home
Rough wood
Comfort zone
Jerry look
Lion king
Royal tiger
Park sitting
Dine Paradise
TYPE
Double Bed
Baby cot
Baby cot
Office Table
Double Bed
Baby cot
Office Table
Sofa
Sofa
Dining Table
DATEOFSTOCK
23/02/02
20/01/02
19/02/02
01/01/02
12/01/02
24/01/02
20/02/02
22/02/02
13/13/01
19/02/02
PRICE
32000
9000
8500
20000
15000
7000
16000
30000
9000
11000
DISCOUNT
15
10
10
20
20
19
20
25
15
15
Price
20000
DISCOUNT
20
Table: NEWONES
NO.
11.
ITEMNAME
White wood
TYPE
Double Bed
DATEOFSTOCK
23/03.03
12.
13.
James 007
Tom look
Sofa
Baby cot
20/02/03
21/02/03
15000
7000
15
10
a) To show all info about the Sofas from the INTERIORS table.
b) To list the ITEMNAME which are priced at more than 10000
from the INTERIORS table.
c) To list ITEMNAME and TYPE of those items, in which
DATEOFSTOCK is before 22/01/02 from the INTERIORS table
in decreasing order of ITEMNAME.
d) To display ITEMNAME and DATEOFSTOCK of those items, in
which the discount percentage is more than 15 from the
INTERIORS table.
e) To count the no of items whose type is “Double Bed” from the
table INTERIORS.
f) To insert a new tow in the NEWONES table with the following
data:
14,”True Indian”, “Office Table”, (28/03/03), 15000, 20.
g) Give the output of the following statement:
(i)
Select COUNT(distinct TYPE) from INTERIORS;
(ii) Select AVG(DISCOUNT) from INTERIORS, where
DATEOFSTOCK&lt;(12/02/02).
(iii) Select SUM(Price) form INTERIORS where
DATEOFSOCK&lt;(12/02/02).
Answer)
a) Select* From INTERIORS Where PRICE&gt;10000;
b) Select ITEMNAME, TYPE From INTERIORS
c) Select ITEMNAME, TYPE From INTERIORS
Where DATEOFSTOCK &lt; (22/01/02) Order by ITEMNAME;
d) Select ITEMNAME, DATEOFSTOCK From INTERIORS Where
DICSOUNT&gt;15;
e) Select Count(*) From INTERIORS Where TYPE=”Double Bed”;
f) Insert Into NEWONES Values
(14,”True Indian”, “Office Table”, (28/03/03), 15000, 20);
e) (i)5
(ii)13
(iii)43000
BIBLIOGRAPHY
1. COMPUTER SCIENCE text book
By: SUMITA ARORA
2. MRS. NIDHI YAGNIK
(P.G.T. Comp. Sc.)
3. PIONEER question bank in C++.
4. K.V.S. Study Material for Computer Science
Download