Sunteți pe pagina 1din 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 int Rent of type int Amount of type float. calculate( ) This member function should calculate the value of amount as Rent+ cost for the units. Where cost for the units can be calculated according to the following conditions. No_of_units Cost First 50 calls Free Next 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 <iostream.h> #include <conio.h> #include <stdio.h> #include<string.h> 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()

{ if(No_of_units<=50) Amount=Rent; else if(No_of_units>=51 && No_of_units<=150) Amount = Rent+(No_of_units-50)*0.80; else if(No_of_units>=151 && No_of_units<=350) Amount=Rent+100*.80+(No_of_units-150)*1.00; else Amount=Rent+No_of_units*1.20; } void phonebill::accept() { cout<<"\n enter user name"; gets(CustomerName); cout<<"\n enter phone no." ; cin>>PhoneNumber; cout<<"\n enter no.of units"; cin>>No_of_units; cout<<"\n enter rent"; cin>>Rent; calculate(); } void phonebill:: display() { cout<<"\n user name"<<CustomerName; cout<<"\n phone no."<<PhoneNumber; cout<<"\n no.of units"<<No_of_units; cout<<"\n rent"<<Rent; cout<<"\n amount"<<Amount; } void main() { clrscr(); phonebill x; x.accept(); clrscr(); x.display(); getch(); } Output: enter user name Akash enter phone no. 298283 enter no.of units 150 enter rent 100 user name Akash phone no. 298283 no.of units 280 rent 100 amount 310

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 integer Totalfare of type float Public Members : A constructor to assign initial values as follows : TCode with the word NULL NoofAdults as 0 NoofKids as 0 Kilometres as 0 TotalFare as 0 A function AssignFare ( ) which calculates and assigns the value of the data member TotalFare as follows : For each Adult Fare(Rs) For Kilometres 500 >=1000 300 <1000 &>=500 200 <500 For each Kid the above Fare will be 50% of the Fare mentioned in the above table For example : If Kilometres is 850, Noofadults = 2 and Noofkids = 3 Then TotalFare should be calculated as Numofadults * 300 + Noofkids * 150 i.e. 2*300 + 3*150=1050 A function EnterTour( ) to input the values of the data members TCode, NoofAdults, NoofKids and Kilometres; and invoke the Assign Fare( ) function. A function ShowTour( ) which displays the content of all the data members for a Tour. Solution: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class Tour { private: char TCode[5]; int NoofAdults; int NoofKids; int Kilometres; float TotalFare; public: Tour(); void AssignFare(); void EnterTour(); void ShowTour(); }; void Tour::Tour(void) { strcpy(TCode,"NULL"); NoofAdults=0;

NoofKids=0; Kilometres= 0; TotalFare=0.0f; } void Tour::EnterTour(void) { cout<<"\nEnter value of travel Code "; cin>>TCode; cout<<"\nEnter no. of adults "; cin>>NoofAdults; cout<<"\nEnter No of kids "; cin>>NoofKids; cout<<"\nEnter distance"; cin>>Kilometres; 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<<"\n\n\n\n\n\n\nTravel List is as follows :"; cout<<"\n Travel Code: "<<TCode; cout<<"\n Number Of Adults: "<<NoofAdults; cout<<"\n Number of kids: "<<NoofKids; cout<<"\n Kilometres: "<<Kilometres; cout<<"\n Total Fare:"<<TotalFare; } void main() { clrscr(); Tour x; x.EnterTour(); x.ShowTour(); getch(); } Output: Enter value of travel Code 101 Enter no. of adults 2 Enter No of kids 3

Enter distance 850 Travel List is as follows Travel Code: 101 Number Of Adults: 2 Number of kids: 3 Kilometres: 850 Total Fare:1050 Q3. Define a class ELECTION with the following specifications : Private members : Data : candidate_name , party , vote_received Public members : Functions : INPUT( ) to input data DISPLAY( ) to display the details of the winner WINNER( ) To return the details of the winner through the object after comparing the votes received by three candidates . Write a suitable main ( ) function to input data in 3 objects of ELECTION type and display the details of the winning candidate. Solution: #include<iostream.h> #include<conio.h> #include<stdio.h> class ELECTION { private: char cname[20]; char party[30]; long voterecevied; public: void input() { cout<<"\n Enter candidates name"; gets(cname); cout<< "\n Enter party of the candidate name"; gets(party); cout<<"\n Enter the amount of vote recevied by the candidate"; cin>>voterecevied; cout<<"\n\n\n\n"; } void display() { clrscr(); cout<<"\n Winning candidate"; cout<<"\n Candidate name"<<cname; cout<< "\n Party of the candidate "<<party; cout<<"\n The amount of vote recevied by the candidate"<<voterecevied; } ELECTION WINNER(ELECTION X,ELECTION Y,ELECTION Z) { if((X.voterecevied>Y.voterecevied) && (X.voterecevied>Z.voterecevied)) {

return(X); } else if ((Y.voterecevied>Z.voterecevied) && (Y.voterecevied>X.voterecevied)) { return(Y); } else return(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 Abhinav Enter 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 Rajeev Enter party of the candidate name CONGRESS Enter the amount of vote recevied by the candidate46466 Winning candidate Candidate name Rahul Party 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 and array B is in descending order. Write a program using function to receive 3 arrays and their sizes to store the 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<iostream.h> #include<conio.h> void main() { int m,n,k; int a[50],b[50],c[100];

int i,j; clrscr(); // entering size of array cout<<"\nEnter the size of 1st array A"; cin>>m; cout<<"\nEnter the size of 2nd array B"; cin>>n; cout<<"\nEnter values for Array A in Ascending Order\n"; for(i=0;i<m;i++) { cout<<"\nEnter value of "<<i+1<<" value of Array A "; cin>>a[i]; } cout<<"\n\n\nEnter values for Array B in Descending Order\n"; for(j=0;j<n;j++) { cout<<"\nEnter value of "<<j+1<<" value of Array B "; cin>>b[j]; } i=m-1; j=0; k=0; for(;i>=0 && j<n;) //Shifting array A and B to Array C { if(a[i]>=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<n) c[k++]=b[j++]; } //printing contents of merged array cout<<"\nMerged arrays are as following....\n"; for(i=0;i<(m+n);i++) { cout<<c[i]<<"\n"; } getch(); }

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 2 Enter value of 3 value of Array A 3 Enter values for Array B in Descending Order Enter 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 4 3 3 2 1 Q5. Suppose A,B,C are arrays of size m, n, m+n respectively. Array A is stored in descending order and array B is in ascending order. Write a program using function to receive 3 arrays and their sizes to store the 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<iostream.h> #include<conio.h> void main() { int m,n,k; int a[50],b[50],c[100]; int i,j; clrscr(); // entering size of array cout<<"\nEnter the size of 1st array A"; cin>>m; cout<<"\nEnter the size of 2nd array B"; cin>>n; cout<<"\nEnter values for Array A in Ascending Order\n"; for(i=0;i<m;i++) { cout<<"\nEnter value of "<<i+1<<" value of Array A "; cin>>a[i]; } cout<<"\n\n\nEnter values for Array B in Descending Order\n"; for(j=0;j<n;j++) { cout<<"\nEnter value of "<<j+1<<" value of Array B "; cin>>b[j];

} i=m-1; j=0; k=0; for(;i>=0 && j<n;) //Shifting array A and B to Array C { if(a[i]>=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<n) c[k++]=b[j++]; } //printing contents of merged array cout<<"\nMerged arrays are as following....\n"; for(i=0;i<(m+n);i++) { cout<<c[i]<<"\n"; } getch(); } Output: Enter the size of 1st array A3 Enter the size of 2nd array B3 Enter values for Array A in Descending Order Enter value of 1 value of Array A 13 Enter value of 2 value of Array A 12 Enter value of 3 value of Array A 11 Enter values for Array B in Ascending Order Enter value of 1 value of Array B 10 Enter value of 2 value of Array B 14 Enter value of 3 value of Array B 16 Merged arrays are as following.... 16 14 13

12 11 10 Q6. Write program using a function SORTSCORE( ) in C++ to sort an array of structure Examinee in descending order of Score using BUBBLE Sort. Note : Assume the following definition of structure Examinee struct 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 289 1002 Anika Jain 345 1003 Gearge Peter 297 Sample content of of the array (after sorting) Roll NO. Name Score 1002 Anika Jain 345 1001 Ravyank Kapur 300 1003 Gearge Peter 297 1005 Farida Khan 289 Solution: //Bubble sorting of Structures #include<iostream.h> #include<conio.h> #include<process.h> #include<stdio.h> struct Examinee { long RollNo; char Name[20]; float Score; }; void SORTSCORE(Examinee x[],int n) { int i,j; Examinee tmp; for(i=0;i<n;i++)//Loop for Bubble Sorting { for(j=0;j<(n-1);j++) { if(x[j].Score<x[j+1].Score) { tmp=x[j]; x[j]=x[j+1]; x[j+1]=tmp; } } }

cout<<"\nExaminees after sorting are \n"; for(i=0;i<n;i++) { cout<<x[i].RollNo<<"\t"<<x[i].Name<<"\t"<<x[i].Score<<"\n"; } } void main() { clrscr(); Examinee x[100]; int i,n; cout<<"Enter number of Examinees"; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter value for Examinee "<<i+1<<"\n"; cout<<"\nEnter Roll No. "; cin>>x[i].RollNo; cout<<"\nEnter Name "; gets(x[i].Name); cout<<"\nEnter Score "; cin>>x[i].Score; } SORTSCORE(x,n); getch(); } Output: Enter number of Examinees3 Enter value for Examinee 1 Enter Roll No. 1 Enter Name Akash Enter Score 300 Enter value for Examinee 2 Enter Roll No. 2 Enter Name Fariha Enter Score 280 Enter value for Examinee 3 Enter Roll No. 3 Enter Name Gagan Enter Score 345 Examinees after sorting are 3 Gagan 345 1 Akash 300 2 Fariha 280
Q6(A). An array E containing elements of structure Employee is required to be arranged in descending

order of salary. Write a C++ function to arrange the same with the help of Selection sort. The array and its size is required to be passed as parameters to the functions. Definitions of the structure is as follows struct Employee {

int empno; char Ename[20]; float salary; }; Solution: //Program for sorting using selection sort #include<iostream.h> //inclusion of header file #include<conio.h> //inclusion of header file #include<stdio.h> //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<n;i++)//Exchange Selection sort { big=x[i].salary; for(j=i+1;j<n;j++) { if(x[j].salary>big) { big=x[j].salary; pos=j; } } tmp=x[i]; x[i]=x[pos]; x[pos]=tmp; } cout<<"\nRecords after sorting are \n"; for(i=0;i<n;i++) { cout<<x[i].empno<<"\t"<<x[i].Ename<<"\t"<<x[i].salary<<"\n"; } } void main() { clrscr(); Employee x[100]; int i,n; cout<<"Enter number of Employees "; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter values for employee "<<i+1<<"\n"; cout<<"\nEnter Employee No. "; cin>>x[i].empno; cout<<"\nEnter Employee Name ";

gets(x[i].Ename); cout<<"\nEnter Salary "; cin>>x[i].salary; } sort(x,n); getch(); } Output: Enter number of Employees 3 Enter values for employee 1 Enter Employee No. 1 Enter Employee Name Amar Enter Salary 2100 Enter values for employee 2 Enter Employee No. 2 Enter Employee Name Dilip Enter Salary 3200 Enter values for employee 3 Enter Employee No. 3 Enter Employee Name Suresh Enter Salary 1100 Records after sorting are 2 Dilip 3200 1 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<iostream.h> #include<conio.h> #include<limits.h> void main() { clrscr(); int sal[100],i,j,tmp,n; cout<<"Enter number of employees "; cin>>n; sal[0]=INT_MIN; for(i=1;i<=n;i++) { cout<<"enter salary of employee no. "<<i<<"\n"; cin>>sal[i]; } for(i=1;i<=n;i++) //loop for insertion sorting { tmp=sal[i]; j=i-1;

while(tmp<sal[j]) { sal[j+1]=sal[j]; j--; } sal[j+1]=tmp; } cout<<"\nSalaries after sorting are \n"; for(i=1;i<=n;i++) { cout<<sal[i]<<"\n"; } getch(); } Output: Enter number of employees 4 enter salary of employee no. 1 2100 enter salary of employee no. 2 3400 enter salary of employee no. 3 1400 enter salary of employee no. 4 3000 Salaries after sorting are 1400 2100 3000 3400 Q8. Write a program to store some employee numbers in an array and then accept an employee number and then search and display whether the given employee number is present in the array or not. If it, is present then display its position. (Use Binary search technique for searching) Solution: //Searching using Binary Search Technique #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int a[100],i,k=0,s,beg=0,end,mid,n; cout<<"\nEnter No. of Employees"; cin>>n; end=n-1; for(i=0;i<n;i++) { cout<<"Enter Employee No. of Employee "<<i+1<<"\n"; cin>>a[i]; } cout<<"\nEnter Employee No. to search"; cin>>s;

while(beg<=end) { mid=(beg+end)/2; if(a[mid]==s) { k=1; cout<<"Employee No.found at "<<mid+1<<" position"; break; } else if(s>a[mid]) beg=mid+1; else end=mid-1; } if(k==0) { cout<<"Employee No. not Found"; } getch(); } Output: Enter No. of Employees4 Enter Employee No. of Employee 1 1200 Enter Employee No. of Employee 2 1220 Enter Employee No. of Employee 3 1240 Enter Employee No. of Employee 4 1270 Enter Employee No. to search1220 Employee No.found at 2 position Q9. Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements which lie on diagonals. [Assuming the 2D Array to be a square matrix with odd dimension i.e. 33, 55, 77 etc.] Example, if the array content is 543 678 129 Output through the function should be : Diagonal One : 5 7 9 Diagonal Two : 3 7 1 Solution: #include<iostream.h> //inclusion of header file #include<conio.h> //inclusion of header file void showdiagonal(int a[100][100],int size) { int i,j; cout<<"\n\n\nDiagonal1 : \t"; for(i=0;i<size;i++)

{ cout<<a[i][i]<<"\t"; } cout<<"\n\n\n\n\nDiagonal Two :\t"; for(i=0;i<size;i++) { for(j=0;j<size;j++) { if((i+j)==(size-1)) { cout<<a[i][j]<<"\t"; } } } } void main() { clrscr(); int a[100][100],size,i,j; cout<<"\nEnter size of array (odd)"; cin>>size; if(size%2!=0) { cout<<"\nEnter values for array \n"; for(i=0;i<size;i++) { for(j=0;j<size;j++) { cout<<"Enter value of "<<i+1<<" row and "<<j+1<<" column cin>>a[i][j]; } } cout<<"\n Original array is \n"; for(i=0;i<size;i++) { for(j=0;j<size;j++) { cout<<a[i][j]<<"\t"; } cout<<"\n"; } showdiagonal(a,size); } else { cout<<"\nArray must have size odd "; } getch(); } Output: Enter size of array (odd)3

";

Enter values for array Enter value of 1 row and 1 column Enter value of 1 row and 2 column Enter value of 1 row and 3 column Enter value of 2 row and 1 column Enter value of 2 row and 2 column Enter value of 2 row and 3 column Enter value of 3 row and 1 column Enter value of 3 row and 2 column Enter value of 3 row and 3 column Original array is 11 12 13 14 15 16 17 18 19 Diagonal1 : 11 15 15 19 17

11 12 13 14 15 16 17 18 19

Diagonal Two : 13

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 354 769 218 Output through the function should be : Middle Row : 7 6 9 Middle Column : 5 6 1 Solution: #include<iostream.h> //inclusion of header file #include<conio.h> //inclusion of header file void show_mid(int a[100][100],int r,int c) { int i,k; k=r/2; cout<<"\n\n\nContents of Middle Row are\n"; for(i=0;i<c;i++) { cout<<a[k][i]<<"\t"; } cout<<"\n\n\n\n\nContents of Middle Column are \n"; for(i=0;i<r;i++) { cout<<a[i][k]<<"\t"; } } void main() { clrscr(); int a[100][100],r,c,i,j;

cout<<"\nEnter no. of rows and columns of array "; cin>>r>>c; cout<<"\nEnter values for array \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<"Enter value of "<<i+1<<" row and "<<j+1<<" column cin>>a[i][j]; } } cout<<"\nOriginal array is \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<a[i][j]<<"\t"; } cout<<"\n"; } show_mid(a,r,c); getch(); } Output:
Enter no. of rows and columns of array 3 3 Enter values for array Enter value of 1 row and 1 column Enter value of 1 row and 2 column Enter value of 1 row and 3 column Enter value of 2 row and 1 column Enter value of 2 row and 2 column Enter value of 2 row and 3 column Enter value of 3 row and 1 column Enter value of 3 row and 2 column Enter value of 3 row and 3 column Original array is 11 12 13 14 15 16 17 18 19 Contents of Middle Row are 14 15 16 Contents of Middle Column are 12 15 18 11 12 13 14 15 16 17 18 19

";

Q11. Write a program using a function SWAPARR( ) in C++ to swap(interchange) the first row elements with the last row elements, for a two dimensional integer array passed as the argument of the function. Example : If the two dimensional array contains : 5 6 3 2 1 2 4 9 2 5 8 1 9 7 5 8 After swapping of the content of first row and last row, it should be as follows:

9 1 2 5

7 2 5 6

5 4 8 3

8 9 1 2

Solution: #include<iostream.h> //inclusion of header file #include<conio.h> //inclusion of header file void swaparr(int a[100][100],int r,int c) { int i,j,tmp; for(j=0;j<c;j++) { tmp=a[0][j]; a[0][j]=a[r-1][j]; a[r-1][j]=tmp; } cout<<"\nAfter swapping first and last rows array is\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<a[i][j]<<"\t"; } cout<<"\n"; } } void main() { clrscr(); int a[100][100],r,c,i,j; cout<<"\nEnter no. of rows and columns of array "; cin>>r>>c; cout<<"\nEnter values for array \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<"enter value of "<<i+1<<" row and "<<j+1<<" column "; cin>>a[i][j]; } } cout<<"\nOriginal array is \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<a[i][j]<<"\t"; } cout<<"\n"; } swaparr(a,r,c);

getch(); } Output: Enter no. of rows and columns of array 3 3 Enter values for array enter value of 1 row and 1 column enter value of 1 row and 2 column enter value of 1 row and 3 column enter value of 2 row and 1 column enter value of 2 row and 2 column enter value of 2 row and 3 column enter value of 3 row and 1 column enter value of 3 row and 2 column enter value of 3 row and 3 column Original array is 1 2 3 4 5 6 7 8 9 After swapping first and last rows array is 7 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 be 10005 10 0 20 02040 0 15 0 00300 10 0 20 02040 10005 Solution: #include<iostream.h> #include<conio.h> void perform(int a[100],int size) { int b[100][100],r,c,k=0; for(r=0;r<size;r++) { for(c=0;c<size;c++) { if(r==c) { b[r][c]=a[k]; k=k+1; } else 1 2 3 4 5 6 7 8 9

{ b[r][c]=0; } } } k=size-1; for(r=0;r<size;r++) { for(c=0;c<size;c++) { if((r+c==(size-1))) { b[r][c]=a[k]; k=k-1; } } } cout<<"\nContents of array B is \n"; for(r=0;r<size;r++) { for(c=0;c<size;c++) { cout<<b[r][c]<<"\t"; } cout<<"\n"; } } void main() { clrscr(); int a[100],size,i; cout<<"\n enter the size of the array"; cin>>size; for(i=0;i<size;i++) { cout<<"\n enter the value of "<<i+1<< "number:"; cin>>a[i]; } perform(a,size); getch(); } OUTPUT: enter the size of the array5 enter the value of 1number:1 enter the value of 2number:2 enter the value of 3number:3 enter the value of 4number:4 enter the value of 5number:5 Contents of array B is 1 0 0 0 5 0 2 0 4 0

0 0 1

0 2 0

3 0 0

0 4 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,3 The resultant 2D array is The resultant 2D array is 100000 100 120000 120 123000 123 123400 123450 123456 Solution: #include<iostream.h> #include<conio.h> void perform(int a[100],int size) { int b[100][100],r=0,k=0,i,c; for(i=0;i<size;i++) { for(c=0;c<=size;c++) { b[r][c]=a[k]; k=k+1; } for(c=i+1;c<size;c++) { b[r][c]=0; } k=0; r=r+1; } cout<<"\nContents of array B is \n"; for(r=0;r<size;r++) { for(c=0;c<size;c++) { cout<<b[r][c]<<"\t"; } cout<<"\n"; } } void main() { clrscr(); int a[100],x,size,i; cout<<"\n enter the size of the array"; cin>>x; for(i=0;i<x;i++) { cout<<"\n enter the value of "<<i+1<< "number:";

cin>>a[i]; } perform(a,x); getch(); } OUTPUT: enter the size of the array3 enter the value of 1number:1 enter the value of 2number:2 enter the value of 3number:3 Contents of array B is 1 0 0 1 2 0 1 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<fstream.h> #include<conio.h> #include<stdio.h> 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<<"\n\n\nTotal No. of is="<<c; } void main() { ofstream H; H.open("DIALOGUE.TXT",ios::out); char s[80]; clrscr(); cout<<"\nEnter the text for Data File \n"; gets(s); H<<s; H.close(); perform(); getch();

} 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 type
struct 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<conio.h> #include<string.h> #include<fstream.h> #include<stdio.h> struct 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<<"\nEnter Name of Game"; gets(x.GameName);

cout<<"\nEnter Name Of Participant "; gets(x.Participant); H.write((char*)&x,sizeof(Game)); cout<<"\nPress 1 to enter more records else to stop "; cin>>ch; } while(ch==1); H.close(); COPYGAME( ); cout<<"\nContents Of BASKET.DAT is as follows:\n"; int c=0,i; ifstream GG; GG.open("BASKET.DAT",ios::binary|ios::in); while(!GG.eof()) { GG.read((char*)&x,sizeof(Game)); c=c+1; } GG.close(); ifstream G; G.open("BASKET.DAT",ios::binary|ios::in); for(i=1;i<(c-1);i++) { G.read((char*)&x,sizeof(Game)); cout<<"\n\nName of Game "<<x.GameName; cout<<"\nName Of Participant "<<x.Participant; } G.close(); getch(); } Output: Enter Name of GameBasket Ball Enter Name Of Participant Amar Press 1 to enter more records else to stop 1 Enter Name of GameCricket Enter Name Of Participant Dhanraj Press 1 to enter more records else to stop 1 Enter Name of GameBasket Ball Enter Name Of Participant Mukesh Press 1 to enter more records else to stop 2 Contents Of BASKET.DAT is as follows: Name of Game Basket Ball Name Of Participant Amar Name of Game Basket Ball Name Of Participant Mukesh Q16 Create a text file named FIRST.TXT which contains some text written into it, write a program using a function named vowelwords( ), that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to

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<stdio.h> #include<conio.h> 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<<s<<"\n"; } } A.close(); B.close(); } void main() {

clrscr(); ofstream X; X.open("FIRST.TXT",ios::binary|ios::out); char st[80]; cout<<"\nEnter Text to store in FIRST.TXT \n"; gets(st); X<<st; X.close(); vowelwords(); cout<<"\n\n\nContents of SECOND.TXT is as follows :\n"; ifstream Y; Y.open("SECOND.TXT",ios::binary|ios::in); while(!Y.eof()) { Y>>st; cout<<st<<" "; } getch(); } Output: Enter Text to store in FIRST.TXT Carry umbrella and overcoat when it rains

Contents of SECOND.TXT is as follows : umbrella and overcoat it

Q17. Create a binary file STUDENT.DAT which should contains the records of the following structure type: struct Student

{ 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<fstream.h> #include<conio.h> #include<stdio.h> 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<35) x.m1=35; if(x.m2<35) x.m2=35; if(x.m3<35) x.m3=35; B.write((char*)&x,sizeof(Student)); c=c+1; } A.close(); B.close(); remove("STUDENT.DAT"); rename("TEMP.DAT","STUDENT.DAT"); ifstream C; C.open("STUDENT.DAT",ios::in); cout<<"\n\nContents After Updating is as follows :"; cout<<"\nRNo.\tM1 \tM2 \tM3 \n"; for(int i=0;i<(c-1);i++) { C.read((char*)&x,sizeof(Student)); cout<<x.rno<<"\t"<<x.m1<<"\t"<<x.m2<<"\t"<<x.m3<<"\n"; } C.close();

} void main() { clrscr(); Student z; int ch=1,v=0; ofstream D; D.open("STUDENT.DAT",ios::out); while(ch==1) { cout<<"\nEnter rno of student "; cin>>z.rno; cout<<"\nEnter marks of 3 subjects"; cin>>z.m1>>z.m2>>z.m3; D.write((char*)&z,sizeof(Student)); v=v+1; cout<<"\nPress 1 to enter more records else to stop"; cin>>ch; } D.close(); ifstream E; E.open("STUDENT.DAT",ios::in); cout<<"\n\nContents Before Updating is as follows :"; cout<<"\nRNo.\tM1 \tM2 \tM3 \n"; for(int i=0;i<v;i++) { E.read((char*)&z,sizeof(Student)); cout<<z.rno<<"\t"<<z.m1<<"\t"<<z.m2<<"\t"<<z.m3<<"\n"; } E.close(); perform(); getch(); } Output: Enter rno of student 1 Enter marks of 3 subjects45 23 56 Press 1 to enter more records else to stop1 Enter rno of student 2 Enter marks of 3 subjects87 56 11 Press 1 to enter more records else to stop1 Enter rno of student 3 Enter marks of 3 subjects67 89 90 Press 1 to enter more records else to stop2 Contents Before Updating is as follows : RNo. M1 M2 M3 1 45 23 56 2 87 56 11 3 67 89 90 Contents After Updating is as follows : RNo. M1 M2 M3

1 2 3

45 87 67

35 56 89

56 35 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 values int 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( ) in C++ copy all those records having AreaCode as 123 from TELEPHON.DAT to TELEBACK.DAT. Solution: #include<conio.h> #include<string.h> #include<fstream.h> #include<stdio.h> class Directory { private: char Name[20]; char Address[30]; char AreaCode[5]; char phone_No[15]; public: void Register( ) { cout<<"\nEnter Name of customer"; gets(Name); cout<<"\nEnter Address of customer "; gets(Address); cout<<"\nEnter Area code"; gets(AreaCode); cout<<"\nEnter Phone number"; gets(phone_No); } void Show( ) { cout<<"\n\nName of customer "<<Name; cout<<"\nAddress of customer "<<Address; cout<<"\nArea code "<<AreaCode; cout<<"\nPhone number "<<phone_No; } int checkcode(char AC[]) { return strcmp(AreaCode,AC); } };

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)); cout<<"\nPress 1 to enter more records else to stop "; cin>>ch; } while(ch==1); H.close(); COPYABC( ); cout<<"\nContents Of Teleback.dat is as follows:\n"; int c=0,i; ifstream GG; GG.open("TELEBACK.DAT",ios::binary|ios::in); while(!GG.eof()) { GG.read((char*)&x,sizeof(x)); c=c+1; } GG.close(); ifstream G; G.open("TELEBACK.DAT",ios::binary|ios::in); for(i=1;i<(c-1);i++) { G.read((char*)&x,sizeof(x)); x.Show(); } getch();

} Output: Enter Name of customerAmar Enter Address of customer Patna Enter Area code123 Enter Phone number123456 Press 1 to enter more records else to stop 1 Enter Name of customerSuresh Enter Address of customer Khagaul Enter 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 code123 Enter 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 Patna Area code 123 Phone number 123456 Name of customer Wilson Address of customer Danapur Area code 123 Phone number 789656 Q19. Write a program to store, delete and display dynamically allocated Queue containing nodes of the following 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<iostream.h> #include<conio.h> #include<process.h> #include<stdio.h> #include<string.h> struct Node { int Itemno; char Itemname[20];

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<<"\nEnter information for the new node..."; cout<<"\nEnter Itemno."; cin>>Itemno; cout<<"\nEnter name "; gets(Itemname); newptr=Create_New_Node(Itemno,Itemname); if(newptr==NULL) { cout<<"\nCannot create new node!!! Aborting!!\n"; exit(1); } push_at_end(newptr); cout<<"\nNow the Queue is:\n"; Display(top); cout<<"\nPress Y to enter more nodes, N to exit..."; cin>>ch; } clrscr(); do { cout<<"\nThe Queue now is :\n"; Display(top); cout<<"\nWant to pop an element? (Y/N)..."; cin>>ch; if(ch=='Y' || ch=='y') pop(); } while(ch=='y' || ch=='Y'); } Node* Create_New_Node(int ino, char nm[]) { ptr=new Node; ptr->Itemno=ino; strcpy(ptr->Itemname,nm); ptr->Link=NULL; return ptr; } void push_at_end(Node *np)

{ if(top==NULL) { top=np; rear=np; } else { rear->Link=np; rear=np; } } void Display(Node *np) { while(np!=NULL) { cout<<np->Itemno<<" "<<np->Itemname<<"\n"; np=np->Link; } cout<<"!!!\n"; } void pop() { if(top==NULL) cout<<"UNDERFLOW !!!\n"; else { ptr=top; top=top->Link; delete ptr; } } Output: Enter information for the new node... Enter Itemno.1 Enter name Lux Now the Queue is: 1 Lux !!! Press Y to enter more nodes, N to exit...y Enter information for the new node... Enter Itemno.2 Enter name Liril Now the Queue is: 1 Lux 2 Liril !!! Press Y to enter more nodes, N to exit...n

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 allocated Stack of Books implemented with 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<iostream.h> #include<conio.h> #include<process.h> #include<stdio.h> #include<string.h> 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<<"\nEnter information for the new node..."; cout<<"\nEnter Book No.";

cin>>n; cout<<"\nEnter Book Name "; gets(bn); newptr=Create_New_Node(n,bn); if(newptr==NULL) { cout<<"\nCannot create new node!!! Aborting!!\n"; exit(1); } push(newptr); cout<<"\nNow the Stack is:\n"; Display(top); cout<<"\nPress Y to enter more nodes, N to exit..."; cin>>ch; } clrscr(); do { cout<<"\nThe Stack now is :\n"; Display(top); cout<<"\nWant to pop an element? (Y/N)..."; cin>>ch; if(ch=='Y' || ch=='y') pop(); }while(ch=='y' || ch=='Y'); } Book* Create_New_Node(int n,char nm[]) { ptr=new Book; ptr->Bno=n; strcpy(ptr->Bname,nm); ptr->Next=NULL; return ptr; } void push(Book *np) { if(top==NULL) { top=np; } else { save=top; top=np; np->Next=save; } }

void Display(Book *np) { while(np!=NULL) { cout<<np->Bno<<"\t"<<np->Bname<<"\n"; np=np->Next; } cout<<"!!!\n"; } void pop() { if(top==NULL) cout<<"UNDERFLOW !!!\n"; else { ptr=top; top=top->Next; delete ptr; } } Output: Enter information for the new node... Enter Book No.1 Enter Book Name Himmat Now the Stack is: 1 Himmat !!! Press Y to enter more nodes, N to exit...y Enter information for the new node... Enter Book No.2 Enter Book Name Bhagya Now the Stack is: 2 Bhagya 1 Himmat !!! Press Y to enter more nodes, N to exit...n The Stack now is : 2 Bhagya 1 Himmat !!! Want to pop an element? (Y/N)...y The Stack now is :

1 !!!

Himmat

Want to pop an element? (Y/N)...n Q21. Write a program using arrays to implement STACK 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 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 <iostream.h> //inputs the header files #include <process.h> #include <conio.h> 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<<"\nOverflow!! STACK is Full"; } else { r=r+1; arr[r]=n; } } void stk::pop() { int i; if(r<0) { cout<<"\nStack is Empty. UNDERFLOW!!!"; } else { cout<< "\nNo. deleted is "<<arr[r]; arr[r]=NULL; r=r-1; } }

void stk::display() { if (r==-1) { cout<<"Stack is Empty"; } else { for(int i=0;i<=r;i++) { cout<<arr[i]<<"\t"; } } } void main() //begin of main function { int ch,n; clrscr(); stk ob; while(1) { cout<<"\n\n\n\n1.Insert"; cout<<"\n2.Delete"; cout<<"\n3.Display"; cout<<"\n4.Exit"; cout<<"\nEnter Choice: "; cin>>ch; switch(ch) //begin of switch case { case 1: cout<<"\nEnter No. to insert in Stack "; cin>>n; ob.push(n); break; case 2: ob.pop(); break; case 3: ob.display(); break; case 4: exit(0); default: cout<<"\nWrong Choice"; } } //end of while } OUTPUT: 1.Insert 2.Delete 3.Display

4.Exit Enter Choice: 1 Enter No. to insert in Stack 11 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 1 Enter No. to insert in Stack 22 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 2 No. deleted is 22 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 3 11 1.Insert 2.Delete 3.Display 4.Exit Enter 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 queue iii) display() to display the element of queue. Solution: //PROGRAM TO IMPLEMENT THE QUEUE// #include <iostream.h> //inputs the header files #include <process.h> #include <conio.h> class que //begin of class { public: int arr[100]; int r; que() //constructor {

};

r=-1; } void push(int); void pop(); void display(); //end of class

void que::push(int n) { if (r>98) { cout<<"\nQueue Overflow!! Queue is Full"; } else { r=r+1; arr[r]=n; } } void que::pop() { int i; if (r<0) { cout<<"\nQueue is Empty. UNDERFLOW!!!"; } else { cout<< "\nNo. deleted is "<<arr[0]; for(i=0;i<r;i++) { arr[i]=arr[i+1]; } arr[r]=NULL; r=r-1; } } void que::display() { if (r==-1) { cout<<"Queue is Empty"; } else { for (int i=0;i<=r;i++) { cout<<arr[i]<<"\t"; } } }

void main() //begin of main function { int ch,n; clrscr(); que ob; while(1) { cout<<"\n1.Insert"; cout<<"\n2.Delete"; cout<<"\n3.Display"; cout<<"\n4.Exit"; cout<<"\nEnter Choice: "; cin>>ch; switch(ch) //begin of switch case { case 1: cout<<"\nEnter No. to insert in Queue "; cin>>n; ob.push(n); break; case 2: ob.pop(); break; case 3: ob.display(); break; case 4: exit(0); default: cout<<"\nWrong Choice"; } } //end of while } Output: 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 1 Enter No. to insert in Queue 11 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 1 Enter No. to insert in Queue 22 1.Insert 2.Delete 3.Display

4.Exit Enter Choice: 2 No. deleted is 11 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 3 22 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 4 Q23. Write a program using arrays to implement CIRCULAR 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 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 <iostream.h> //inputs the header files #include <process.h> #include <conio.h> 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<<"\nQueue Overflow!! Queue is Full"; } else if(rear==-1) { front=rear=0; } else if(rear==size-1) rear=0;

else rear++; arr[rear]=n; } void cirque::del() { int i; if (front==-1) { cout<<"\nQueue is Empty"; } else { cout<< "\nNo. deleted is "<<arr[front]; arr[front]=NULL; if(front==rear) front=-1; rear=-1; elseif(front==(size-1)) front=0; else front++; } } void cirque::display() { int i=0; if (rear==-1) { cout<<"Queue is Empty"; } else if(rear>=front) { for (i=0;i<front;i++) { cout<<"- "; } for(i=front;i<=rear;i++); { cout<<arr[i]<<" "; } } else { for(i=0;i<=rear;i++) cout<<arr[i]; for(;i<front;i++) cout<<" "; for(i=front;i<size;i++) cout<<arr[i]<<" "; }

} void main() //begin of main function { int ch,n; clrscr(); cirque ob(5); while(1) { cout<<"\n1.Insert"; cout<<"\n2.Delete"; cout<<"\n3.Display"; cout<<"\n4.Exit"; cout<<"\nEnter Choice: "; cin>>ch; switch(ch) //begin of switch case { case 1: cout<<"\nEnter No. to insert in Circular Queue "; cin>>n; ob.insert(n); break; case 2: ob.del(); break; case 3: ob.display(); break; case 4: exit(0); default: cout<<"\nWrong Choice"; } } } Output: 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 1 Enter No. to insert in Queue 11 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 1 Enter No. to insert in Queue 22 1.Insert

2.Delete 3.Display 4.Exit Enter Choice: 2 No. deleted is 11 1.Insert 2.Delete 3.Display 4.Exit Enter Choice: 3 22 1.Insert 2.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<iostream.h> #include<conio.h> #include<process.h> #include<stdio.h> struct Student { long RollNo; char Name[20]; int m1,m2,m3; }; void main() { clrscr(); Student x[10]; int i,c; for(i=0;i<10;i++) { cout<<"\nEnter value for Student "<<i+1<<"\n"; cout<<"\nEnter Roll No. "; cin>>x[i].RollNo; cout<<"\nEnter Name "; gets(x[i].Name); cout<<"\nEnter marks of 3 subjects "; cin>>x[i].m1>>x[i].m2>>x[i].m3; } cout<<"\n\nList of students whom are failed in more than 1 subjects are \n"; for(i=0;i<10;i++) { c=0;

if(x[i].m1<33) c=c+1; if(x[i].m2<33) c=c+1; if(x[i].m3<33) c=c+1; if(c>1) { cout<<x[i].RollNo<<"\t"<<x[i].Name<<"\t"<<x[i].m1<<"\t"<<x[i].m2<<"\t"<<x[i].m3<<"\n"; } } getch(); } Output: Enter value for Student 1 Enter Roll No. 1 Enter Name Akash Enter marks of 3 subjects 87 79 56 Enter value for Student 2 Enter Roll No. 2 Enter Name Suresh Enter marks of 3 subjects 32 12 67 Enter value for Student 3 Enter Roll No. 3 Enter Name Watson Enter marks of 3 subjects 67 34 79 List of students whom are failed in more than 1 subjects are 2 Suresh 32 12 67 Q25. Write a program using function SUMFUN() having parameters X (of type double) and n (of type integer) with a return type as double to find the sum of the series given below :
X + X2/3! + X3/5! + .+ Xn/(2N-1)!

Solution: //Finding sum of series by accepting arguments in function and return a value #include <iostream.h> #include <conio.h> #include<math.h> double SUMFUN(double X,int n) { int i,j,f; double s=0.0; for (i=1;i<=n;i++) { f=1; for(j=1;j<=((2*i)-1);j++) { f=f*j; } s=s+(pow(X,i)/f); }

return(s); } void main() { double X,ans; int n; clrscr(); cout<<"\nEnter the value of X and n "; cin>>X>>n; ans=SUMFUN(X,n); cout<<"\nSum of series="<<ans; getch(); } Output: Enter the value of X and n 3 2 Sum of series=4.5 SECTION B Q1. Write the SQL commands for (i) to (iv) and outputs for (v) to (viii) on the basis of tables BOOKS and ISSUES. Table: BOOKS Book_ID BookName AuthorNam Publisher Price Qty e L01 Maths Raman ABC 70 20 L02 Science Agarkar DEF 90 15 L03 Social Suresh XYZ 85 30 L04 Computer Sumita ABC 75 7 L05 Telugu Nannayya DEF 60 25 L06 English Wordsworth DEF 55 12 Table: ISSUES Book_ID Qty_Issued L02 13 L04 5 L05 21 (i) To show Book name, Author name and Price of books of ABC publisher. (ii) To display the details of the books in descending order of their price. (iii) To decrease the Qty_Issued from ISSUES table by 3 (all rows must decrease). (iv) To display the Book Id, Book name, Publisher, Price, Qty, Qty_Issued from both the tables with their matching Book ID. (v) SELECT sum(price) FROM Books WHERE Publisher = DEF; (vi)SELECT Publisher, min(price) FROM Books GROUP BY Publisher; (vii)SELECT Price from Books, Issues where Books.Book_ID=Issues.Book_ID AND Qty_Issued=5; (viii)SELECT Count(Distinct Publisher) FROM Books;

Solution:
SQL> create table BOOKS(Book_ID varchar2(3),BookName varchar2(15),AuthorName varchar2(20),Publisher varchar2(15),Price number(2),Qty number(2)); Table created. SQL> insert into BOOKS values('L01','Maths','Raman','ABC',70,20); 1 row created.

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 ----------------- ---------------------------------L02 Science Agarkar DEF L03 Social Suresh XYZ L04 Computer Sumita ABC L01 Maths Raman ABC L05 Telegu Nannayya DEF L06 English Wordworth DEF 6 rows selected. SQL> update ISSUES set Qty_Issued=Qty_Issued-3; 3 rows updated. SQL>select BOOKS.Book_Id,BookName,Publisher,Price,Qty,Qty_Issued from BOOKS,ISSUES where BOOKS.Book_Id=ISSUES.Book_ID BOO BOOKNAME PUBLISHER PRICE QTY QTY_ISSUED ----------------- --------------- ---------- ------------------L02 Science DEF 90 15 10 L04 Computer ABC 75 7 2 L05 Telegu DEF 60 25 18 SQL> select sum(price) from Books where Publisher='DEF'; SUM(PRICE) ---------205 PRICE ---------90 85 75 70 60 55 QTY ---------15 30 7 20 25 12

(iii) (iv)

(v)

(vi)

SQL> select Publisher,min(Price) from Books GROUP BY Publisher; PUBLISHER MIN(PRICE) -----------------------ABC 70 DEF 55 XYZ 85 SQL> select price from Books,Issues where Books.Book_ID=Issues.Book_ID AND Qty_Issued=5; no rows selected

(vii) (viii)

SQL> select count(Distinct Publisher) from Books; COUNT(DISTINCTPUBLISHER) -----------------------3 Q2. Consider the following tables EMPLOYEES and EMPSALARY. Write SQL commands for the statements (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. Paris 215 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 and 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 where DESIGNATION=Salesman AND EMPLOYEES.EMPID=EMPSALARY.EMPID; (vi) SELECT COUNT(DISTINCT DESIGNATION) FROM EMPSALARY; (vii) SELECT DESIGNATION,SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION HAVING COUNT(*)>2; (viii) SELECT SUM(BENEFIT) FROM EMPLOYEES WHERE DESIGNATION=Clerk;

Solution:
SQL> create table EMPLOYEES(EMPID NUMBER(3),FIRSTNAME VARCHAR2(10),LASTNAME VARCHAR2(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), designation varchar2(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

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 Howard 335 Henry Williams 12 Moore Street Boston 244 Manila Sengupta 24 Friends Street New Delhi 105 Mary Jones 842, Vine Ave Losantiville 441 Peter thompson 11 Red Road Paris 400 Rachel Lee 121 Harrison St. New York 300 Robert Samuel 9 Fifth Cross Washington 152 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 80000 Sarah Ackerman 87500 (iv) SQL> select designation,min(salary) from empsalary group by designation having designation = 'Manager' or designation='Clerk'; DESIGNATIO MIN(SALARY) -------------------Clerk 40000 Manager 65000 (v) SQL>SELECT FIRSTNAME,SALARY FROM EMPLOYEES, EMPSALARY where DESIGNATION= 'Salesman' AND EMPLOYEES.EMPID=EMPSALARY.EMPID; FIRSTNAME SALARY ------------------Rachel 32000 Peter 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'

* 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 IC301 IC799 MC101 IC302 AM812 IC899 AM501 MU499 IC701 STARTING MUMBAI BANGALORE INDORE DELHI KANPUR MUMBAI DELHI MUMBAI DELHI ENDING DELHI DELHI MUMBAI MUMBAI BANGALORE KOCHI TRIVANDRUM MADRAS AHMEDABAD TABLE : FARES FL_NO 1C701 MU499 AM501 IC899 1C302 1C799 MC101 i. ii. iii. iv. v. vi. AIRLINES Indian Airlines Sahara Jet Airways Indian Airlines Indian Airlines Indian Airlines Deccan Airlines FARE 6500 9400 13450 8300 4300 10500 3500 TAX% 10 5 8 4 10 10 4 NO_FLIGHTS 8 2 3 8 3 1 1 3 4 NO STOPS 0 1 0 0 1 4 5 3 0

Display FL_NO and NO_FLIGHTS from KANPUR to BANGALORE from the table FLIGHTS. Arrange the contents of the table FLIGHTS in the ascending order of FL_NO. Display the FLNO and fare to be paid for the flights from DELHI to MUMBAI using the tables FLIGHTS and FARES, where the fare to be paid = FARE +FARE*TAX%/100. Display the minimum fare Indian Airlines is offering from the table FARES. SELECT FL_NO, NO_FLIGHTS, AIRLINES from FLIGHTS, FARES where STARTING=DELHI and FLIGHTS.FL_NO=FARES.FL_NO. SELECT count (distinct ENDING) from FLIGHTS.

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' OR SenderID=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

(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),CneeAddress varchar2(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');

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 Kohli S Kaur (ii) SQL> select CneeID,CnorName,CnorAddress,CneeName,CneeAddress from Consignors C, Consignee E where 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 --------------------- -------------------KO19 MU15 A P Roy 2A, Central Avenue ND48 MU50 B P Jain 13, Block D,A Vihar ND08 ND02 P Dhingra 16/J, Moore Enclave MU05 ND01 Rahul Kishore 5,ParkAvenue MU32 ND02 S Mittal P245,AB Colony CNEECITY --------------Kolkata New Delhi New Delhi Mumbai 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

--------------R Singhal Amit Kumar

--------------Rahul Kishore 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.CnorlD AND 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 Enclave B 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

S-ar putea să vă placă și