Programs
Programs
{
cout<<"Enter book details......."<<endl;
initial();
if(No_of_Copies>0)
{
cout<<"enter How many book you want to issue:";
cin>>No_of_Copies_Issued;
if(No_of_Copies>=No_of_Copies_Issued)
{
No_of_Copies=No_of_Copies-No_of_Copies_Issued;
cout<<endl<<" "<<No_of_Copies_Issued<<" book is issued..";
display();
}
else
cout<<No_of_Copies_Issued<<" books is not available in stock..";
}
else
cout<<"Book is not available";
}
void return_book()
{
cout<<"enter book detail you want to return...";
cout<<endl<<"Enter Book Number: ";
cin>>BookNo;
cout<<endl<<"Enter Book Name: ";
gets(B_Name);
No_of_Copies=No_of_Copies+No_of_Copies_Issued;
cout<<endl<<BookNo<<":"<<B_Name<<"Book is returned......";
}
void display()
{
cout<<"Book Number: "<<BookNo<<endl;
cout<<"Book Name: "<<B_Name<<endl;
cout<<"Author Name: "<<B_Author<<endl;
cout<<"publisher Name: "<<B_Publisher<<endl;
cout<<"Price: "<<Amount<<endl;
}
};
void main()
{
clrscr();
Library a1;
int ch;
cout<<"1->Issue book..."<<endl;
cout<<"2->Return Book....."<<endl;
cout<<"Enter your choice...";
cin>>ch;
switch(ch)
{
case 1:
a1.issue_book();
break;
case 2:
a1.return_book();
break;
}
getch();
}
Output is as follows:-
ASSIGNMENT-2
Q 2) Write a program using classes to represent fixed_deposit
ASSIGNMENT-3
Q 3) Write a C++ program using class to represent batsmen in a
cricket team. Include the following members:
Data members:
First name,Last name,Runs made,Number of fours, Number of
sixes.
Member Functions:
(i)
To assign the initial values.
(ii) To update runs made(It should simultaneously update
fours and sixes,if required).
(iii) To display the batsmans information.
Make appropriate assumptions about access labels .
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class bat {
char first_name[20];
char last_name[20];
int runs,no_of_fours, no_of_sixes;
public:
void get_data();
void update();
void disp_data();
};
void bat::get_data()
{
cout<<"\n Enter first name of batsman : ";
gets(first_name);
cout<<"\n Enter Last name of batsman : "
;
gets(last_name);
cout<<"\n Enter runs playing at : ";
cin>>runs;
cout<<"\n Enter number of fours hit by him:";
cin>>no_of_fours;
cout<<"\n Enter number ofsixes hit by him: ";
cin>>no_of_sixes;
}
void bat::update()
{
int new_fours, new_sixes, new_runs;
cout<<"\n Enter new fours : ";
cin>>new_fours;
ASSIGNMENT-4
Q 4) Write a C++ program to append a record in file BOOK.DAT
with the help of function Modify () with the values of Book NEW
passed to its arguments.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
class Book
{
private:
int Book_no;
char Book_name[20];
public:
void enterdetails();
void showdetails();
int book_no()
{
return Book_no;
}
}a1[3],a[3];
void Book::enterdetails()
{
cout<<"\n Enter book no:- ";
cin>>Book_no;
cout<<"\n Enter book name:- ";
gets(Book_name);
}
void Book::showdetails()
{
cout<<"\n Book no is:- "<<" "<<Book_no;
cout<<"\n Book name is:- ";
cout<<Book_name;
}
void main()
{
ofstream fout;
fout.open("BOOK.dat",ios::app|ios::binary);
for(int i=0; i<=2; i++)
{
a1[i].enterdetails();
fout.write((char*)&a1[i],sizeof(a1[i]));
cout<<"\n Record added to file.";
}
fout.close();
ifstream fin;
fin.open("BOOK.dat",ios::in|ios::binary);
for(int j=0; j<=2; j++)
{
while(fin.read((char*)&a[i],sizeof(a[i])));
{
a[i].showdetails();
getch();
}
}
fin.close();
getch();
}
Output is as follows:-
ASSIGNMENT-5
Q 5) Write a C++ function anti of class antique to read objects of
ifstream fin;
fin.open("ANTIQUE.DAT",ios::in|ios::binary);
while(fin.read((char*)&A,sizeof(A)))
{
if(A.getprice()>10000&&A.getprice()<15000)
A.show();
cout<<"\n";
}
fin.close();
}
ASSIGNMENT-6
In a C++ program , make a file GAMES.DAT and display details of
those games which are meant for children of age range 8 to 13.
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void dispgames90;
class GAMES
{
int gamecode;
char gamename[10];
char agerange[10];
public:
void enter()
{
cout<<"\n enter gamecode , gamename , gamerange)";
cin>>gamecode;
gets(gamename);
gets(agerange);
}
void display()
{
cout<<gamecode<<" "<<gamename<<" "<<agerange;
}
char* ager90
{
return agerange;
}
}t1[2],t;
void main()
{
clrscr();
ofstream fout;
fout.open("GAMES.DAT",ios::out|ios::binary);
for(int i=0;i<=1;i++)
{
t1[i].enter();
fout.write((char*)&t1,sizeof(t1[i]));
}
fout.close();
dispgames();
getch();
}
void dispgames()
{
ifstream fin;
char ch;
fin.open("GAMES.DAT",ios::in|ios::binary);
while(fin.read((cgar*)&t,sizeof(t)))
fin.get(ch);
if(strcmp(t.ager()," 8 to 13")==0)
t.display();
cout<<"\n";
}
fin.close();
}
OUTPUT
ASSIGNMENT-7
ASSIGNMENT-8
Write a function in a program COPYAREA() in C++ , that would copy only
those records having area as SOUTH from CONSUMER.DAT to BACKUP.DAT
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Consumer { char C_Name[20];
char C_Address[30];
char Area[25];
char C_Phone[15];
public:
void Ledger()
{ cout<<\n enter name of the customer;gets(C_Name);
cout<<\n enter address:;gets(C_address);
cout<<\n enter area;gets(Area);
cout<<\n Enter Name;gets(C_Phone);}
void Disp()
{cout<<\n name of the customer<<C_Name;
cout<<\n Address<<C_Address;
cout<<\n area <<Area;
cout<<\n phone <<C_Phone;
}
int CheckCode(char AC[])
{ return strcmp(Area,AC);}
}C[3],p,k;
void COPYAREA(void);
void main()
{ clrscr();
ofstream u(CONSUMER.DAT,ios::out|ios::binary);
cout<<\n Enter the details of 2 customers;
for(int i=1;i<3;i++)
{ C[i].Ledger();
u.write((char*)&C[i],sizeof([i]));}
u.close();
COPYAREA();
Ifstream l(BACKUP.DAT,ios::in||ios::binary);
While(l)
{l.read((char*)&k,sizeof(k));
k.Disp();cout<<\n ;
}
l.close();
getch();}
void COPYAREA()
{ ofstream b(BACKUP.DAT,ios::out|ios::binary);
ifstream c(CONSUMER.DAT,ios::in|ios::binary);
while(c)
{ c.read((char*)&p,sizeof(p));
if(p.CheckCode(SOUTH)==0)
b.write((char*)&p,sizeof(p));
}
c.close();
b.close();
}
OUTPUT
ASSIGNMENT-9
Write a c++ program of binary search of the given elements entered by the
user in ascending order
#include<iostream.h>
#include<conio.h>
int Bsearch(int[],int,int);
void main()
{
clrscr();
int ar[50],index,item,a;
cout<<"\n With how many elements do you want to create an array? ";
cin>>a;
cout<<"\nEnter array elements(must be in ascending order):\n";
for(int i=0;i<a;i++)
cin>>ar[i];
cout<<"\n Enter element to be searched for: ";
cin>>item;
index=Bsearch(ar,a,item);
if(index==-1)
cout<<"\n Given element could not be found!";
else
cout<<"\n Element found at
index:"<<index<<",position:"<<index+1<<"\n";
getch();
}
int Bsearch(int ar[],int size, int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if (item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;
else last=mid-1;
} return -1; }
OUTPUT
ASSIGNMENT-10
Write a c++ program of LINEAR search of the given elements entered by
the user in ascending order
#include<iostream.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
clrscr();
int N,index,arr[20],item;
cout<<"\n With how many elements do you want to create an array (max
20)? "; cin>>N;
cout<<"\n Enter array elements : \n";
for(int i=0;i<N;i++)
cin>>arr[i];
cout<<"\n Enter elements to be searched for :\n"; cin>>item;
index=Lsearch(arr,N,item);
if (index==-1)
cout<<"\n Sorry!! Given element could not be found \n";
else
cout<<"\n Element found at index : "<<index<<" and
position :"<<index+1;
getch();
}
int Lsearch(int arr[],int size,int item)
{ for(int i=0;i<size;i++)
{ if(arr[i]==item)
return i;
}return -1;}
OUTPUT
ASSIGNMENT-11
Write a traversal program in C++ to convert all the elements in an
array into its square
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ar[50],item,n,index;
cout<<"\n how many elements do you want to create array with";
cin>>n;
cout<<"\n enter the elements";
for(int i=0;i<n;i++)
cin>>ar[i];
cout<<"\n new elements are";
for(i=0;i<n;i++)
{ar[i]*=67;cout<<ar[i]<<" ";}
getch();
}
ASSIGNMENT-12
Write a C++ program to insert a element enter by the user in the array list.
And arrange it in the list .
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Findpos(int[],int,int);
void main()
{
clrscr();
int index, n, item,arr[50];
cout<<" how many elements do you want to create array with...?";
cin>>n;
cout<<"\n Enter array elements(must be in ascending order).\n";
for(int i=0;i<n;i++)
cin>>arr[i];
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\n Enter elements to be inserted.";
cin>>item;
if(n==50)
{ cout<<"\n Overflow";
exit(1);
}
index=Findpos(arr,n,item);
for(i=n;i>index;i--)
{ arr[i]=arr[i-1];
}
arr[index]=item;
n+=1;
cout<<"\n Want to insert more elements?(y/n)";
cin>>ch;
}
cout<<"\n The array now is as shown here";
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<"\n";
getch();
}
int Findpos(int arr[],int size, int item)
{
int pos;
if(item<arr[0])
pos=0;
else
{ for(int i=0;i<size-1;i++)
{ if(arr[i]<item&&item<arr[i+1])
{ pos=i+1;break;
}
}
if(i==size-1)
pos=size;
}
return pos;
}
OUTPUT
ASSIGNMENT-13
Write a C++ program to sort an inserted unsorted array in ascending order
by using Bubble Sorting .
OUTPUT
ASSIGNMENT-14
Write a C++ program to sort an inserted unsorted array in ascending order
by using selection Sorting
#include<conio.h>
#include<iostream.h>
void selsort(int[],int);
void main()
{clrscr();
int ar[50],item,n,index;
cout<<"\n how many elements??";
cin>>n;
cout<<"\n enter array elements";
for(int i=0;i<n;i++)
cin>>ar[i];
selsort(ar,n);
cout<<"\n \n the sorted array is as shown below ";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}
void selsort(int ar[],int size)
{ int small,pos,tmp;
for(int i=0;i<size-1;i++)
{ small=ar[i];
pos=i;
for(int j=i+1;j<size;j++)
{
if(ar[j]<small)
{ small=ar[j]; pos=j;}
}
tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
cout<<"\n array after pass-"<<i+1<<"-is:";
for(j=0;j<size;j++)
cout<<ar[j]<<" ";
}}
OUTPUT
ASSIGNMENT-15
OUTPUT