Final Comp Prac Print
Final Comp Prac Print
BY ANDREA THOMAS
GRADE 12 A
2019 - 2020
_________________ _________________
TEACHER IN CHARGE EXTERNAL
EXAMINER
2
INDEX
S. NO. CONTENT PAGE NO.
1 REVISION OF C++ 4
2 STRUCTURED QUERY 31
LANGUAGE
3 CLASSES AND OBJECTS 48
4 FUNTION OVERLOADING 74
AND CONSTRUCTORS AND
DESTRUCTORS
5 FILE HANDLING 82
6 ARRAYS 104
7 STACKS, QUEUES AND 119
LINKED LISTS
REVISION OF C++
3
1. Write a menu driven program using functions to do the following:-
Sum of prime numbers from 2 to N
Sum of first N prime numbers
#include<iostream.h>
#include<conio.h>
for(int i=2;i<=m;i++)
f=0;
for(int j=1;j<=i;j++)
if(i%j==0)
f++;
4
}
if(f==2)
s=s+i;
for(int i=2;;i++)
f=0;
for(int j=1;j<=i;j++)
if(i%j==0)
f++;
if(f==2)
s=s+i;
c++;
if(c==l)
5
break;
void main()
int n, ch;
char ans;
cin>>n; cout<<"\n";
do{
cout<<"\n";
cout<<"Displaying menu;-\n";
cin>>ch; cout<<"\n";
switch(ch)
case 1:
sumone(n);
break;
case 2:
6
sumtwo(n);
break;
default:
break;
cin>>ans;
}while(ans=='y');
getch();
2. Write a program in C++ to accept a 1 – D array of type int with N number of elements,
write separate functions for the following ;-
To print the count of Armstrong numbers in the array
To return the count of numbers which are palindromes
To print the factorial of the smallest number in the array
To print the sum of all the multiples of 5
To shift all the even numbers to the left and odd numbers to the right
#include<iostream.h>
7
#include<conio.h>
int l,c=0,r,s=0;
for(int i=0;i<n;i++)
s=0;
l=a[i];
while(l>0)
r=l%10;
s=s+r*r*r;
l=l/10;
if(s==a[i])
c++;
int l,c=0,r,s=0;
for(int i=0;i<n;i++)
8
{
s=0;
l=a[i];
while(l>0)
r=l%10;
s=s*10+r;
l=l/10;
if(s==a[i])
c++;
for(int i=0;i<n;i++)
if(a[i]<small)
small=a[i];
for(int i=1;i<=small;i++)
9
{
f=f*i;
int s=0;
for(int i=0;i<n;i++)
if(a[i]%5==0)
s=s+a[i];
for(int i=0;i<n;i++)
if(a[i]%2==0)
j++;
10
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
void main()
char ans;
cin>>n; cout<<"\n";
for(int i=0;i<n;i++)
cin>>a[i];
do{
cout<<"\n";
cout<<"Displaying menu;-\n";
cout<<"\t Enter 1 to print the count of Armstrong numbers in the array \n";
cout<<"\t Enter 2 To return the count of numbers which are palindromes \n";
11
cout<<"\t Enter 3 To print the factorial of the smallest number in the array \n";
cout<<"\t Enter 5 To shift all the even numbers to the left and odd numbers to the right \n";
cin>>ch; cout<<"\n";
switch(ch)
case 1:
armstrong(n,a);
break;
case 2:
palin(n,a);
break;
case 3:
factorial(n,a);
break;
case 4:
multiples(n,a);
break;
case 5:
swap(n,a);
break;
default:
12
cout<<"Enter valid choice\n";
break;
cin>>ans;
}while(ans=='y');
getch();
13
Swap the first half of the array with the second half, i.e. the first element
should be swapped with the middle value and so on.
#include<iostream.h>
#include<conio.h>
char found='n';
cin>>num; cout<<"\n";
for(i=0;i<n;i++)
if(num==a[i])
pos=i+1;
found='y';
break;
if(found=='n')
14
void reverse(int n, int a[])
int i,j,temp;
for(i=0,j=n-1;i<n/2;i++,j--)
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(j=0;j<n;j++)
cout<<a[j]<<" ";
int t,j,i;
if(n%2==0)
j=n/2;
else
j=n/2+1;
for(i=0;i<n/2;i++,j++)
15
{
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
void main()
char ans;
cin>>n; cout<<"\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"\n";
cout<<"Displaying menu;-\n";
cout<<"\t Enter 3 to swap the first half of the array with the second half, i.e. the first element
16
should be swapped with the middle value and so on. \n";
do{
cin>>ch; cout<<"\n";
switch(ch)
case 1:
linearsearch(n,a);
break;
case 2:
reverse(n,a);
break;
case 3:
swap(n,a);
break;
default:
break;
cin>>ans;
}while(ans=='y');
getch();
17
}
18
getch();
}
5. WAF to check if the entered number is prime or not. If the number is prime, return 1
else return 0.Place the function after the main. Write the prototype as well.
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
int prime(int a);
cout<<"enter a\n";
cin>>a;
cout<<prime(a);
getch();
}
int prime(int &x)
{
int i, count=0;
for(i=1;i<=x; i++)
{
if(x % i==0)
count++;
}
if(count==2)
return 1;
else
return 0;
}
19
6. Write a program to input radius of a circle and calculate circumference, diameter and area
of circle. User defined functions should be used to do the calculations.
#include<iostream.h>
#include<conio.h>
float circumference(float);
float diameter(float);
float area(float);
void main()
{
float r, circum;
float dia, ar;
cout<<”Enter radius”;
cin>>r;
circum=circumference( r);
cout<<”circumference”<<circum;
dia=diameter(r);
cout<<”diameter”<<dia;
ar=area(r);
cout<<”area”<<ar;
getch();
}
float circumference(float rad)
{
int cir=0;
cir=2*rad;
return cir;
}
float diameter(float rad)
{
float d;
d=2*3.14* rad;
return d;
}
20
float area(float rad)
{
float a;
a=3.14*rad*rad;
return a;
}
21
8. Write a program using user defined function to calculate sum of the series-
-2x2/3! + 4x4/5! -…………2nx 2n/(2n+1)!. The function should accept x and n as parameters
and return the sum.
#include<iostream.h>
#include<conio.h>
int fact(int);
double sumseries(int, int);
void main()
{
int x1,n1;
double sum=0;
cout<<”Enter value for x and n”;
cin>>x1>>n1;
sum=sumseries(x1,n1);
cout<<”sum of series”<<sum;
getch();
}
double sumseries(int x, int n)
{
double s=0;
int i, t=-1;
for(i=2;i<=n;i+=2)
{
s=s + (t*i*(double)pow(x, i))/fact(i+1);
t=t*(-1);
}
return s;
}
int fact(int n3)
{
int i, f=1;
for(i=1;i<=n3;i++)
f=f*i;
22
return f;
}
9. Write a menu driven program using user defined functions to do the following operations :
- Display multiplication table
- Calculate factorial of a number
- Calculate divisors of a number. Call by reference
- Calculate reverse of a number(return type long)
- To check if a number is prime or not. Return ‘y’ if prime and ‘n’ if not prime.
#include<iostream.h>
#include<conio.h>
void mult(int);
long fact(int);
void divisor(int);
long reverse(int);
char checkprime(int);
void main()
{
int ch, n;
long res;
char sol;
char ans;
do
{
cout<<"Enter the number ";
cin>>n;
cout<<"1:Display multiplication table\n";
cout<<"2:Calculate factorial of a number\n";
cout<<"3:Calculate reverse of a number\n";
cout<<"4:To check if a number is prime or not\n";
cout<<"5:Calculate divisors of a number\n";
cout<<"Enter choice\n";
cin>>ch;
switch(ch)
{
23
case 1: mult(n);
break;
case 2: res=fact(n);
cout<<"\nResult = "<<res<<"\n";
break;
case 3: res=reverse(n);
cout<<"\nreversed no = "<<res<<"\n";
break;
case 4: sol=checkprime(n);
if(sol=='y')
cout<<"\nThe number is a prime no\n";
else
cout<<"\nThe number is not prime\n";
break;
case 5: divisor(n);
break;
}
cout<<"Want to continue\n";
cin>>ans;
}while(ans=='y' || ans=='Y');
getch();
}
void mult(int num)
{
int i;
for(i=1;i<=10;i++)
{
cout<<num<<'*'<<i<<'='<<num*i<<'\n';
}
}
long fact(int num)
{
int i;
long f=1;
for(i=1;i<=num; i++)
f=f*i;
return f;
}
void divisor(int num)
{
24
int i;
cout<<" The divisors are - ";
for(i=2;i<num; i++)
if(num % i==0)
cout<<i<<' '<<"\n";
}
long reverse(int num)
{
long r=0;
int v;
while(num>0)
{
v=num%10;
r=r*10 + v;
num=num/10;
}
return r;
}
char checkprime(int num)
{
int i, f = 1;
if (num==0 || num==1)
return 'n';
for (i=2; i<num; i++)
{
if(num % i==0)
{
f=0;
break;
}
}
if(f==0)
return 'n';
else
return 'y';
}
25
26
10.Define a function Bintodec() which will take a long integer (in binary format) as parameter
and will return equivalent Decimal of long data type.
#include<iostream.h>
#include<conio.h>
#include<math.h>
long bintodec(long bnum)
{
long dnum=0;
int r, ctr=0;
while(bnum !=0)
{
r=bnum%2;
dnum+=r*pow(2,ctr);
ctr++;
bnum/=10;
}
cout<<"\ndecimal number: "<<dnum;
}
void main()
{
int num;
cout<<"Enter number\n";
27
cin>>num;
bintodec(num);
getch();
}
11. Write a program to input value in seconds and convert to its equivalent hours, minutes
and seconds using call by reference.
#include<iostream.h>
#include<conio.h>
void convert(int &s)
{
int h, m;
h=s/3600;
s=s%3600;
m=s/60;
s=s%60;
cout<<"hours: "<<h<<"\nminutes: "<<m<<"\nseconds: "<<s;
}
void main()
{
int s1;
cout<<"Enter time in seconds\n";
cin>>s1;
convert(s1);
getch();
}
28
29
SQL
30
WORKSHEET 1:
RELATION : STUDENT1
Rollno Name Stipend Stream Avgmarks Grade Class
31
QUERIES;
Q1. Display list of all commerce streamed students from the table.
Q2. Display names of students who are in class 12 sorted by stipend in ascending order.
Q3.Display details of students in the descending order of their Avgmarks and if two
students have the same Avgmarks, display in the ascending order of their class.
Q4. Display name, stream and stipend received in a year assuming stipend is paid every
month.
32
Q5. Display details of students whose name starts from ‘V’.
33
Worksheet 2
Relation : PREPAID3
CREATE TABLE PREPAID3(NO INT(3), cname varchar(20), model varchar(20), act_date date,
validity int(4), msp varchar(20), amount int(9));
34
1) To display Cname and Amount of customers having “Nokia” model and “Hutch” MSP.
4) To modify the record of LG Clients by increasing the amount by 100 and validity by 20
days.
Worksheet 3
35
Relation: INSURANCE
Company Type Years Name Coverage
1. Give the name and type of all companies whose coverage is more than 100000 but
36
less than 700000.
2. List the name, company and years of all those whose type is life and years are more
than 20.
select name, company, years from insurance where type=”life” and years>20;
37
5. List the name in descending order of coverage.
7. Display the details of all companies whose insurance years range from 20 to 30
years.
Insert command;
Worksheet 4
Relation : EMPLOYEE
Eno Ename Designation Department
38
MG01 Arina Manager Logistics
39
2. Display name of employees who are Clerk
Relation: ACCOUNT
40
Eno Basic DA Tax
41
2. To display Ename and Net_Income of all persons (Net_Income = Basic + DA - tax).
3. To display Ename and Basic of all persons whose basic is greater than 7000.
select e.ename, a.basic from account a, employee e where a.basic>7000 and e.eno=a.eno;
(“OF03”,”Ruban”,”Officer”,”Sales”).
42
6. To increment the value of DA by 25% of the existing value for all employees whose
Worksheet 5:
Relation: MOVIE
Movieid Moviena Actor Type Cost Languag Number
me e
43
INSERT INTO MOVIE VALUES(“A115”,”Bruce Almighty”,”Jim
Carrey”,”Comedy”,250,”English”,4);
Relation: ISSUED
Movieid Qty_Issued
A005 2
R001 1
A115 3
44
1. To display moviename, actor and cost of all English movies.
45
6. To display the number of movies issued along with their name.
46
CLASSES AND OBJECTS
47
Q1. Declare a class ITEM with the following members:-
Private members;
Public member:
Input() - To input details ( Item_no, Item_name, Qty, Price_per_unit ) and then invoke
calculate() to assign the amount.
Ans;
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class ITEM{
private:
int item_no;
char item_name[30];
int qty;
int amt;
void calculate()
48
{
amt=qty*ppu;
public:
void input();
void display();
}I[30];
void ITEM::input()
cin>>item_no;
gets(item_name);
cout<<"Enter quantity\n";
cin>>qty;
cin>>ppu;
calculate();
void ITEM::display()
49
}
void main()
int n;
cin>>n;
for(int i=0;i<n;i++)
I[i].input();
I[i].display();
getch();
50
Q2. Declare a class student with the following members
PRIVATE MEMBERS;
Calgrade() – To calculate the total_marks and percentage. The function must assign grade
based on the following criteria.
PERCENTAGE GRADE
90-100 A1
80-89 A2
70-79 B1
60-69 B2
40-59 C
<40 D
PUBLIC MEMBERS-
ANS.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class student{
private:
char name[30];
int rollno;
int marks[3];
51
float p; //percentage
char grade[5];
void calgrade()
if(p>=90&&p<=100)
strcpy(grade,"A1");
else if(p>=80&&p<=89)
strcpy(grade,"A2");
else if(p>=70&&p<=79)
strcpy(grade,"B1");
else if(p>=60&&p<=69)
strcpy(grade,"B2");
else if(p>=40&&p<=59)
strcpy(grade,"C");
else if(p<40)
strcpy(grade,"D");
public:
void input();
void display();
};
void student::input()
52
float s=0;
cout<<"Enter name\n";
gets(name);
cin>>rollno;
cout<<"Enter marks\n";
for(int i=1;i<6;i++)
cout<<”Enter mark\n”;
cin>>marks[i];
s=s+marks[i];
p=(s/500)*100;
calgrade();
void student::display()
puts(name);
void main()
53
{
student s;
s.input();
s.display();
getch();
54
int accno;
char n[20];
float balance;
public:
void getdata()
{
cout<<“enter name, account no and balance”;
gets(n);
cin>>accno>>balance;
}
void transaction(float amt, char type)
{
if (type==’D’ || type == ‘d’)
balance+=amt;
if (type==’W’ || type==‘w’)
balance-=amt;
}
void showdata()
{
cout<<"Account no - "<<accno<<"\nBalance - "<<balance;
}
};
void main()
{
account acc1;
float tamt=0;
char type;
acc1.getdata();
cout<<”Enter amount for transaction ”;
cin>>tamt;
cout<<”Enter transaction type(‘d’ or ‘w’)”;
cin>>type;
acc1.transaction (tamt, type);
acc1.showdata();
getch();
}
55
3.
Write a function to declare class salesman with private members salesman number, sales
made in 4 regions, target, total_sales, commission.
Accept details. Calculate total sales made by adding sales of 4 regions. If total sales are more
than target then commission is 15%.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class salesman
{
int salesman_no;
int regionsale[4];
float target, tot_sale, comm;
void calc();
public:
void getdata();
void display();
};
void salesman::getdata()
{
cout<<"Enter salesman no. ";
cin>>salesman_no;
cout<<"enter target sale";
cin>>target;
calc();
56
}
void salesman::calc()
{
tot_sale=0;
cout<<”Enter region sale\n”;
for(int i=0; i<4; i++)
{
cin>>regionsale[i];
tot_sale+=regionsale[i];
}
if (tot_sale>target)
comm=tot_sale*0.15;
}
void salesman::display()
{
cout<<"salesman no. "<<salesman_no<<”\n”;
cout<<"region sale\n ”;
for(int i=0; i<4; i++)
cout<<regionsale[i]<<”\n”;
cout<<"total sale ”<<tot_sale;
cout<<"\ncommission - "<<comm;
}
void main()
{
salesman s1;
s1.getdata();
s1.display();
getch();
}
57
4. Write a program to define a class item with the data members such as icode (array of 10)
and price (array of 10) (hint- accept n in main and pass as an argument in all the functions)
The member functions of the class are
1. getdata() to accept details for n items where n is entered by the user in the main program
2.putdata() to display details of n items
3.totprice() calculate total for n items
4. maxprice() calculate maximum price.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class item
{
int icode[10];
int price[10];
public:
void getdata(int);
void putdata(int);
int totprice(int);
int maxprice(int);
}o;
void item::getdata(int n)
{
int i;
cout<<"enter icode and price respectively\n";
for(i=0;i<n;i++)
{
cin>>icode[i];
cin>>price[i];
}
}
void item::putdata(int n)
{
int i;
for(i=0;i<n;i++)
cout<<icode[i]<<" "<<price[i]<<endl;
}
int item::totprice(int n)
{
int i, t=0;
for(i=0;i<n;i++)
58
t=t+ price[i];
return t;
}
int item::maxprice(int n)
{
int i;
int max=-32768;
for(i=0;i<n;i++)
if(price[i]>max)
max=price[i];
return max;
}
void main()
{
int n;
cout<<"how many items\n";
cin>>n;
o.getdata(n);
o.putdata(n);
cout<<"the total is "<< o.totprice(n) <<endl;
cout<<"the max price is "<< o.maxprice(n) <<endl;
getch();
}
5.
Declare a class radio station with the following
Data members:
Entrance exam marks, eem (Out of 30)
Audition marks, am (Out of 70)
59
Result
Member functions: Private:
Assign() – To assign value to result depending on the following criteria .
If Total marks (entrance exam and audition marks) >=80 and audition marks >=60
then candidate is “Selected”.
If Total marks >=80 and audition marks <60 then candidate is in “Waiting list”.
Otherwise “Rejected”
Public:
Input () – Input details of marks and invoke assign().
Output () – To display all data members and total marks
int retresult(char re[]);
int rettotmarks();
Write functions for the following
i) Sort () – To sort an array of ‘n’ objects in descending order of total marks
ii) Selected () – Display only selected candidates from an array of ‘n’ objects.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class radio
{
private:
int e;
int a;
char result[30];
void assign( )
{
if((e+a)>=80 && e>=60)
strcpy(result, “selected");
else if((e+a)>=80 &&e<60)
strcpy(result, “waiting");
else
strcpy(result, “rejected");
}
public:
void input( );
void output( );
int retresult(char r[ ]);
int rettotmarks( );
}s[30];
60
void radio :: input()
{
cout<<"enter audition marks\n";
cin>>a;
cout<<"enter entrance marks\n";
cin>>e;
assign();
}
void radio :: output()
{
cout<<a<<" "<<e<<" "<<result<<endl;
}
int radio:: rettotmarks()
{
return(e+a);
}
int radio :: retresult(char r[])
{
if(strcmp(result, “selected")==0)
return 1;
else
return 0;
}
void main()
{
int n, i, ch, j;
radio temp;
cout<<"enter number of candidates\n";
cin>>n;
for(i=0;i<n;i++)
s[i].input();
cout<<"displaying menu\n";
cout<<"1 sort\n";
cout<<"2 selected candidates\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:
for(i=0;i<n;i++)
61
{
for(j=0;j<(n-1-i);j++)
{
if(s[j].rettotmarks()>s[j+1].rettotmarks())
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
s[i].output();
break;
case 2:
for(i=0;i<n;i++)
{
if(s[i].retresult("selected")==1)
s[i].output();
}
break;
}
getch();
}
62
6.
Define a class student with the following members :-
Rno – integer
Name – char name[20
Grade – integer
Sec – char
Marks – float
Input() – A function to input all details
Display() – A function to display all details
Write standalone functions to perform the following:-
WAF to check if a user entered rno is present or not. If present, display the details of that
student. If not, display message “Not found”.
WAF to check if a user entered name is present or not.
WAF to count no of students in section ‘A’.
WAF to display details of students who has obtained marks within the range of 85 and 90.
WAF to sort the students details in ascending order of marks.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int rno;
char name[30];
int grade;
char sec;
63
int marks;
public:
void getdata();
void putdata();
int retroll();
int retname(char []);
char retsec();
int retmarks();
}s[30];
void student::getdata()
{
cout<<"enter roll number\n";
cin>>rno;
cout<<"enter name\n";
gets(name);
cout<<"enter grade and sec\n";
cin>>grade>>sec;
cout<<"enter marks\n";
cin>>marks;
}
void student::putdata()
{
cout<<rno<<" "<<name<<" "<<grade<<" "<<sec<<" "<<marks<<endl;
}
int student::retroll()
{
return rno;
}
int student::retname(char na[30])
{
if(strcmp(name, na)==0)
return 1;
else
return 0;
}
char student::retsec()
{
return sec;
}
int student::retmarks()
64
{
return marks;
}
void searchroll(int r, int n)
{
int i;
int flag=0;
for(i=0;i<n;i++)
{
if(s[i].retroll()==r)
{
flag=1;
s[i].putdata();
break;
}
}
if (flag==0)
cout<<"record not found\n";
}
void searchname(char na[30],int n)
{
int i;
for(i=0;i<n;i++)
if(s[i].retname(na)==1)
s[i].putdata();
}
void countsec(int n)
{
int count=0;
int i;
for(i=0;i<n;i++)
if(s[i].retsec()=='a')
count++;
cout<<count;
}
void marksrange(int n)
{
int i;
for(i=0;i<n;i++)
if(s[i].retmarks()>=85&&s[i].retmarks()<=100)
65
s[i].putdata();
}
void sort(int n)
{
int i, j;
student temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(s[j].retmarks( )>s[j+1].retmarks( ))
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
s[i].putdata();
}
void main()
{
int n, i, r, ch;
char ans;
char na[30];
cout<<"how many students\n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the details of student "<<(i+1)<<"\n";
s[i].getdata();
}
do
{
cout<<"Displaying menu\n";
cout<<"1 for search on roll\n";
cout<<"2 for search on name\n";
cout<<"3 for counting students of section a\n";
cout<<"4 counting students scoring between 85 to 100\n";
66
cout<<"5 for sorting\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter the roll number you want to search\n";
cin>>r;
searchroll(r, n);
break;
case 2: cout<<"enter the name you want to search\n";
cin>>na;
searchname(na, n);
break;
case 3: countsec(n);
break;
case 4: marksrange(n);
break;
case 5: sort(n);
break;
}
cout<<"Do you want to continue?\n";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}
67
68
69
7.
Define a class “CLOCK”
Private members:
Hours integer
Minutes integer
Public members:
Void Readtime (int h, int m);
Void disp_time( )
clock Addtime(CLOCK t1, CLOCK t2);
clock time_diff(CLOCK t1, CLOCK t2);
• If t1 contains 3 hrs 50 minutes and t2 contains 4 hours 20 minutes the difference to be
calculated and stored is 0 hours and 30 minutes.
• If t1 contains 6 hrs 50 minutes and t2 contains 4 hours 20 minutes the difference to be
calculated and stored is 2 hours and 30 minutes.
• If t1 contains 9 hrs 10 minutes and t2 contains 4 hours 30 minutes the difference to be
calculated and stored is 4 hours and 40 minutes.
class clock
{
int hours;
int minutes;
public:
void readtime(int h, int m);
void disp();
clock addtime(clock t1,clock t2);
clock timediff(clock t1,clock t2);
};
70
void clock :: readtime(int h, int m)
{
hours=h;
minutes=m;
}
void clock::disp()
{
cout<<hours<<" "<<minutes<<"\n";
}
clock clock :: addtime(clock t1,clock t2)
{
clock t3;
t3.hours=t1.hours+t2.hours;
t3.minutes=t1.minutes+t2.minutes;
if(t3.minutes>60)
{
t3.hours++;
t3.minutes=t3.minutes-60;
}
return t3;
}
clock clock :: timediff(clock t1,clock t2)
{
clock t3;
int x, y, z;
x=t1.hours*60+t1.minutes;
y=t2.hours*60+t2.minutes;
if(x>y)
{
z=x-y;
t3.hours=z/60;
t3.minutes=z%60;
}
else
{
z=y-x;
t3.hours=z/60;
t3.minutes=z%60;
}
return t3;
71
}
void main()
{
clock t1,t2,t3;
int h1,h2,m1,m2;
cout<<"enter first time in hours and mins\n";
cin>>h1>>m1;
t1.readtime(h1,m1);
cout<<"enter second time in hours and mins\n";
cin>>h2>>m2;
t2.readtime(h2,m2);
t3=t3.addtime(t1,t2);
cout<<"the time after adding is \n";
t3.disp();
t3=t3.timediff(t1,t2);
cout<<"the time after subtracting is\n";
t3.disp();
getch();
}
72
FUNCTION OVERLOADING
CONSTRUCTORS AND
DESTRUCTORS
73
WAP using an overloaded function SUM to find
a. Sum of the squares of 3 nos.
b. Sum of the cubes of 2 nos.
c. Sum of the factors of a no.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int sum(int a, int b, int c)
{
int s=pow(a,2)+pow(b,2)+pow(c,2);
return s;
}
int sum(int a, int b)
{
int s=pow(a,3)+pow(b,3);
return s;
}
int sum(int a)
{
int s=0;
for(int i=1; i<=a; i++)
if(a%i==0)
s=s+i;
return s;
}
void main()
{
int a, b, c;
cout<<"enter 3 numbers\n";
cin>>a>>b>>c;
cout<<"Sum of squares of 3 numbers\n";
cout<<sum(a, b, c);
cout<<endl;
cout<<"Sum of cubes of 2 numbers\n";
cout<<sum(a, b);
cout<<endl;
cout<<"Sum of the factors of a number\n";
cout<<sum(a);
cout<<endl;
74
getch();
}
75
strcpy(country," ");
}
address(char na[30],char st[30],int hn)
{
strcpy(name, na);
houseno=hn;
strcpy(street, st);
strcpy(city, "Delhi");
strcpy(country, "India");
}
void input();
void output();
}s[30];
void address::input()
{
cout<<"enter name\n";
gets(name);
cout<<"enter street\n";
gets(street);
cout<<"enter house number\n";
cin>>houseno;
cout<<"enter city and country\n";
gets(city);
gets(country);
}
void address::output()
{
cout<<"name "<<name<<"\n"<<"street "<<street<<"\n"<<"house
"<<houseno<<"\n"<<"city "<<city<<"\n"<<"country "<<country<<endl;
}
void main()
{
char st[30];
char na[30];
int hn;
address s1;
s1.output();
cout<<"enter name\n";
gets(na);
cout<<"enter street\n";
76
gets(st);
cout<<"enter house number\n";
cin>>hn;
address s2=address(na, st, hn);
s2.output();
getch();
}
77
}
void main()
{
int a, b, c;
cout<<"enter 3 numbers\n";
cin>>a>>b>>c;
cout<<”Max of two numbers\n”;
cout<<max(a, b)<<endl;
cout<<”Max of three numbers\n”;
cout<<max(a, b, c);
getch();
}
#include<iostream.h>
class item
{
int itemno;
char itemname[20];
int q;
int ppu;
int amount;
78
cal()
{
amount=ppu*q;
}
public:
item()
{
itemno=0;
strcpy(itemname," ");
q=0;
ppu=0;
amount=0;
}
item(item &s)
{
itemno=s.itemno+1;
strcpy(itemname, s.itemname);
q=s.q;
ppu=s.ppu+0.1*s.ppu;
amount=s.amount+1;
}
void input();
void output();
};
void item::input()
{
cout<<"enter item number and item name\n";
cin>>itemno>>itemname;
cout<<"enter quantity and price per unit\n";
cin>>q>>ppu;
cal();
}
void item::output()
{
cout<<"itemno"<<itemno<<"\n"<<"itemname"<<itemname<<"\n"<<"quantity
"<<q<<"\n"<<"price per unit "<<ppu<<"\n"<<"amount "<<amount<<endl;
}
void main()
{
item i1, i3;
79
i1.output();
item i2=i1;
i2.output();
i3.input();
i3.output();
getch();
}
80
FILE HANDLING
81
1.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void create()
{
cout<<endl;
cout<<”Enter the lines:\n”;
ofstream o1(“myfile.txt”);
char str[20],ch;
do
{
gets(str);
o1<<str<<endl;
cout<<”Do you want to add more?(Y/N): ”;
cin>>ch;
}while(ch==’y’||ch==’Y’);
o1.close();
}
void byte()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch;
while(i1.get(ch))
c++;
cout<<”No of bytes: ”<<c;
i1.close();
}
void alpha()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch;
while(i1.get(ch))
{
82
if(isalpha(ch))
c++;
}
i1.close();
cout<<”No of alphabets: ”<<c;
}
void digit()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch;
while(i1.get(ch))
{
if(isdigit(ch))
c++;
}
i1.close();
cout<<”No of digits: ”<<c;
}
void upplow()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
int c1=0;
char ch;
while(i1.get(ch))
{
if(isupper(ch))
c++;
else if(islower(ch))
c1++;
}
i1.close();
cout<<”No of uppercase: ”<<c;
cout<<”No of lowercase: ”<<c1;
}
void vowel()
83
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch;
while(i1.get(ch))
{
switch(ch)
{
case ’A’:
case ’a’:
case ’e’:
case ’E’:
case ’I’:
case ’i’:
case ’o’:
case ’O’:
case ’u’:
case ’U’: c++;break;
}
}
i1.close();
cout<<”No of vowels: ”<<c;
}
void word()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch[20];
while(i1>>ch)
{
c++;
}
i1.close();
cout<<”No of words: ”<<c;
}
void sentence()
{
cout<<endl;
84
ifstream i1(“myfile.txt”);
int c=0;
char ch[80];
while(i1.getline(ch,80))
{
c++;
}
i1.close();
cout<<”No of sentences: ”<<c;
}
void disp1()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch[20];
while(i1>>ch)
{
if(strlen(ch)<5)
cout<<ch;
}
i1.close();
}
void disp2()
{
cout<<endl;
ifstream i1(“myfile.txt”);
int c=0;
char ch[80];
while(i1.getline(ch,80))
{
if(ch[0]==’M’|| ch[0]==’m’|| ch[0]==’n’|| ch[0]==’N’)
c++;
}
i1.close();
cout<<”No of sentences starting with M or N: ”<<c;
}
void cntto()
{
cout<<endl;
85
ifstream i1(“myfile.txt”);
int c=0;
char ch[20];
while(i1>>ch)
{
if(strcmpi(ch,””)==0)
c++;
}
i1.close();
cout<<”No of occurences: ”<<c;
}
void dispA()
{
cout<<endl;
ifstream i1(“myfile.txt”);
ofstream o1(“mystory.txt”);
int c=0;
char ch[20];
while(i1>>ch)
{
if(ch[0]==’A’|| ch[0]==’a’)
o1<<ch<<” ”;
}
i1.close();
}
void changecase()
{
cout<<endl;
ifstream i1(“myfile.txt”);
ofstream o1(“newfile.txt”);
int c=0;
char ch[80];
while(i1.getline(ch,80))
{
strupr(ch);
o1<<ch<<endl;
}
i1.close();
}
void star()
86
{
cout<<endl;
ifstream i1(“myfile.txt”);
ofstream o1(“star.txt”);
int c=0;
char ch;
while(i1.get(ch))
{
if(ch==’ ’)
o1<<’*’;
}
i1.close();
}
void enterfile ()
{
char ch[80];
cout<<”Enter the file name ”;
gets(ch);
char ch1;
ifstream i1(strcat(ch,“.txt”));
cout<<endl;
while(i1.get(ch1))
{
cout<<ch1;
}
i1.close();
}
void main()
{
create();
byte();
alpha();
digit();
upplow();
vowel();
word();
sentence();
disp1();
disp2();
87
cntto();
dispA();
enterfile();
star();
changecase();
getch();
}
2.
Write a menu driven program to perform the following operations on a binary file
“Society.dat”
∙ Appending data into the file.
∙ Displaying the records present in the file.
∙ Searching for records where no of rooms is less than n.
∙ Display those records of housetype “HIG” and “Duplex”
∙ Delete the record of that member where no of rooms is 1
∙ Copy the records of housetype “Villa” and “Duplex” to Delux.dat
∙ Modify the record of 4 room flat by updating its cost.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
class society
{
int mcode;
88
char mname[15];
char housetype[20];
int noofrooms;
float cost;
public:
void getdata();
void printdata();
int ret_norooms();
int checktype(char type[]);
void update();
};
void society::getdata()
{
cout<<”Enter the member code\n”;
cin>>mcode;
cout<<”Enter the member name\n”;
gets(mname);
cout<<”Enter the house type\n”;
gets(housetype);
cout<<”Enter the no of room\n”;
cin>>noofrooms;
cout<<”Enter the cost\n”;
cin>>cost;
}
void society::printdata()
{
cout<<”\n\nDisplaying details\n”;
cout<<”Code:”<<mcode<<endl;
cout<<”Name:”<<mname<<endl;
cout<<”Type:”<<housetype<<endl;
cout<<”No of rooms:”<<noofrooms<<endl;
cout<<”Cost:”<<cost<<endl;
}
int society::checktype(char type[])
{
return strcmpi(type, housetype);
}
void society::update()
{
cost*=1.1;
89
}
society s;
void append_data()
{
int n;
ofstream o1(“society.dat”, ios::binary);
cout<<”How many records?\n”;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<”\nEnter the details of the member\n”;
s.getdata();
o1.write((char*)&s, sizeof(s));
}
o1.close();
}
void disp_data()
{
ifstream i1(“society.dat”, ios::binary);
if(!i1)
{
cout<<”File does not exist\n”;
return;
}
while(i1.read((char*)&s, sizeof(s)))
s.printdata();
i1.close();
}
void search(int n)
{
cout<<”The records for which no of rooms is less than ”<<n<<” are:\n”;
ifstream i1(“society.dat”, ios::binary);
while(i1.read((char*)&s, sizeof(s)))
{
if(s.ret_norooms()<n)
s.printdata();
}
i1.close();
}
void search_housetype()
90
{
ifstream i1(“society.dat”, ios::binary);
cout<<”The records for which the house type is HIG Duplex are:\n”;
while(i1.read((char*)&s, sizeof(s)))
{
if(s.checktype(“HIG”)==0||s.checktype(“Duplex”)==0)
s.printdata();
}
i1.close();
}
void delete_record()
{
ifstream i1(“society.dat”, ios::binary);
ofstream o1(“backup.dat”, ios::binary);
while(i1.read((char*)&s, sizeof(s)))
{
if(s.ret_norooms()!=1)
o1.write((char*)&s, sizeof(s));
}
i1.close();
o1.close();
remove(“society.dat”);
rename(“backup.dat”, ”society.dat”);
}
void delux()
{
ifstream i1(“society.dat”, ios::binary);
ofstream o1(“Delux.dat”, ios::binary);
while(i1.read((char*)&s, sizeof(s)))
{
if(s.checktype(“Villa”)==0||s.checktype(“Duplex”)==0)
o1.write((char*)&s, sizeof(s));
}
i1.close();
o1.close();
}
void modify_4()
{
fstream i1(“society.dat”, ios::binary| ios::in| ios::out);
while(i1.read((char*)&s, sizeof(s)))
91
{
if(s.ret_norooms()==4 && s.checktype(“Flat”)==0)
s.update();
i1.seekp(-sizeof(s), ios::cur);
i1.write((char*)&s, sizeof(s));
}
i1.close();
}
void main()
{
int choice, n;
char choice1;
do
{
cout<<”Displaying Menu\n”;
cout<<”1.Append data\n2.Display data\n3.Searching for records where no of
rooms is less than n\n”;
cout<<”4.Display records of house type HIG or Duplex\n”;
cout<<”5.Delete the record where no of rooms is 1\n”;
cout<<”6.Copy the records of housetype “Villa” and “Duplex” to Delux.dat\n”;
cout<<”7.Modify record of 4 room flat by updating cost.\n”;
cin>>choice;
switch(choice)
{
case 1:append_data();
break;
case 2:disp_data();
break;
case 3:cout<<”Enter the value of n\n”;
cin>>n;
search(n);
break;
case 4:search_housetype();
break;
case 5:delete_record();
break;
case 6:delux();
break;
case 7:modify_4();
break;
92
default:cout<<”Invalid entry\n”;
}
cout<<”\nWould you like to continue?(Y/N)\n”;
cin>>choice1;
}while(choice1==’y’||choice1==’Y’);
getch();
}
93
3.
94
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class library
{
int bno;
char bname[25];
char author[25];
float price;
char status;
public:
void input_data();
void output_data();
int ret_bno();
int ret_bookname(char a[]);
char return_status();
void change_status();
};
void library::input_data()
{
cout<<”Enter the book number\n”;
cin>>bno;
cout<<”Enter book name\n”;
gets(bname);
cout<<”Enter author name\n”;
gets(author);
cout<<”Enter the price\n”;
cin>>price;
cout<<”Enter status\n”;
cin>>status;
}
void library::output_data()
{
cout<<”Book number: “<<bno<<endl;
cout<<”Book name: “<<bname<<endl;
cout<<”Author:”<<author<<endl;
cout<<”Price: ”<<price<<endl;
cout<<”Status: ”<<status<<endl;
}
95
int library::ret_bno()
{
return bno;
}
int library::ret_bookname(char a[])
{
return(strcmpi(bname, Aa));
}
char library::return_status()
{
return status;
}
void library::change_status()
{
if(status==’I’)
status=’A’;
else
status=’I’;
}
void add_details()
{
int flag=0;
library L1,L;
cout<<”Enter the details to be added\n”;
L.input_data();
ifstream i1(“library.dat”, ios::binary);
ofstream o1(“temp.dat”, ios::binary);
while(i1.read((char*)&L1, sizeof(L1)))
{
if(ret_bno()<L1.ret_bno()&&flag==0)
{
flag=1;
o1.write((char*)&L,sizeof(L));
o1.write((char*)&L1,sizeof(L1));
}
else
o1.write((char*)&L1,sizeof(L1));
}
if(flag==0)
o1.write((char*)&L,sizeof(L));
96
i1.close();
o1.close();
remove(“library.dat”);
rename(“temp.dat”, “library.dat”);
}
void disp_data()
{
library l;
ifstream i1(“library.dat”, ios::binary);
while(i1.read((char*)&l, sizeof(l)))
output_data();
i1.close();
}
void disbosom(int no)
{
int found=0;
library l;
ifstream i1(“library.dat”, ios::binary);
while(i1.read((char*)&l, sizeof(l)))
{
if(ret_bno()==no)
{
found=1;
cout<<”Status of the book is “<<return_status();
break;
}
}
if(found==0)
cout<<”Book not found\n”;
i1.close();
}
void issue book()
{
char name[20];
cout<<”Enter the name of the book to be issued\n”;
gets(name);
library l;
int found=0;
ifstream i1(“library.dat”, ios::binary);
ofstream o1(“temp.dat”, ios::binary);
97
while(i1.read((char*)&l, sizeof(l)))
{
if(ret_bookname(name)==0 && l.return_status()==’A’)
{
found=1;
l.change_status();
}
o1.write((char*)&l, sizeof(l));
}
i1.close();
o1.close();
remove(“library.dat”);
rename(“temp.dat”, “library.dat“);
if(found==0)
cout<<”Book not found, no change made\n”;
else
cout<<”Book issued\n”;
}
void main()
{
int no, choice;
char ch;
do
{
cout<<”Displaying menu\n”;
cout<<”1.Add records in ascending order\n”;
cout<<”2.Display all records\n”;
cout<<”3.Display the status of specific book\n”;
cout<<”4.Issue a book\n”;
cin>>choice;
switch(choice)
{
case 1: add_details();
break;
case 2: disp_details();
break;
case 3: cout<<”Enter the book number\n”;
cin>>no;
disp_book(no);
break;
98
case 4: issue_book();
break;
default: cout<<”Invalid entry”;
}
cout<<”\nDo you want to continue?(Y/N)\n”;
cin>>ch;
}while(ch==’y’||ch==’Y’);
}
99
100
ARRAYS
101
1.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, m, i, j, ch;
int temp=0;
int rsum, csum;
int a[5][5];
int b[5][5];
int c[5][5];
char ans;
do
{
cout<<"enter no of rows and columns\n";
cin>>n;
cout<<"enter elements\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cin>>a[i][j];
}
}
cout<<"\nDisplaying menu\n";
cout<<"1.Add 2 matrices\n";
cout<<"2.Display major and minor diagonal\n";
cout<<"3.Transpose of array\n";
102
cout<<"4.Display row and column sum\n";
cout<<"5.Interchanging first row and middle column\n";
cout<<"6.Display middle row and middle column\n";
cout<<"7.Replace every rows first element with sum of the elements of that row\
n";
cout<<"8.Interchanging elements of major diagonal and minor diagonal\n";
cout<<"enter choice\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter no. rows and columns for 2nd matrix\n";
cin>>m;
cout<<"enter elements\n";
for(i=0;i<m; i++)
{
for(j=0;j<m; j++)
{
cin>>b[i][j];
}
}
cout<<"displaying elements of matrix 1\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\nDisplaying elements of matrix 2\n";
for(i=0;i<m; i++)
{
for(j=0;j<m; j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\n";
}
if(n=m)
103
{
cout<<"\nDisplaying added matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
break;
case 2:
cout<<"displaying elements of matrix 1\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"displaying major diagonal\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
if(i==j)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout<<"\n";
}
cout<<"displaying minor diagonal\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
104
if(i+j==(n-1))
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout<<"\n";
}
break;
case 3:
cout<<"displaying elements of matrix \n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"displaying transpose of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
if(i<j)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
break;
105
case 4:
cout<<"displaying elements of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<n;i++)
{
rsum=0;
for(j=0;j<n; j++)
{
rsum+=a[i][j];
}
cout<<"the rsum is "<<rsum<<"\n";
}
for(i=0;i<n;i++)
{
csum=0;
for(j=0;j<n; j++)
{
csum+=a[j][i];
}
cout<<"the csum is "<<csum<<"\n";
}
break;
case 5:
cout<<"displaying elements of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<n;i++)
106
{
for(j=0;j<n; j++)
{
temp=a[0][j];
a[0][j]=a[i][1];
a[i][1]=temp;
}
cout<<"\n";
}
cout<<"displaying interchanged matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
break;
case 6:
cout<<"displaying elements of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"displaying middle row of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
if(j==1)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout<<"\n";
107
}
cout<<"displaying middle column of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
if(i==1)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout<<"\n";
}
break;
case 7:
cout<<"displaying elements of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<n;i++)
{
rsum=0;
for(j=0;j<n; j++)
{
rsum+=a[i][j];
a[i][0]=rsum;
}
}
cout<<"displaying new matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
108
cout<<"\n";
}
break;
case 8:
cout<<"displaying elements of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
if(j==(n-i-1))
{
temp=a[i][j];
a[i][j]=a[i][i];
a[i][i]=temp;
}
}
}
cout<<"displaying new matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
break;
}
cout<<"do you want to continue\n";
cin>>ans;
}
while(ans=='y'||ans=='Y');
109
getch();
}
OUTPUT:
case 1:
case 2:
110
case 3:
111
case 4:
case 5:
112
case 6:
case 7:
113
case 8:
2.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, m, i, j, k;
int a[5];
int b[5];
int c[10];
cout<<"enter no of elements\n";
cin>>n;
cout<<"enter array 1\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter no of elements\n";
cin>>m;
cout<<"enter array 2\n";
for(i=0;i<m; i++)
cin>>b[i];
for(i=0,j=0,k=0;i<m&&j<n; k++)
{
114
if(a[i]<=b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
}
while(i<m)
{
c[k]=a[i];
i++;
k++;
}
while(j<n)
{
c[k]=b[j];
j++;
k++;
}
cout<<"New array"<<endl;
for(i=0;i<m+n; i++)
cout<<c[i]<<" ";
getch();
}
115
STACKS, QUEUES AND LINKED
LISTS
116
STACKS
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int top=-1;
int n=5;
struct student
{
int sno;
char sname[20];
float marks;
};
void push(student s[],int n)
{
int no;
char na[20];
float m;
if(top==n-1)
cout<<“overflow”;
else
{
cout<<“enter rno, name, marks\n”;
cin>>no;
gets(na);
cin>>m;
top++;
s[top].sno=no;
strcpy(s[top].sname, na);
s[top].marks=m;
}
}
void pop(student s[],int n)
{
int no1;
float m1;
char na1[20];
if(top==-1)
cout<<“underflow”;
else
{
117
no1= s[top].sno;
strcpy(na1, s[top].sname);
m1=s[top].marks;
top--;
cout<<“deleted:”<<no1<<na1<<m1<<endl;
}
}
void disp(student s[])
{
if(top==-1)
cout<<“underflow”;
else
{
for(int i=top; i>=0; i++)
cout<<s[i].sno<<s[i].sname<<s[i].marks;
}
}
void main()
{
student s[10];
int ch ;
char ans;
do
{
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"Enter choice\n";
cin>>ch;
switch(ch)
{
case 1:push(s, n);
break;
case 2: pop(s, n);
break;
case 3: disp(s);
break;
}
cout<<"Do you want to continue\n"<<endl;
cin>>ans;
118
}while(ans=='y');
getch();
}
119
QUEUES
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int n=5;
int front=-1;
int rear=-1;
int s[n];
void insert(int s[])
{
int sn;
if(rear==n-1)
cout<<"overflow";
else
{
cout<<"enter the value ";
cin>>sn;
rear++;
s[rear]=sn;
}
if(rear==0)
front=0;
}
void dele(int s[])
{
int sno1;
if(front==-1)
cout<<"underflow";
else if(front==rear)
{
sno1=s[front];
cout<<"Deleted element is"<<sno1;
front=-1;
rear=-1;
}
else
{
sno1= s[front];
front++;
cout<<"deleted element is"<<sno1<<endl;
120
}
}
void disp(int s[])
{
if(front==-1)
cout<<"underflow";
else
{
for(int i=front; i<=rear; i++)
cout<<s[i];
}
}
void main()
{
int ch ;
char ans;
do
{
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. Display"<<endl;
cout<<"choice";
cin>>ch;
switch(ch)
{
case 1:insert(s);
break;
case 2: dele(s);
break;
case 3: disp(s);
break;
}
cout<<"Continue"<<endl;
cin>>ans;
}
while(ans=='y');
getch();
}
121
122
CIRCULAR QUEUES
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int n=5;
int front=-1;
int rear=-1;
int s[n];
void insert(int s[])
{
int sn;
if((rear==n-1) && (front==0) || (front==rear+1))
cout<<"overflow";
else if(rear==n-1)
{
cout<<"enter the value ";
cin>>sn;
rear=0;
s[rear]=sn;
}
else
{
cout<<"enter the value";
cin>>sn;
rear++;
s[rear]=sn;
if(rear==0)
front=0;
}
}
void dele(int s[])
{
int sno1;
if(front==-1)
cout<<"underflow";
else if(front==rear)
{
sno1=s[front];
cout<<"Deleted element is"<<sno1;
front=-1;rear=-1;
123
}
else if(front==n-1)
{
sno1= s[front];
front=0;
cout<<"deleted element is"<<sno1<<endl;
}
else
{
sno1= s[front];
front++;
cout<<"deleted element is"<<sno1<<endl;
}
}
void disp(int s[])
{
int i;
if(front==-1)
cout<<"underflow";
else if(front<rear)
{
for(i=front; i<=rear; i++)
cout<<s[i];
}
else
{
for(i=front; i<n;i++)
cout<<s[i]<<" ";
for(i=0;i<=rear; i++)
cout<<s[i]<<" ";
}
}
void main()
{
int ch ;
char ans;
do
{
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
124
cout<<"3. Display"<<endl;
cout<<"choice";
cin>>ch;
switch(ch)
{
case 1:insert(s);
break;
case 2: dele(s);
break;
case 3: disp(s);
break;
}
cout<<"Continue"<<endl;
cin>>ans;
}while(ans=='y');
}
125
LINKED LISTS
1.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct stud
{
int rno;
char name[20];
float marks;
stud *link;
};
stud *ptr=NULL,*top=NULL,*temp=NULL;
void push()
{
if(top==NULL)
{
ptr=new stud;
cin>>ptr->rno;
gets(ptr->name);
cin>>ptr->marks;
ptr->link=NULL;
top=ptr;
}
else
{
ptr=new stud;
cin>>ptr->rno;
gets(ptr->name);
cin>>ptr->marks;
ptr->link=top;
top=ptr;
}
}
void pop()
{
if(top==NULL)
cout<<"Underflow";
else
{
126
temp=top;
cout<<"Deleted element is\n"<<temp->rno<<"\n"<<temp->name<<"\n"<<temp-
>marks;
top=top->link;
delete temp;
}
}
void disp()
{
if(top==NULL)
cout<<"UNDERFLOW";
else
{
ptr=top;
while(ptr!=NULL)
{
cout<<ptr->rno<<" "<<ptr->name<<" "<<ptr->marks<<endl;
ptr=ptr->link;
}
}
}
void main()
{
int ch;
char ans;
do
{
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:disp();
break;
127
}
cout<<"\nContinue\n";
cin>>ans;
}
while(ans=='y');
getch();
}
128
2.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct stud
{
int rno;
char name[20];
float marks;
stud *link;
};
stud *ptr=NULL , *rear=NULL,*front=NULL,*temp=NULL;
void insert()
{
if(rear==NULL)
{
ptr=new stud;
cin>>ptr->rno;
gets(ptr->name);
cin>>ptr->marks;
ptr->link=NULL;
rear=ptr;
front=ptr;
}
else
{
ptr=new stud;
cin>>ptr->rno;
gets(ptr->name);
cin>>ptr->marks;
ptr->link=NULL;
rear->link=ptr;
rear=ptr;
}
}
void del()
{
if(front==NULL)
cout<<"Underflow";
else if(front==rear)
129
{
temp=front;
cout<<"Deleted element is"<<temp->rno<<" "<<temp->name<<" "<<temp-
>marks<<endl;
front=NULL;
rear=NULL;
}
else
{
temp=front;
cout<<"Deleted element is"<<temp->rno<<" "<<temp->name<<" "<<temp-
>marks<<endl;
front=front->link;
delete temp;
}
}
void disp()
{
if(front==NULL)
cout<<"UNDERFLOW";
else
{
ptr=front;
while(ptr!=NULL)
{
cout<<ptr->rno<<" "<<ptr->name<<" "<<ptr->marks<<endl;
ptr=ptr->link;
}
}
}
void main()
{
int ch;
char ans;
do
{
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
130
cout<<"Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:disp();
break;
}
cout<<"continue";
cin>>ans;
}
while(ans=='y');
getch();
}
131
132