0% found this document useful (0 votes)
123 views

Programs

The document contains 8 programming assignments related to C++ classes and files. The assignments involve creating classes to represent books in a library, fixed deposits, batsmen in cricket, and games. They also involve reading and writing objects to binary files, copying specific records between files based on conditions, and storing characters to different files based on type.

Uploaded by

akash singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Programs

The document contains 8 programming assignments related to C++ classes and files. The assignments involve creating classes to represent books in a library, fixed deposits, batsmen in cricket, and games. They also involve reading and writing objects to binary files, copying specific records between files based on conditions, and storing characters to different files based on type.

Uploaded by

akash singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

ASSIGNMENT-1

Q1) Write a program using classes to represent a book in a


library. Include the following members:
Data Members : Book Number, Book Name, Author, Publisher,
Price, No. of copies issued.
Member functions :
(i) To assign initial Values
(ii) To issue a book after checking for its availablity.
(iii) To return a book
(iv) To display book information
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Library
{
int BookNo;
char B_Name[25];
char B_Author[25];
char B_Publisher[25];
float Amount;
int No_of_Copies;
int No_of_Copies_Issued;
public:
void initial()
{
cout<<endl<<"Enter Book Number:- ";
cin>>BookNo;
cout<<endl<<"Enter Book Name: ";
gets(B_Name);
cout<<endl<<"Enter Author Name: ";
gets(B_Author);
cout<<endl<<"Enter Publisher Name: ";
gets(B_Publisher);
cout<<endl<<"Enter Price: ";
cin>>Amount;
cout<<endl<<"Enter Number of copies: ";
cin>>No_of_Copies;
}
void issue_book()

{
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

account of 2 customers with the following data members:Name of


the depositor, Account Number, Time Period(1or3or5 years),
Amount. The class also contains member funcyions:
(i)
To intialise data members.
(ii) Tofor withdrawl of money(after half of the time period has
passed).
(iii) To display the data mambers.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class Fixed
{
char name[20];
float acc_no;
int time;
float amt;
public:
void get_data();
void withdrawl();
void disp_data();
};
void Fixed::get_detail()
{
cout<<"\n Enter the name of the depositor : ";
gets(name);
cout<<"\n Enter account number : ";
cin>>acc_no;
cout<<"\n Enter time period ( 1 or 3 or 5 years : ";
cin>>time;
cout<<"\n Enter FD amount : ";
cin>>amt;
}
void Fixed::disp_detail()
{
cout<<"\n Depositor Name :"<<name;
cout<<"\n Account number :"<<acc_no;
cout<<"\n Time Period :"<<time;
cout<<"\n FD amount :"<<amt;
}
void Fixed::withdrawl()
{

float rate=0.1, withdrawn_amt,temp,t1;


temp=time/2.0;
t1= amt*rate*temp;
withdrawn_amt=amt+t1;
cout<<"\n The amount withdrawl after half of the time period is :
"<<withdrawn_amt;
}
void main()
{
clrscr();
Fixed deposit[10];
for(int i=0;i<10;i++)
{
cout<<"\n Enter Details of Depositor :";
deposit[i].get_data();
deposit[i].disp_data();
deposit[i].withdrawl();
cout<<endl;
}
getch();
}
Output is as follows:-

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;

cout<<"\n Enter new sixes :";


cin>>new_sixes;
new_runs=(new_fours*4)+(new_sixes*6);
cout<<"\n New runs scored : "<<new_runs;
runs=runs+new_runs;
no_of_fours+=new_fours;
no_of_sixes+=new_sixes;
}
void batsmen::disp_data()
{
cout<<"\n Batsman name :"<<first_name<<" "<<last_name;
cout<<"\n Number of fours hit : "<<no_of_fours;
cout<<"\n Number of sixes hit : "<<no_of_sixes;
cout<<"\n Total runs scored : "<<runs;
}
void main()
{
clrscr();
cout<<"\n Welcome to India vs Austraila Commonwealth bank Series.";
bat batting;
batting.get_detail();
batting.update();
cout<<endl;
batting.disp_detail();
getch();
}
Output is as follows:-

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

antique from binary file ANTIQUE.DAT of those antique items who


have price between 10000 to 15000.
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
void anti();
class Antique
{
int ano;
char aname[10];
float price;
public:
void buy()
{
cout<<"\n Enter(ano,price,aname)";
cin>>ano>>price;
gets(aname);
}
void show()
{
cout<<" "<<ano;
cout<<" "<<aname;
cout<<" "<<price;
}
float getprice()
{ return price; }
}A1[2],A;
void main()
{
clrscr();
ofstream fout;
fout.open("ANTIQUE.DAT",ios::out|ios::binary);
for(int i=0;i<=2;i++)
{
A1[i].buy();
fout.write((char*)&A1[i],sizeof(A1[i]));
}
fout.close();
anti();
getch();
}
void anti()
{

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

Q 7) Write a program that reads character from the keyboard one


by one. All lower case characters get store inside the file LOWER,
all upper case characters get stored inside the file UPPER and all
other characters get stored inside OTHERS .
#include<iostream.h>
#include <ctype.h>
#include<conio.h>
#include <stdio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
ofstream filout1,filout2,filout3;
filout1.open("UPPER.txt");
filout2.open("LOWER.txt");
filout3.open("OTHER.txt");
cout<<"Enter contents to store in file (Enter # to stop):\n";
while((c=getchar())!='#')
{
if(isupper(c))
{
filout1<<c;
}
else if(islower(c))
{
filout2<<c;
}
else
{
filout3<<c;
}
}
filout1.close();
filout2.close();
filout3.close();
getch();
}

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 .

//Program to Bubble sort an array


#include<iostream.h>
#include<conio.h>
void Bubblesort(int[],int);
void main()
{
clrscr();
int index,n,item,ar[50];
cout<<"\n How many elements do you want to enter? ";
cin>>n;
cout<<"\n Enter array elements..";
for(int i=0;i<n;i++)
cin>>ar[i];
Bubblesort(ar,n);
cout<<" The sorted array is as shown below: \n";
for(i=0;i<n;i++)
{ cout<<ar[i]<<" ";
}
getch();
}
void Bubblesort(int ar[],int size)
{
int temp,ctr=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{ if(ar[j]>ar[j+1])
{ temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
cout<<"\n Array after iteration"<<++ctr<<"is : ";
for(int k=0;k<size;k++)
cout<<ar[k]<<" ";
cout<<"\n";
}
}

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

Write a C++ program to merge 2 given arrays( A in ascending order , B in


descending order) into a 3rd array C , which should be in ascending order.
#include<iostream.h>
#include<conio.h>
void merge(int[],int,int[],int,int[]);
void main()
{ clrscr();
int A[50],B[50],C[50],mn=0,m,n;
cout<<"\n no. of elements in the first array are";
cin>>m;
cout<<"\n no. of elements in the second array are";
cin>>n;
mn=m+n;
cout<<"\n enter elements of the first aray(in asc. order) ";
for(int i=0;i<m;i++)
cin>>A[i];
cout<<"\n enter elements of the second aray(in desc. order) ";
for(int j=0;j<n;j++)
cin>>B[j];
merge(A,m,B,n,C);
cout<<"\n \n the merged array is as show below \n ";
for(i=0;i<mn;i++)
cout<<C[i]<<" "; cout<<endl;
getch();
}
void merge(int A[],int m,int B[],int n,int C[])
{ int a,b,c;
for(a=0,b=-1,c=0;a<m&&b>=0;)
{ if(A[a]<=B[b]) C[c++]=A[a++];
else C[c++]=B[b--];
}
if(a<m)
{ while(a<m)
C[c++]=A[a++];
}
else
{ while(b>=0)
C[c++]=B[b--];
}
}

OUTPUT

You might also like