CS 240 Stage 3 Memory Hierarchy Abstractions for Practical ...
List of Practical Cs With Solution
-
Upload
arjun-kala -
Category
Documents
-
view
221 -
download
0
Transcript of List of Practical Cs With Solution
-
8/3/2019 List of Practical Cs With Solution
1/57
LIST OF PRACTICALS
CLASS XII
SUBJECT : COMPUTER SCIENCE
Q1. Define a class PhoneBill in C++ with the following descriptions.
Private members:
CustomerName of type character array
PhoneNumber of type long
No_of_units of type intRent of type int
Amount of type float.
calculate( ) This member function should calculate the value of amount asRent+ cost for the units.
Where cost for the units can be calculated according to the following conditions.
No_of_units Cost
First 50 calls FreeNext 100 calls 0.80 @ unit
Next 200 calls 1.00 @ unit
Remaining calls 1.20 @ unit
Public members:* A constructor to assign initial values of CustomerName as Raju, PhoneNumber as
259461, No_of_units as 50, Rent as 100, Amount as 100.* A function accept( ) which allows user to enter CustomerName, PhoneNumber,
No_of_units and Rent and should call function calculate( ).
* A function Display( ) to display the values of all the data members on the screen.
Solution:
#include
#include
#include #include
class phonebill{private:
char CustomerName[20];
long PhoneNumber;
int No_of_units ;int Rent;
float Amount;
void calculate();public:
phonebill()
{ PhoneNumber=259461;No_of_units=50;
strcpy(CustomerName,"Raju");
Rent=100;Amount=100;
}
void accept();
void display();};
void phonebill:: calculate()
mailto:1.00@unitmailto:1.00@unit -
8/3/2019 List of Practical Cs With Solution
2/57
{
if(No_of_units=51 && No_of_units=151 && No_of_units
-
8/3/2019 List of Practical Cs With Solution
3/57
Q2. Define a class Tour in C++ with the description given below :
Private Members :
TCode of type string Noofadults of type integer
Noofkids of type integer
Kilometres of type integerTotalfare of type float
Public Members :
A constructor to assign initial values as follows :TCode with the word NULL
NoofAdults as 0
NoofKids as 0Kilometres as 0
TotalFare as 0
A function AssignFare ( ) which calculates and assigns the value of the data member TotalFare as follows :
Foreach Adult
Fare(Rs) For Kilometres
500 >=1000
300 =500
200
-
8/3/2019 List of Practical Cs With Solution
4/57
NoofKids=0;
Kilometres= 0;
TotalFare=0.0f;
}void Tour::EnterTour(void)
{
coutTCode;
coutNoofAdults;coutNoofKids;
coutKilometres;
AssignFare();
}
void Tour::AssignFare(void){
float fare=0.0f;
if(Kilometres>1000)
{fare = NoofAdults*500+NoofKids*250;
}else if(Kilometres>=500)
{
fare = NoofAdults*300+NoofKids*150;
}
else
fare = NoofAdults*200+NoofKids*100;Totalfare=fare;
}void Tour::ShowTour(void){
cout
-
8/3/2019 List of Practical Cs With Solution
5/57
Enter distance 850
Travel List is as followsTravel Code: 101
Number Of Adults: 2
Number of kids: 3Kilometres: 850
Total Fare:1050
Q3. Define a classELECTIONwith the following specifications :
Private members :
Data : candidate_name , party , vote_receivedPublic members :
Functions : INPUT( ) to input data
DISPLAY( ) to display the details of the winner
WINNER( ) To return the details of the winner through the objectafter comparing the votes received by three candidates .
Write a suitable main ( ) function to input data in 3 objects ofELECTIONtype and display the details of th
winning candidate.
Solution:#include
#include#include
class ELECTION
{
private:char cname[20];
char party[30];
long voterecevied;public:
void input(){cout
-
8/3/2019 List of Practical Cs With Solution
6/57
return(X);
}
else if ((Y.voterecevied>Z.voterecevied) && (Y.voterecevied>X.voterecevied))
{return(Y);
}
elsereturn(Z);
}
};void main()
{
ELECTION A,B,C,W;clrscr();
A.input();
B.input();
C.input();W=W.WINNER(A,B,C);
W.display();
getch();
}Output:
Enter candidates name AbhinavEnter party of the candidate name BJP
Enter the amount of vote recevied by the candidate45861
Enter candidates name Rahul
Enter party of the candidate name RJD
Enter the amount of vote recevied by the candidate56445
Enter candidates name RajeevEnter party of the candidate name CONGRESSEnter the amount of vote recevied by the candidate46466
Winning candidate
Candidate name RahulParty of the candidate RJD
The amount of vote recevied by the candidate56466
Q4. Suppose A,B,C are arrays of size m, n, m+n respectively. Array A is stored in ascending order an
array B is in descending order. Write a program using function to receive 3 arrays and their sizes to store th
elements of A and B into C in descending order.Solution:
//program to merge two arrays in a single array
//Array A is in Ascending Order & Array B is in Descending Order//Array C must be in Descending Order
#include
#include
void main(){
int m,n,k;
int a[50],b[50],c[100];
-
8/3/2019 List of Practical Cs With Solution
7/57
int i,j;
clrscr();
// entering size of array
coutm;
coutn;cout
-
8/3/2019 List of Practical Cs With Solution
8/57
Output:
Enter the size of 1st array A3
Enter the size of 2nd array B3
Enter values for Array A in Ascending Order
Enter value of 1 value of Array A 1
Enter value of 2 value of Array A 2Enter value of 3 value of Array A 3
Enter values for Array B in Descending OrderEnter value of 1 value of Array B 5
Enter value of 2 value of Array B 4
Enter value of 3 value of Array B 3
Merged arrays are as following....
5
43
3
2
1
Q5. Suppose A,B,C are arrays of size m, n, m+n respectively. Array A is stored in descending order an
array B is in ascending order. Write a program using function to receive 3 arrays and their sizes to store th
elements of A and B into C in descending order.
Solution:
//program to merge two arrays in a single array//Array A is in Ascending Order & Array B is in Descending Order
//Array C must be in Descending Order
#include#include
void main(){int m,n,k;
int a[50],b[50],c[100];
int i,j;
clrscr();// entering size of array
coutm;coutn;
cout
-
8/3/2019 List of Practical Cs With Solution
9/57
}
i=m-1;
j=0;
k=0;for(;i>=0 && j=b[j])
{c[k]=a[i];
i=i-1;
}else
{
c[k]=b[j];j=j+1;
}
k=k+1;
}if(i>=0)
{
while(i>=0)
c[k++]=a[i--];}
else{
while(j
-
8/3/2019 List of Practical Cs With Solution
10/57
12
11
10
Q6. Write program using a function SORTSCORE( ) in C++ to sort an array ofstructureExaminee in
descending order of Score using BUBBLE Sort.
Note : Assume the following definition of structure Examineestruct Examinee{
long RollNo;char Name[20];
float Score;
};Sample content of of the array (before sorting)
Roll NO. Name Score
1001 Ravyank Kapur 300
1005 Farida Khan 2891002 Anika Jain 345
1003 Gearge Peter 297
Sample content of of the array (after sorting)
Roll NO. Name Score1002 Anika Jain 345
1001 Ravyank Kapur 3001003 Gearge Peter 297
1005 Farida Khan 289
Solution:
//Bubble sorting of Structures#include
#include
#include#include
struct Examinee{long RollNo;
char Name[20];
float Score;
};void SORTSCORE(Examinee x[],int n)
{
int i,j;Examinee tmp;
for(i=0;i
-
8/3/2019 List of Practical Cs With Solution
11/57
cout
-
8/3/2019 List of Practical Cs With Solution
12/57
int empno;
char Ename[20];
float salary;
};
Solution:
//Program for sorting using selection sort
#include //inclusion of header file#include //inclusion of header file
#include //inclusion of header file
struct Employee{
int empno;
char Ename[20];float salary;
};
void sort(Employee x[],int n)
{int i,j,big,pos;
Employee tmp;
for(i=0;i
-
8/3/2019 List of Practical Cs With Solution
13/57
gets(x[i].Ename);
coutx[i].salary;
}sort(x,n);
getch();
}
Output:
Enter number of Employees 3
Enter values for employee 1
Enter Employee No. 1
Enter Employee Name AmarEnter Salary 2100
Enter values for employee 2
Enter Employee No. 2Enter Employee Name Dilip
Enter Salary 3200
Enter values for employee 3Enter Employee No. 3
Enter Employee Name SureshEnter Salary 1100
Records after sorting are
2 Dilip 32001 Amar 2100
3 Suresh 1100
Q7. Write a program to accept salaries of some employees in an array of size accepted from user and display
the salaries in ascending order. (Use insertion sort technique to sort the salaries)Solution:
//Arranging salaries stored in an array using Insertion sort
#include
#include
#includevoid main()
{
clrscr();int sal[100],i,j,tmp,n;
coutn;sal[0]=INT_MIN;
for(i=1;i
-
8/3/2019 List of Practical Cs With Solution
14/57
while(tmp
-
8/3/2019 List of Practical Cs With Solution
15/57
while(beg
-
8/3/2019 List of Practical Cs With Solution
16/57
{
cout
-
8/3/2019 List of Practical Cs With Solution
17/57
Enter values for array
Enter value of 1 row and 1 column 11
Enter value of 1 row and 2 column 12Enter value of 1 row and 3 column 13
Enter value of 2 row and 1 column 14
Enter value of 2 row and 2 column 15Enter value of 2 row and 3 column 16
Enter value of 3 row and 1 column 17
Enter value of 3 row and 2 column 18Enter value of 3 row and 3 column 19
Original array is11 12 13
14 15 16
17 18 19
Diagonal1 : 11 15 19
Diagonal Two : 13 15 17
Q10. Write a function in C++ which accepts a 2D array of integers and its size as
arguments and displays the elements of middle row and the elements of middle column.[Assuming the 2D Array to be a square matrix with odd dimension i.e. 33, 55, 77 etc...]
Example, if the array content is
3 5 4
7 6 92 1 8
Output through the function should be :
Middle Row : 7 6 9Middle Column : 5 6 1
Solution:#include //inclusion of header file#include //inclusion of header file
void show_mid(int a[100][100],int r,int c)
{
int i,k;k=r/2;
cout
-
8/3/2019 List of Practical Cs With Solution
18/57
coutr>>c;
cout
-
8/3/2019 List of Practical Cs With Solution
19/57
9 7 5 8
1 2 4 9
2 5 8 1
5 6 3 2
Solution:
#include //inclusion of header file
#include //inclusion of header filevoid swaparr(int a[100][100],int r,int c)
{int i,j,tmp;
for(j=0;j
-
8/3/2019 List of Practical Cs With Solution
20/57
getch();
}
Output:
Enter no. of rows and columns of array 3 3
Enter values for arrayenter value of 1 row and 1 column 1
enter value of 1 row and 2 column 2
enter value of 1 row and 3 column 3enter value of 2 row and 1 column 4
enter value of 2 row and 2 column 5
enter value of 2 row and 3 column 6enter value of 3 row and 1 column 7
enter value of 3 row and 2 column 8
enter value of 3 row and 3 column 9
Original array is
1 2 3
4 5 6
7 8 9
After swapping first and last rows array is7 8 9
4 5 6
1 2 3
Q12. Write a function in C++ which accepts an integer array and its size as arguments / parameters and
assign the elements into a two dimensional array of integers in the following format
(size must be odd)If the array is 1 2 3 4 5 If the array is 10 15 20
The output must be The output must be1 0 0 0 5 10 0 200 2 0 4 0 0 15 0
0 0 3 0 0 10 0 20
0 2 0 4 0
1 0 0 0 5
Solution:
#include
#includevoid perform(int a[100],int size)
{
int b[100][100],r,c,k=0;for(r=0;r
-
8/3/2019 List of Practical Cs With Solution
21/57
{
b[r][c]=0;
}
}}
k=size-1;
for(r=0;r
-
8/3/2019 List of Practical Cs With Solution
22/57
0 0 3 0 0
0 2 0 4 0
1 0 0 0 5
Q13. Write a function in C++ which accepts an integer array and its size as arguments and assign the
elements into a two dimensional array of integers in the following format:
If the array is 1,2,3,4,5,6 if the array is 1,2,3The resultant 2D array is The resultant 2D array is
1 0 0 0 0 0 1 0 0
1 2 0 0 0 0 1 2 01 2 3 0 0 0 1 2 3
1 2 3 4 0 0
1 2 3 4 5 01 2 3 4 5 6
Solution:
#include
#includevoid perform(int a[100],int size)
{
int b[100][100],r=0,k=0,i,c;
for(i=0;i
-
8/3/2019 List of Practical Cs With Solution
23/57
cin>>a[i];
}
perform(a,x);
getch();}
OUTPUT:
enter the size of the array3enter the value of 1number:1
enter the value of 2number:2
enter the value of 3number:3Contents of array B is
1 0 0
1 2 01 2 3
Q14 Write a function in C++ to print the count of the word is as an independent word
in at text file DIALOGUE.TXT.For example, if the content of the file DIALOGUE. TXT is
This is his book. Is this book good?
Then the output of the program should be 2.
Solution://Program to print total no. of "is"
#include#include
#include
void perform()
{ifstream A;
A.open("DIALOGUE.TXT",ios::in);
char s[15];int c=0;
while(!A.eof()){
A.getline(s,15,' ');
if((s[0]=='i' || s[0]=='I') && (s[1]=='S' || s[1]=='s'))
{
c++;}
}
cout
-
8/3/2019 List of Practical Cs With Solution
24/57
}
Output:
Enter the text for Data File
This is his book. is this book good?
Total No. of is=2
Q15 Given a binary file GAME.DAT, containing records of the following structure typestruct Game
{char GameName [20];
char Participant [30];
};
Write a function in C++ that would read contents from the file GAME.DAT and creates a file named
BASKET.DAT copying only those records from GAME.DAT where the game name is Basket Ball.
Solution:
#include
#include
#include
#includestruct Game
{
char GameName[20];char Participant[30];
};
void COPYGAME(){
Game y;
ifstream A;
A.open("GAME.DAT",ios::binary|ios::in);ofstream B;
B.open("BASKET.DAT",ios::binary|ios::out);
while(!A.eof()){
A.read((char*)&y,sizeof(Game));
if(strcmp(y.GameName,"Basket Ball")==0){
B.write((char*)&y,sizeof(Game));
}}
A.close();
B.close();
}
void main( ){
clrscr();Game x;
ofstream H;
int ch;H.open("GAME.DAT",ios::binary|ios::out);
do
{ cout
-
8/3/2019 List of Practical Cs With Solution
25/57
cout
-
8/3/2019 List of Practical Cs With Solution
26/57
contain only those words from the file FIRST.TXT which start with a lowercase vowel (i.e. with a, e, I
o, u).
For example if the file FIRST.TXT contains
Carry umbrella and overcoat when it rains
Then the file SECOND.TXT shall contain:
umbrella and overcoat it
Solution:
#include
#include
void vowelwords()
{
char s[20];
ifstream A;
A.open("FIRST.TXT",ios::in);
ofstream B;
B.open ("SECOND.TXT",ios::out);
while(!A.eof())
{
A.getline(s,20,' ');
if(s[0]=='a'||s[0]=='e'||s[0]=='i'||s[0]=='o'||s[0]=='u')
{
B
-
8/3/2019 List of Practical Cs With Solution
27/57
-
8/3/2019 List of Practical Cs With Solution
28/57
{
int rno;
float m1,m2,m3;
};
Write a program using function in C++ that would read contents from the file STUDENT.DAT and
modify the marks to 35 if anybodys any marks are less than 35.Solution:
#include#include
#include
struct Student{
int rno;
float m1,m2,m3;};
void perform()
{int c=0;
Student x;
ifstream A;
A.open("STUDENT.DAT",ios::in);ofstream B;
B.open("TEMP.DAT",ios::out);
while(!A.eof()){
A.read((char*)&x,sizeof(Student));
if(x.m1
-
8/3/2019 List of Practical Cs With Solution
29/57
}
void main()
{
clrscr();Student z;
int ch=1,v=0;
ofstream D;D.open("STUDENT.DAT",ios::out);
while(ch==1)
{coutz.rno;
coutz.m1>>z.m2>>z.m3;
D.write((char*)&z,sizeof(Student));
v=v+1;
coutch;
}
D.close();
ifstream E;E.open("STUDENT.DAT",ios::in);
cout
-
8/3/2019 List of Practical Cs With Solution
30/57
1 45 35 56
2 87 56 35
3 67 89 90
Q18 Given a binary file TELEPHON.DAT, containing records of the following class Directory :
class Directory
{ char Name[20] ;char Address[30] ;
char AreaCode[5] ;
char phone_No[15] ;public ;
void Register( ) ; // to accept values
void Show( ) ; // to display valuesint CheckCode(char AC[ ]);// to compare area code with accepted argument
} ;
Write a program to store object of above class in the data file at first and then using a function COPYABC( ) i
C++ copy all those records having AreaCode as 123 from TELEPHON.DAT to TELEBACK.DAT.
Solution:
#include
#include
#include#include
class Directory{
private:
char Name[20];
char Address[30];char AreaCode[5];
char phone_No[15];
public:void Register( )
{cout
-
8/3/2019 List of Practical Cs With Solution
31/57
void COPYABC()
{
Directory x;
ifstream A;A.open("TELEPHON.DAT",ios::binary|ios::in);
ofstream B;
B.open("TELEBACK.DAT",ios::binary|ios::out);while(!A.eof())
{
A.read((char*)&x,sizeof(x));if(x.checkcode("123")==0)
{
B.write((char*)&x,sizeof(x));}
}
A.close();
B.close();}
void main( )
{
clrscr();Directory x;
ofstream H;int ch;
H.open("TELEPHON.DAT",ios::binary|ios::out);
do
{x.Register();
H.write((char*)&x,sizeof(x));
coutch;
}while(ch==1);H.close();
COPYABC( );
cout
-
8/3/2019 List of Practical Cs With Solution
32/57
}
Output:
Enter Name of customerAmar
Enter Address of customer PatnaEnter Area code123
Enter Phone number123456
Press 1 to enter more records else to stop 1
Enter Name of customerSuresh
Enter Address of customer KhagaulEnter Area code254
Enter Phone number345212
Press 1 to enter more records else to stop 1
Enter Name of customerWilson
Enter Address of customer Danapur
Enter Area code123Enter Phone number789656
Press 1 to enter more records else to stop 2
Contents Of Teleback.dat is as follows:Name of customer Amar
Address of customer PatnaArea code 123
Phone number 123456
Name of customer WilsonAddress of customer Danapur
Area code 123
Phone number 789656
Q19. Write a program to store, delete and display dynamically allocated Queue containing nodes of thfollowing given structure:struct NODE
{
int Itemno;
char Itemname[20];NODE *Link;
};
The Program should contain the following functions:i) push( )- to insert new element in Queue ii) pop( ) to delete an element from the Queue
iii) display() to display the element of Queue.
Solution://QUEUE USING LINKED LIST
#include
#include#include
#include
#include
struct Node{
int Itemno;
char Itemname[20];
-
8/3/2019 List of Practical Cs With Solution
33/57
Node *Link;
}
*top, *newptr, *save, *ptr,*rear;
Node* Create_New_Node(int,char[]);void push_at_end(Node*);
void Display(Node*);
void pop();void main()
{
top=rear=NULL;int rno;
char ch='y',name[20];
clrscr();while(ch=='y' || ch=='Y')
{
cout
-
8/3/2019 List of Practical Cs With Solution
34/57
{
if(top==NULL)
{
top=np;rear=np;
}
else{
rear->Link=np;
rear=np;}
}
void Display(Node *np){
while(np!=NULL)
{
cout
-
8/3/2019 List of Practical Cs With Solution
35/57
The Queue now is :
1 Lux
2 Liril!!!
Want to pop an element? (Y/N)...y
The Queue now is :
2 Liril!!!
Want to pop an element? (Y/N)...n
Q20. Write a program to store, delete and display dynamically allocatedStackof Books implemented wit
the help of the following structure:struct Book
{
int BNo;
char BName[20];
Book *Next;
};
The Program should contain the following functions:
i) push( )- to insert new element in stack ii) pop( ) to delete an element from the stack
iii) display() to display the element of stack.
Solution:
//STACK USING LINKED LIST-Insertion in the beginning of Stack
#include
#include#include
#include
#include
struct Book{
int Bno;char Bname[20];
Book *Next;
}
*top, *newptr, *save, *ptr;Book* Create_New_Node(int n,char nm[]);
void push(Book*);
void Display(Book*);void pop();
void main(){top=NULL;
int n;
char ch='y',bn[20];clrscr();
while(ch=='y' || ch=='Y')
{
cout
-
8/3/2019 List of Practical Cs With Solution
36/57
cin>>n;
cout
-
8/3/2019 List of Practical Cs With Solution
37/57
void Display(Book *np)
{
while(np!=NULL)
{cout
-
8/3/2019 List of Practical Cs With Solution
38/57
1 Himmat
!!!
Want to pop an element? (Y/N)...n
Q21. Write a program using arrays to implement STACKas an array. The Program must be written using
classes and objects and it must contain the following functions:i) push( )- to insert new element in stack ii) pop( ) to delete an element from the stack
iii) display() to display the element of stack.
Solution://PROGRAM TO IMPLEMENT THE STACK//
#include //inputs the header files
#include #include
class stk //begin of class
{
public:int arr[100];
int r;
stk() //constructor
{r=-1;
}void push(int);
void pop();
void display();
}; //end of class
void stk::push(int n)
{if (r>98)
{cout
-
8/3/2019 List of Practical Cs With Solution
39/57
void stk::display()
{
if (r==-1){
cout
-
8/3/2019 List of Practical Cs With Solution
40/57
4.Exit
Enter Choice: 1
Enter No. to insert in Stack 11
1.Insert
2.Delete3.Display
4.Exit
Enter Choice: 1
Enter No. to insert in Stack 22
1.Insert
2.Delete
3.Display
4.ExitEnter Choice: 2
No. deleted is 22
1.Insert
2.Delete3.Display
4.Exit
Enter Choice: 3
11
1.Insert2.Delete
3.Display4.ExitEnter Choice: 4
Q22. Write a program using arrays to implement QUEUE as an array. The Program must be written using
classes and objects and it must contain the following functions:
i) push( )- to insert new element in queue ii) pop( ) to delete an element from the queueiii) display() to display the element of queue.
Solution:
//PROGRAM TO IMPLEMENT THE QUEUE//#include //inputs the header files
#include
#include class que //begin of class
{
public:
int arr[100];int r;
que() //constructor
{
-
8/3/2019 List of Practical Cs With Solution
41/57
r=-1;
}
void push(int);
void pop();void display();
}; //end of class
void que::push(int n)
{
if (r>98){
cout
-
8/3/2019 List of Practical Cs With Solution
42/57
void main() //begin of main function
{
int ch,n;
clrscr();que ob;
while(1)
{cout
-
8/3/2019 List of Practical Cs With Solution
43/57
4.Exit
Enter Choice: 2
No. deleted is 111.Insert
2.Delete
3.Display4.Exit
Enter Choice: 3
221.Insert
2.Delete
3.Display4.Exit
Enter Choice: 4
Q23. Write a program using arrays to implement CIRCULAR QUEUE as an array. The Program must bewritten using classes and objects and it must contain the following functions:
i) push( )- to insert new element in circular queue ii) pop( ) to delete an element from the queue iii)
display() to display the element of circular queue.
Solution://PROGRAM TO IMPLEMENT THE CIRCULAR QUEUE//
#include //inputs the header files#include
#include
class cirque //begin of class
{public:
int arr[100];
int front,rear,size;cirque(int s)
{front=-1;rear=-1;
size=s;
}
void insert (int);void del();
void display();
}; //end of class
void cirque::insert(int n)
{if ((front==0 && rear==size-1)||(front==rear+1))
{
cout
-
8/3/2019 List of Practical Cs With Solution
44/57
else
rear++;
arr[rear]=n;
}
void cirque::del()
{ int i;if (front==-1)
{
cout
-
8/3/2019 List of Practical Cs With Solution
45/57
}
void main() //begin of main function
{int ch,n;
clrscr();
cirque ob(5);while(1)
{
cout
-
8/3/2019 List of Practical Cs With Solution
46/57
2.Delete
3.Display
4.Exit
Enter Choice: 2
No. deleted is 11
1.Insert2.Delete
3.Display
4.ExitEnter Choice: 3
22
1.Insert2.Delete
3.Display
4.Exit
Enter Choice: 4
Q24. An array of structure stores details of 10 students (rno, name, marks in three subjects) Write a program
to create such an array and print out a list of students who have failed in more than one subject.(Max. Marks
=100, Passing Marks =33)Solution:
//Array of Structures#include
#include
#include
#includestruct Student
{
long RollNo;char Name[20];
int m1,m2,m3;};
void main()
{
clrscr();Student x[10];
int i,c;
for(i=0;i
-
8/3/2019 List of Practical Cs With Solution
47/57
if(x[i].m1
-
8/3/2019 List of Practical Cs With Solution
48/57
return(s);
}
void main()
{double X,ans;
int n;
clrscr();coutX>>n;
ans=SUMFUN(X,n);cout
-
8/3/2019 List of Practical Cs With Solution
49/57
SQL> insert into BOOKS values('L02','Science','Agarkar','DEF',90,15);
1 row created.
SQL>insert into BOOKS values('L03','Social','Suresh','XYZ',85,30);
1 row created.SQL> insert into BOOKS values('L04','Computer','Sumita','ABC',75,7);
1 row created.
SQL> insert into BOOKS values('L05','Telegu','Nannayya','DEF',60,25);1 row created.
SQL> insert into BOOKS values('L06','English','Wordworth','DEF',55,12);
1 row created.
SQL> create table ISSUES(Book_ID varchar2(3),Qty_Issued number(2));
Table created.
SQL> insert into ISSUES values('L02',13);
1 row created.
SQL> insert into ISSUES values('L04',5);1 row created.
SQL> insert into ISSUES values('L05',21);
1 row created.
(i) SQL>select BookName,AuthorName,Price from BOOKS where Publisher='ABC';
BOOKNAME AUTHORNAME PRICE--------------- -------------------- ----------
Maths Raman 70
Computer Sumita 75
(ii) SQL> select * from BOOKS order by price desc;
BOO BOOKNAME AUTHORNAME PUBLISHER PRICE QTY
--- --------------- -------------------- --------------- ---------- ----------L02 Science Agarkar DEF 90 15
L03 Social Suresh XYZ 85 30L04 Computer Sumita ABC 75 7L01 Maths Raman ABC 70 20
L05 Telegu Nannayya DEF 60 25
L06 English Wordworth DEF 55 12
6 rows selected.
(iii) SQL> update ISSUES set Qty_Issued=Qty_Issued-3;
3 rows updated.
(iv) SQL>select BOOKS.Book_Id,BookName,Publisher,Price,Qty,Qty_Issued from BOOKS,ISSUES
where BOOKS.Book_Id=ISSUES.Book_IDBOO BOOKNAME PUBLISHER PRICE QTY QTY_ISSUED
--- --------------- --------------- ---------- ---------- ----------
L02 Science DEF 90 15 10L04 Computer ABC 75 7 2
L05 Telegu DEF 60 25 18
(v) SQL> select sum(price) from Books where Publisher='DEF';SUM(PRICE)
----------
205
-
8/3/2019 List of Practical Cs With Solution
50/57
(vi) SQL> select Publisher,min(Price) from Books GROUP BY Publisher;
PUBLISHER MIN(PRICE)
--------------- ----------
ABC 70DEF 55
XYZ 85
(vii) SQL> select price from Books,Issues where Books.Book_ID=Issues.Book_ID AND Qty_Issued=5;
no rows selected
(viii) SQL> select count(Distinct Publisher) from Books;
COUNT(DISTINCTPUBLISHER)
------------------------3
Q2. Consider the following tables EMPLOYEES and EMPSALARY. Write SQL commands for the statement
(i) to (iv) and give outputs for SQL queries (v) to (viii).
EMPLOYEES
EMPID FIRSTNAME LASTNAME ADDRESS CITY
010 George Smith 83, First Street Howard
105 Mary Jones 842 Vine Ave Losantiville
152 Sam Tones 33 Elm St. Paris215 Sarah Ackerman 440 U.S. 110 Upton
244 Manila Sengupta 24 Friends Street New Delhi
300 Robert Samuel 9 Fifth Cross Washington
335 Henry Williams 12 Moore Street Boston
400 Rachel Lee 121 Harrison St. New York
441 Peter Thompson 11 Red Road Paris
EMPSALARY
EMPID SALARY BENEFIT DESIGNATION
010 75000 15000 Manager
105 65000 15000 Manager
152 80000 25000 Director
215 75000 12500 Manager
244 50000 12000 Clerk
300 45000 10000 Clerk
335 40000 10000 Clerk
400 32000 7500 Salesman
441 28000 7500 Salesman
(i) To display Firstname, Lastname, Address and City of all employees living in Paris from the table
EMPLOYEES.
(ii) To display the contents of EMPLOYEES table in descending Order of FIRSTNAME.(iii) To display the Firstname, Lastname and Toatal salary of all Managers from the tables EMPLOYEES an
EMPSALARY, where Total salary is calculated as Salary+Benefits.
(iv) To display the Minimum salary among Managers and Clerks from the table EMPSALARY.(v) SELECT FIRSTNAME,SALARY FROM EMPLOYEES, EMPSALARY wher
DESIGNATION=Salesman AND EMPLOYEES.EMPID=EMPSALARY.EMPID;
(vi) SELECT COUNT(DISTINCT DESIGNATION) FROM EMPSALARY;
(vii) SELECT DESIGNATION,SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATIONHAVING COUNT(*)>2;
(viii) SELECT SUM(BENEFIT) FROM EMPLOYEES WHERE DESIGNATION=Clerk;
-
8/3/2019 List of Practical Cs With Solution
51/57
Solution:SQL> create table EMPLOYEES(EMPID NUMBER(3),FIRSTNAME VARCHAR2(10),LASTNAMEVARCHAR2(10),ADDRESS VARCHAR2(25),CITY VARCHAR2(15));
Table created.
SQL> INSERT INTO EMPLOYEES VALUES(010,'George','Smith','83,First Street','Howard');
1 row created.
SQL> INSERT INTO EMPLOYEES VALUES(105,'Mary','Jones','842, Vine Ave','Losantiville');1 row created.SQL> INSERT INTO EMPLOYEES VALUES(152,'Sam','Tones','33elm St.','Paris');
1 row created.
SQL> INSERT INTO EMPLOYEES VALUES(215,'Sarah','Ackerman','440 U.S. 110','Upton');1 row created.
SQL> INSERT INTO EMPLOYEES VALUES(244,'Manila','Sengupta','24 Friends Street','New Delhi');
1 row created.SQL> INSERT INTO EMPLOYEES VALUES(300,'Robert','Samuel','9 Fifth Cross','Washington');
1 row created.
SQL> INSERT INTO EMPLOYEES VALUES(335,'Henry','Williams','12 Moore Street','Boston');
1 row created.SQL> INSERT INTO EMPLOYEES VALUES(400,'Rachel','Lee','121 Harrison St.','New York');
1 row created.
SQL> INSERT INTO EMPLOYEES VALUES(441,'Peter','thompson','11 Red Road','Paris');1 row created.
SQL> create table EMPSALARY(EMPID number(3),salary number(5),benefit number(5), designationvarchar2(10));
Table created.
SQL> insert into empsalary values(010,75000,15000,'Manager');
1 row created.SQL> insert into empsalary values(105,65000,15000,'Manager');
1 row created.SQL> insert into empsalary values(152,80000,25000,'Director');
1 row created.
SQL> insert into empsalary values(215,75000,12500,'Manager');1 row created.
SQL> insert into empsalary values(244,50000,12000,'Clerk');
1 row created.SQL> insert into empsalary values(300,45000,10000,'Clerk');
1 row created.
SQL> insert into empsalary values(335,40000,10000,'Clerk');1 row created.
SQL> insert into empsalary values(400,32000,7500,'Salesman');
1 row created.
SQL> insert into empsalary values(441,28000,7500,'Salesman');1 row created.
(i) SQL> select firstname,lastname,address,city from employees where city='Paris';FIRSTNAME LASTNAME ADDRESS CITY
---------- ---------- ------------------------- ---------------
Sam Tones 33elm St. Paris
-
8/3/2019 List of Practical Cs With Solution
52/57
Peter thompson 11 Red Road Paris
(ii) SQL> select * from employees order by firstname;EMPID FIRSTNAME LASTNAME ADDRESS CITY
---------- ---------- ---------- ------------------------- ---------------
10 George Smith 83,First Street Howard335 Henry Williams 12 Moore Street Boston
244 Manila Sengupta 24 Friends Street New Delhi
105 Mary Jones 842, Vine Ave Losantiville441 Peter thompson 11 Red Road Paris
400 Rachel Lee 121 Harrison St. New York
300 Robert Samuel 9 Fifth Cross Washington152 Sam Tones 33elm St. Paris
215 Sarah Ackerman 440 U.S. 110 Upton
9 rows selected.
(iii) SQL> select firstname,lastname, salary+benefit "Tot. sal" from employees E,empsalary S where
E.Empid=S.Empid AND designation ='Manager';
FIRSTNAME LASTNAME Tot. sal
---------- ---------- ----------George Smith 90000
Mary Jones 80000Sarah Ackerman 87500
(iv) SQL> select designation,min(salary) from empsalary group by designation having designation = 'Manager
or designation='Clerk';DESIGNATIO MIN(SALARY)
---------- -----------
Clerk 40000Manager 65000
(v) SQL>SELECT FIRSTNAME,SALARY FROM EMPLOYEES, EMPSALARY where DESIGNATION='Salesman' AND EMPLOYEES.EMPID=EMPSALARY.EMPID;
FIRSTNAME SALARY
---------- ----------
Rachel 32000Peter 28000
(vi) SQL> SELECT COUNT(DISTINCT DESIGNATION) FROM EMPSALARY;COUNT(DISTINCTDESIGNATION)
--------------------------
4
(vii) SQL> SELECT DESIGNATION,SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION
HAVING COUNT(*)>2;DESIGNATIO SUM(SALARY)
---------- -----------
Clerk 135000
Manager 215000
(viii) SQL> SELECT SUM(BENEFIT) FROM EMPLOYEES WHERE DESIGNATION='Clerk';
SELECT SUM(BENEFIT) FROM EMPLOYEES WHERE DESIGNATION='Clerk'
-
8/3/2019 List of Practical Cs With Solution
53/57
*
ERROR at line 1:
ORA-00904: invalid column name
Q3. Study the following tables FLIGHTS and FARES and write SQL commands for the questions (i) to (iv)
and give outputs for SQL queries (v) to (vi).
TABLE : FLIGHTS
FL_NO STARTING ENDING NO_FLIGHTS NO STOPS
IC301 MUMBAI DELHI 8 0
IC799 BANGALORE DELHI 2 1
MC101 INDORE MUMBAI 3 0
IC302 DELHI MUMBAI 8 0
AM812 KANPUR BANGALORE 3 1IC899 MUMBAI KOCHI 1 4
AM501 DELHI TRIVANDRUM 1 5
MU499 MUMBAI MADRAS 3 3
IC701 DELHI AHMEDABAD 4 0
TABLE : FARES
FL_NO AIRLINES FARE TAX%
1C701 Indian Airlines 6500 10
MU499 Sahara 9400 5
AM501 Jet Airways 13450 8
IC899 Indian Airlines 8300 4
1C302 Indian Airlines 4300 10
1C799 Indian Airlines 10500 10
MC101 Deccan Airlines 3500 4
i. Display FL_NO and NO_FLIGHTS from KANPUR to BANGALORE from the table FLIGHTS.
ii. Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
iii. Display the FLNO and fare to be paid for the flights from DELHI to MUMBAI using the tablesFLIGHTS and FARES, where the fare to be paid = FARE +FARE*TAX%/100.
iv. Display the minimum fare Indian Airlines is offering from the table FARES.
v. SELECT FL_NO, NO_FLIGHTS, AIRLINES from FLIGHTS, FARES where STARTING=DELHI
and FLIGHTS.FL_NO=FARES.FL_NO.vi. SELECT count (distinct ENDING) from FLIGHTS.
-
8/3/2019 List of Practical Cs With Solution
54/57
Q4. Consider the following tables.
Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries
(v) to (viii)
(i) To display the names of all Senders from Mumbai.
(ii) To display the RecID), SenderName, SenderAddress, RecName, RecAddress for every Recipient.
(iii) To display Recipient details in ascending order of RecName.(iv) To display number of Recipients from each city.
(v) SELECT DISTINCT SenderCity FROM Sender;
(vi) SELECT A. SenderName, B.RecName FROM Sender A, Recipient B WHERE A.SenderlD =
B.SenderlD AND B.RecCity = Mumbai;(vii) SELECT RecName, RecAddress FROM Recipient WHERE RecCity NOT IN (Mumbai, Kolkata);
(viii) SELECT RecID , RecName FROM Recipient WHERE SenderID = MU02' ORSenderID=ND50';
Q5. Consider the following tables Consignors and Consignee. Write SQL commands for
the statements (i) to (iv) and give outputs for SQL queries (v) to (viii).TABLE : CONSIGNORS
-
8/3/2019 List of Practical Cs With Solution
55/57
(i) To display the names of all Consignors from Mumbai.(ii) To display the CneelD, CnorName, CnorAddress, CneeName, CneeAddress for every Consignee.
(iii) To display consignee details in ascending order of CneeName.
(iv) To display number of consignors from each city,(v) SELECT DISTINCT City FROM CONSIGNEE;
(vi) SELECT A.CnorName, B.CneeName FROM Consignors A, Consignee B WHERE A.CnorID = B.CnorlD
AND B.CneeCity = Mumbai;
(vii) SELECT CneeName, CneeAddress FROM Consignee WHERE CneeCity NOT IN (Mumbai,Kolkata);
(viii) SELECT CNEEID,CNEENAME FROM CONSIGNEE WHERE CNORID='MU15' OR
CNORID='ND01';
Solution:SQL> CREATE TABLE CONSIGNORS(CnorID char(4),CnorName varchar2(15),CnorAddress
varchar2(20),City varchar2(15));Table created.
SQL> insert into consignors values('ND01','R Singhal','24, ABC Enclave','New Delhi');
1 row created.
SQL>insert into consignors values('ND02','Amit Kumar','123,PalmAvenue','New Delhi');
1 row created.SQL> insert into consignors values('MU15','R Kohli','5/A,South Street','Mumbai');
1 row created.
SQL>insert into consignors values('MU50','S Kaur','27-K,Westend','Mumbai');1 row created.
SQL> create table consignee(CneeID char(4),CnorID char(4),CneeName varchar2(15),CneeAddressvarchar2(20),CneeCity varchar2(15));
Table created.
SQL> insert into consignee values('MU05','ND01','Rahul Kishore','5,ParkAvenue','Mumbai');1 row created.
SQL> insert into consignee values('ND08','ND02','P Dhingra','16/J, Moore Enclave','New Delhi');
1 row created.SQL> insert into consignee values('KO19','MU15','A P Roy','2A, Central Avenue','Kolkata');
-
8/3/2019 List of Practical Cs With Solution
56/57
1 row created.
SQL> insert into consignee values('MU32','ND02','S Mittal','P245,AB Colony','Mumbai');
1 row created.
SQL> insert into consignee values('ND48','MU50','B P Jain','13, Block D,A Vihar','New Delhi');1 row created.
(i) SQL> select CnorName from Consignors where city='Mumbai';CNORNAME
---------------
R KohliS Kaur
(ii) SQL> select CneeID,CnorName,CnorAddress,CneeName,CneeAddress from Consignors C, Consignee Ewhere C.CnorID=E.CnorID;
CNEE CNORNAME CNORADDRESS CNEENAME CNEEADDRESS
---- --------------- -------------------- --------------- --------------------
KO19 R Kohli 5/A,South Street A P Roy 2A, Central Avenue ND48 S Kaur 27-K,Westend B P Jain 13, Block D,A Vihar
MU05 R Singhal 24, ABC Enclave Rahul Kishore 5,ParkAvenue
ND08 Amit Kumar 123,PalmAvenue P Dhingra 16/J, Moore Enclave
MU32 Amit Kumar 123,PalmAvenue S Mittal P245,AB Colony
(iii) SQL> select * from consignee order by CneeName;CNEE CNOR CNEENAME CNEEADDRESS CNEECITY
---- ---- --------------- -------------------- ---------------
KO19 MU15 A P Roy 2A, Central Avenue Kolkata
ND48 MU50 B P Jain 13, Block D,A Vihar New Delhi ND08 ND02 P Dhingra 16/J, Moore Enclave New Delhi
MU05 ND01 Rahul Kishore 5,ParkAvenue Mumbai
MU32 ND02 S Mittal P245,AB Colony Mumbai
(iv) SQL>select city, count(*) from consignors group by city;CITY COUNT(*)--------------- ----------
Mumbai 2
New Delhi 2
(v) SQL> SELECT DISTINCT City FROM CONSIGNEE;
SELECT DISTINCT City FROM CONSIGNEE
*ERROR at line 1:
ORA-00904: invalid column name
SQL> SELECT DISTINCT CNEECITY FROM CONSIGNEE;
CNEECITY
---------------Kolkata
Mumbai
New Delhi
(vi) SQL>SELECT A.CNORNAME, B.CNEENAME FROM Consignors A, Consignee B WHERE
A.CNORID= B.CNORID AND B.CNEECITY = 'Mumbai';
CNORNAME CNEENAME
-
8/3/2019 List of Practical Cs With Solution
57/57
--------------- ---------------
R Singhal Rahul Kishore
Amit Kumar S Mittal
SQL> SELECT A.CnorName, B.CneeName FROM Consignors A, Consignee B WHERE A.CnorID =
B.CnorlD AND B.CneeCity = 'Mumbai';
SELECT A.CnorName, B.CneeName FROM Consignors A, Consignee B WHERE A.CnorID = B.CnorlDAND B.CneeCity = 'Mumbai'
*
ERROR at line 1:ORA-00904: invalid column name
(vii) SQL> SELECT CneeName, CneeAddress FROM Consignee WHERE CneeCity NOT IN ('Mumbai','Kolkata');
CNEENAME CNEEADDRESS
--------------- --------------------
P Dhingra 16/J, Moore EnclaveB P Jain 13, Block D,A Vihar
(viii) SQL> SELECT CNEEID,CNEENAME FROM CONSIGNEE WHERE CNORID='MU15' OR
CNORID='ND01';CNEE CNEENAME
---- ---------------MU05 Rahul Kishore
KO19 A P Roy