0% found this document useful (0 votes)
228 views70 pages

ASSIGNMENT-1 (20 Files Merged) PDF

The document contains code snippets for 7 programming assignments. Each assignment includes a question, sample code to solve the question, and sample output. The assignments cover topics like: 1) Finding the highest number and its location in an array 2) Getting the transpose of a matrix 3) Entering and displaying details of multiple persons using a structure

Uploaded by

Prakhar Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views70 pages

ASSIGNMENT-1 (20 Files Merged) PDF

The document contains code snippets for 7 programming assignments. Each assignment includes a question, sample code to solve the question, and sample output. The assignments cover topics like: 1) Finding the highest number and its location in an array 2) Getting the transpose of a matrix 3) Entering and displaying details of multiple persons using a structure

Uploaded by

Prakhar Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

_ASSIGNMENT-1_

QUES-Write a program to enter ‘n’ numbers and get the


highest number with the location.

ANS-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int array[100],highest,size,c,location=1;
cout<<"Enter the number of elements in array : ";
cin>>size;
for(c=0;c<size;c++)
{
cout<<"Enter element "<<c+1<<" : ";
cin>>array[c];
}
highest=array[0];
for(c=1;c<size;c++)
{
if(array[c]>highest)
{
highest=array[c];
location=c+1;
}
}
cout<<"Highest element is present at location "<<location;
cout<<" and its value is : "<<highest;
getch();
}

OUTPUT:
ASSIGNMENT-2_
QUES – Write a program to get transpose of a matrix.

ANS-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;

// Storing element of matrix entered by user in array a[][].

cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}

// Displaying the matrix a[][]

cout << endl << "Entered Matrix: " << endl;


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}

// Finding transpose of matrix a[][] and storing it in array trans[][].

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}

// Displaying the transpose,i.e, Displaying array trans[][].

cout << endl << "Transpose of Matrix: " << endl;


for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
getch();
}

OUTPUT:
ASSIGNMENT-3_
QUES- Write a program to enter and display the details of
the persons .

ANS-
#include<iostream.h>
#include<conio.h>
struct person
{
char name[30];
int age;
float salary;
};
void main()
{
clrscr();
int i,size;
cout<<"Enter the number of persons :";
cin>>size;
person p[50];
for(i=0;i<size;i++)
{
cout<<"Enter details of person:"<<i+1;
cout<<endl<<"Enter Name:";
cin>>p[i].name;
cout<<endl<<"Enter age:";
cin>>p[i].age;
cout<<endl<<"Enter salary:";
cin>>p[i].salary;
}
cout<<endl<<"The details of all persons are:";
for(i=0;i<size;i++)
{
cout<<endl<<"The details of person "<<i+1<<" are";
cout<<endl<<"Name:"<<p[i].name;
cout<<endl<<"Age:"<<p[i].age;
cout<<endl<<"Salary:"<<p[i].salary;
cout<<endl;
}
getch();
}

OUTPUT:
ASSIGNMENT-4_
QUES – WRITE A PROGRAM TO SWAP TWO NUMBERS.

ANS-
#include<iostream.h>
#include<conio.h>
void swap(); //function prototype
void main()
{
clrscr();
swap(); //calling function
getch();
}
void swap() //function definition
{
int a,b,c;
cout<<"Enter value of first number:";
cin>>a;
cout<<"Enter the value of second number:";
cin>>b;
c=a;
a=b;
b=c;
cout<<endl<<"The swapped numbers are";
cout<<endl<<"a="<<a<<endl<<"b="<<b;
}
OUTPUT:
ASSIGNMENT-5_
QUES- Write a program to get the LCM and HCF of two
numbers.

ANS-

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,x,y,t,gcd,lcm;
cout<<"Enter two integers:";
cin>>x>>y;
a=x;
b=y;
while(b!=0)
{
t=b;
b=a%b;
a=t;

}
gcd=a;
lcm=(x*y)/gcd;
cout<<"Greatest common divisor of "<<x<<" and "<<y<<" is
"<<gcd;
cout<<"\nLeast common multiple of "<<x<<" and "<<y<<" is
"<<lcm;
getch();
return(0);
}
OUTPUT:
ASSIGNMENT-6_
QUES- Write a program to print the Fibonacci series.

ANS-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, s, n;
a=b=1;
cout<<endl<<"Enter the total number for a series =";
cin>>n;
cout<<endl<<a<<”,”<<b;
for(int i=1;i<=n;i++)
{
s=a+b;
cout<<”,”<<s;
a=b;
b=s;
}
getch();
}
OUTPUT:
ASSIGNMENT-7_
QUES-Write a program to calculate the area of circle,
rectangle and triangle using Function Overloading.

ANS-
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of circle :"<<pi*a*a;
}
void fn::area(int a, int b)
{
cout<<"Area of rectangle :"<<a*b;
}
void fn::area(float t,int a, int b)
{
cout<<"Area of triangle :"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\t Function Overloading";
cout<<"\n1.Area of circle\n2.Area of rectangle\n3.Area of
triangle\n4.Exit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter base and height of Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);

}
getch();
}
OUTPUT:
ASSIGNMENT-8_
QUES – Write to check whether a number or word is a
palindrome or not using function overloading.

ANS-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
//Number palindrome check
void palindrome(int no)
{
int r, rev=0, temp=no;
do
{
r=temp%10;
rev=rev*10+r;
temp/=10;
}while(temp!=0);
if(no==rev)
cout<<"\n It is a palindrome Number";
else
cout<<"\n It is not palindrome Number";
}

//String palindrome check


void palindrome(char text[])
{
int i , j;
char p='Y';
j=strlen(text)-1;
for(i=0;text[i]!='\0';++i)
for(j;j<i;--j)
if(text[i]!=text[j])
p='N';
if(p=='Y')
cout<<"\n It is a palindrome string";
else
cout<<"\n It is not a palindrome string";
}
void main()
{
int num;
char str[100];
int ch;
clrscr();
do
{
cout<<"\n PALINDROME CHECK USING FUNCTION
OVERLOADING";
cout<<"\n PALINDROME MENU CHECK";
cout<<"\n 1.STRING PALINDROME CHECK";
cout<<"\n 2.NUMBER PALINDROME CHECK";
cout<<"\n 3.EXIT";
cout<<"\n ENTER YOUR CHOICE ";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n Enter the string to check for
palindrome ";
gets(str);
palindrome(str);
break;
case 2:cout<<"\n Enter a number to check for
Palindrome";
cin>>num;
palindrome(num);
break;
case 3: exit(0);
}
getch();

}while(1);
}

OUTPUT:
ASSIGNMENT-9_
QUES – Write a program to calculate the netpay when user
enters hra ,da ,basic and pf using classes.

ANS-
#include<iostream.h>
#include<conio.h>
class emp
{
protected:
int eno;
char name[20],des[20];
public:
void get()
{
cout<<endl;
cout<<"Enter the employee number";
cin>>eno;
cout<<"Enter the employee name";
cin>>name;
cout<<"Enter Designation:";
cin>>des;
}
void display()
{
cout<<"Empno:"<<eno;
cout<<endl<<"Name:"<<name;
cout<<endl<<"Designation:"<<des;
}
};
class salary:public emp
{
private:
float basic,hra,da,pf,netpay;
void calculate()
{
netpay=(basic+hra+da)-pf;
}
public:
void get1()
{
get();
cout<<"Enter the basic pay:";
cin>>basic;
cout<<"Enter the House Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance:";
cin>>da;
cout<<"Enter the Provident Fund:";
cin>>pf;
calculate();
}
void disp()
{
display();
cout<<endl<<"Basic:"<<basic;
cout<<endl<<"HRA:"<<hra;
cout<<endl<<"DA:"<<da;
cout<<endl<<"PF:"<<pf;
cout<<endl<<"Net Pay:"<<netpay;
}
};
void main()
{
clrscr();
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get1();
}
for(i=0;i<n;i++)
s[i].disp();
getch();
}

OUTPUT:
ASSIGNMENT-10_
QUES- Write a program to calculate the area of square and
circle using inheritance.

ANS-

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class shape
{
protected:
char name[20];
float dim,area;
public:
virtual void get()=0;
};
class circle:public shape
{
public:
void get()
{
strcpy(name,"CIRCLE");
cout<<"\n Enter Radius";
cin>>dim;
area=3.14*dim*dim;
cout<<"\n Area of Shape:"<<name<<"="<<area;
}
};
class square:public shape
{
public:
void get()
{
strcpy(name,"SQUARE");
cout<<"\n Enter Side:";
cin>>dim;
area=dim*dim;
cout<<"\n Area of Square:"<<name<<"="<<area;
}
};
void main()
{
clrscr();
square S;
circle C;
S.get();
C.get();
getch();
}

OUTPUT:
ASSIGNMENT-11_
QUES- Write a program for bank to deposit / withdraw cash
using classes.

ANS-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class account
{
unsigned long accno;
char name[30];
float balance;
public:
void getdata();
void dispdata();
void deposit(float);
void withdraw(float);
};
void account::getdata()
{
cout<<"\n Enter the Account Number:";
cin>>accno;
cout<<"\n Enter the Customer Name:";
gets(name);
cout<<"\n Enter Initial Amount:";
cin>>balance;
}
void account::dispdata()
{
cout<<"\n Account Number:"<<accno;
cout<<"\n Customer Name:"<<name;
cout<<"\n Balance Amount:"<<balance;
}
void account::deposit(float amount)
{
balance+=amount;
cout<<"\n Amount Deposited :New Balance:"<<balance;
}
void account::withdraw(float amount)
{
if((balance-amount)>1000)
{
balance-=amount;
cout<<"\n Amount Withdraw:New
Balance:"<<balance;
}
else
cout<<"\n Not Enough Money In Account!!!!!!";
}
void main()
{
account cust;
cust.getdata();
float amt;
cout<<"\n Enter Amount To Be Deposited:";
cin>>amt;
cust.deposit(amt);
cust.dispdata();
cout<<"\n Enter Amount To Be Withdraw:";
cin>>amt;
cust.withdraw(amt);
cust.dispdata();
getch();
}
OUTPUT:
ASSIGNMENT-12_
QUES-Write a program to enter salary and commison, and
calculate the net salary using classes.

ANS-

#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[30];
int age;
float salary;
public:
void getdata();
void update_salary(float comm);
void show();
};
void employee::getdata()
{
cout<<"Enter Name of the Employee:";
cin>>name;
cout<<"Enter age:";
cin>>age;
cout<<"Enter Salary:";
cin>>salary;
}
void employee::update_salary(float comm)
{
salary=salary+comm;
}
void employee::show()
{
cout<<endl<<"The Employee Name is:"<<name;
cout<<endl<<"The age is:"<<age;
cout<<endl<<"The Total Salary:"<<salary;
}
void main()
{
clrscr();
employee e;
float comm;
e.getdata();
cout<<"Enter Commision:";
cin>>comm;
e.update_salary(comm);
e.show();
getch();
}

OUTPUT:
ASSIGNMENT-13_
QUES- Write a program for a hotel for calculating the
amount by check-in and check-out.

ANS-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class HOTEL
{
int Rno;
char Cname[30];
char category[10];
float tariff;
int NOD;
float CALC();
public:
void Checkin();
void Checkout();
};
float HOTEL::CALC()
{
float amount;
int c1,c2,c3;
c1=strcmp(category,"CLUB");
c2=strcmp(category,"SUIT");
c3=strcmp(category,"EXECUTIVE");
if(c1==0)
amount=2000*NOD;
else if(c2==0)
amount=5000*NOD;
else if(c3==0)
amount=10000*NOD;
return(amount);
}
void HOTEL::Checkin()
{
cout<<"\n Enter Room no:";
cin>>Rno;
cout<<"\n Enter Customer name:";
gets(Cname);
cout<<"\n Enter Category (CLUB/SUIT/EXECUTIVE):";
cin>>category;
cout<<"\n Enter number of days:";
cin>>NOD;
};
void HOTEL::Checkout()
{
cout<<"\n BILL DETAILS";
cout<<"\n ROOM NUMBER:"<<Rno;
cout<<"\n CUSTOMER NAME:"<<Cname;
cout<<"\n ROOM CATEGORY:"<<category;
cout<<"\n NUMBER OF DAYS STAYED:"<<NOD;
cout<<"\n BILL AMOUNT TO BE PAID:"<<CALC();
}
void main()
{
HOTEL H;
clrscr();
H.Checkin();
H.Checkout();
getch();
}
OUTPUT:
ASSIGNMENT-14_
QUES- Write a program to read a text file “PASSAGE.TXT”
and display the number of words starting with a capital
alphabet and the no. of occurrences of the word “is”.

ANS-

#include<fstream.h>
#include<string.h>
#include<process.h>
#include<ctype.h>
#include<conio.h>
void main()
{
clrscr();
int wc=0,uc=0;
char word[40];
ifstream fin("PASSAGE.TXT",ios::in);
if(!fin)
{
cout<<"File cannot be opened!!!";
exit(0);
}
else
{
while(!fin.eof())
{
fin.getline(word,10,' ');
if(strcmp(word,"is")==0 || strcmp(word,"Is")==0)
wc++;
if(isupper(word[0]))
uc++;
}
}
cout<<"The no. of words starting with capital letter="<<uc;
cout<<"\n The no. of occurrences of word 'is'= "<<wc;
fin.close();
getch();
}

OUTPUT:
ASSIGNMENT-15_
QUES- Write a program to input a text file name, read the
contents of the file and create a new file named COPY.TXT,
which shall contain only those words from the original file
which don’t start with an uppercase vowel (i.e., with ‘A’, ‘E’,
‘I’, ‘O’, ‘U’)

ANS-
#include<fstream.h>

#include<string.h>

#include<conio.h>

void main()

clrscr();

char word[30];

ifstream fin("PASSAGE.TXT", ios::in);

ofstream fout("COPY.TXT", ios::out);

if(!fin || !fout)

cout<<"File cannot be opened!!!";

exit(0);

}
else

while(!fin.eof())

fin.getline( word, 10, ' ');

if(word[0]=='A'||word[0]=='E'||word[0]=='I'||word[0]=='O'||word[0]=='U')

fout<<word<<" ";

cout << "SUCCESS!";

fin.close();

fout.close();

getch();

OUTPUT:
ASSIGNMENT-16_
QUES- A C++ program to Copy one file onto the end of
another, adding line numbers.

ANS-
#include <iostream.h>
#include <process.h>
#include <fstream.h>
#include <conio.h>
/* Copy one file onto the end of another, adding line numbers*/
int main ()
{
clrscr();
char myline[256];
int lc = 0;
ofstream outfile("demo.txt",ios::app);
ifstream infile("example.txt");
if (! infile)
{
cerr << "Failed to open input file\n";
exit(0);
}
while (1)
{
infile.getline(myline,256);
if (infile.eof()) break;
lc++;
outfile << lc << ": " << myline << "\n";
}
infile.close();
outfile.close();

cout << "Output " << lc << " records" << endl;
getch();
return(0);
}

OUTPUT:
ASSIGNMENT-17_
QUES-Write a menu driven program to perform binary file
operations.
struct student
{
int roll_no ;
char name[20];
float marks;
}
1.Create a new file .
2.Display all the records from the file .
3.Display all the records for particular roll no.,if
present(Search operation).
4.Exit.

ANS-
#include <fstream.h>
#include <stdio.h>
#include <process.h>
#include <conio.h>
void create();
void display();
void roll_search();
struct student
{
int roll_no;
char name[20];
float marks;
};
void create() //creating a new file
{
ofstream fout("stud.dat",ios::binary);
student s;
char rep;
do
{
cout<<"Enter roll number:";
cin>>s.roll_no;
cout<<"Enter marks:";
cin>>s.marks;
cout<<"Enter name:";
gets(s.name);
fout.write((char*)&s,sizeof(s));
cout<<"Do you want to add more records:"<<endl;
cin>>rep;
}
while(rep=='y'||rep=='Y');
fout.close();
}
void display()//displaying all the records from the file
{
ifstream fin("stud.dat",ios::binary);
student s;
while(fin.read((char*)&s,sizeof(s)))
{
cout<<"\n Roll number:"<<s.roll_no<<endl;
cout<<"\n Name :"<<s.name<<endl;
cout<<"Marks"<<s.marks<<endl;
}
fin.close();
}
void roll_search()//searching on the basis of student's roll
number
{
ifstream fin("stud.dat",ios::binary);
student s;
int x;
cout<<"Enter the roll number to be searched:"<<endl;
cin>>x;
while (fin.read((char*)&s,sizeof(s)))
{
if(s.roll_no==x)
{
cout<<"Record found"<<endl;
cout<<"Roll number:"<<s.roll_no<<endl;
cout<<"Name:"<<s.name<<endl;
cout<<"Marks:"<<s.marks<<endl;

}
}
fin.close();
}
void main()
{
char ch;
do
{
clrscr();
int x;
cout<<"Menu"<<endl;
cout<<"1.Create"<<endl;
cout<<"2.Display"<<endl;
cout<<"3.Search on roll number"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>x;
switch(x)
{
case 1: create();
break;
case 2: display();
break;
case 3: roll_search();
break;
case 4: exit(0);
break;
default: cout<<"wrong input"<<endl;
}
cout<<"Do you wish to continue(Y/N)"<<endl;
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}

OUTPUT:
ASSIGNMENT-18_
QUES-A menu driven program to scan, append, modify and
view records of a Binary file .

ANS-

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include<stdlib.h>
static int totrec=0; //totrec variable keep track for total variable
entered//Initially Record scanned are Zero
void main()
{
int choice;
while(1)
{
clrscr();
cout<<"Choose your choice\nNOTE : one choice for one
record\n";
cout<<"1) Scanning initial records\n";
cout<<"2) Appending records\n";
cout<<"3) Modifying or append records\n";
cout<<"4) Viewing records\n";
cout<<"5) Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
{
ofstream outfile;
outfile.open("emp",ios::out);
cout<<"\n\nPlease enter the details as per
demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by
him : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec= totrec + 1;
outfile.close();
}
break;
case 2 :
{
ofstream outfile;
outfile.open("emp",ios::app);
cout<<"\nPlease enter the details as per
demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name; outfile<<name<<endl;
cout<<"Enter Age : ";
int age; cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by
him: ";
char lang[25];
cin>>lang; outfile<<lang<<endl;
totrec = totrec + 1;
outfile.close();
}
break;
case 3 :
{
ofstream outfile;
outfile.open("emp",ios::ate);
cout<<"Do you want in adding record?\nEnter
(y/n)";
char ans;
cin>>ans;
if(ans=='y' || ans=='Y')
{
cout<<”\nPlease enter the details as
demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by
him:";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
}
outfile.close();
}
break;
case 4 :
{
ifstream infile;
infile.open("emp",ios::in);
const int size=80;
char line[size]; int counter=totrec;
while(counter > 0)
{
infile.getline(line,size);
cout<<"\n\nNAME : "<<line<<endl;
infile.getline(line,size);
cout<<"AGE : "<<line<<endl;
infile.getline(line,size);
cout<<"LANGUAGE : "<<line<<endl;
counter--;
}
infile.close();
}
getch();
break;
case 5 : exit(0);
break;
default:
cout<<"\nInvalid Choice\nTRY AGAIN\n";
}
}
}

OUTPUT:
ASSIGNMENT-19_
QUES-WRITE A MENU DRIVEN TELEPHONE DIRECTORY
PROGRAM (BINARY FILE HANDLING).

ANS-

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
#include<stdlib.h>
class phoneBook
{
char name[20],phno[6];
public:
void getdata();
void showdata();
char *getname()
{
return name;
}
char *getphno()
{
return phno;
}
void update(char *nm,char *telno)
{
strcpy(name,nm);
strcpy(phno,telno);
}
};
void phoneBook :: getdata()
{
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno;
}
void phoneBook :: showdata()
{
cout<<"\n";
cout<<setw(15)<<name;
cout<<” ”<<setw(8)<<phno;
}
void main()
{
phoneBook rec; fstream file;
file.open("c:\\phone.dat", ios::ate|ios::in|ios::out|ios::binary);
char ch,nm[20],telno[6]; int choice,found=0;
while(1)
{
clrscr();
cout<<"\n*****Phone Book*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone No.\n";
cout<<"4) Search Person Name\n";
cout<<"5) Update Telephone No.\n";
cout<<"6) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice)
{
case 1 : //New Record
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;
case 2 : //Display All Records
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n";
while(file)
{
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear(); getch(); break;
case 3 : //Search Tel. no. when person name known.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;
case 4 : //Search name on basis of tel. no
cout<<"\n\nEnter Telephone No : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(telno,rec.getphno())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;
case 5 : //Update Telephone No.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0; int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1; break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
}
break;
case 6 : exit(0);
break;
}
}
file.close();
}

OUTPUT:
ASSIGNMENT-20_
QUES- Write a C++ menu driven program for sorting.

ANS-
#include<iostream.h>
#include<conio.h>
void Input(int[],int);
void Bubble_sort(int[],int);
void Selection_sort(int[],int);
void Insertion_sort(int[],int);
void main()
{
clrscr();
int a[50],n,c;
cout<<"\n Enter the no. of elements to be entered in the array : ";
cin>>n;
Input(a,n);
cout<<"\n Enter the type of Sorting";
cout<<"\n 1. Bubble Sort";
cout<<"\n 2. Selection Sort";
cout<<"\n 3.Insertion Sort";
cout<<"\n Enter your choice : ";
cin>>c;
if(c==1)
Bubble_sort(a,n);
else if(c==2)
Selection_sort(a,n);
else if(c==3)
Insertion_sort(a,n);
else
cout<<"\n Invalid choice";
getch();
}
void Input(int a[],int n)
{
cout<<"\n Enter the elements of array : ";
for(int i=0;i<n;i++)
cin>>a[i];
}
void Bubble_sort(int a[],int n)
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"\n After Pass "<<i+1<<endl;
for(int k=0;k<n;k++)
cout<<a[k]<<"\t";
}
}
void Selection_sort(int a[],int n)
{
int i,j,small,pos,temp;
for(i=0;i<n;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
cout<<"\n After Pass "<<i+1<<endl;
for(j=0;j<n;j++)
cout<<a[j]<<"\t";
}
}
void Insertion_sort(int a[],int n)
{
int i,k,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
cout<<"\n After pass "<<i<<endl;
for(k=0;k<n;k++)
cout<<a[k]<<"\t";
}
}
OUTPUT:

You might also like