You are on page 1of 67

INDEX

S.NO. PROGRAM DATE SIGN


1. Define a class clothing in c++ with the following
descriptions

2. Program to create the class BANK to initialize, deposit,


withdraw & display the details of customer

3. Write a program using classes and objects in which


random values are generated

4. Program to show the working of constructors

5. TO WRITE A PROGRAM TO SHOW CONSTRUCTOR


OVERLOADING

6. Program to maintain the details of college students and


print them using inheritance

7. Write a menu driven program to create at text file that

I) Count the number of words is a file.


II) Count the number of lines is a file.
III) Count and display the words starting with
vowels and store them in a new file.
IV) Count and display the words starting with
uppercase alphabets.

Count the numberof blank spaces

8. Write a program in c++ to create a binary file with 5


records and display them

9. Write a program in c++ to search a record in a binary file

10. Write a program in c++ to delete a record in a binary file

11. Given Telephon.dat containing records of class directory


{
Char name[20];
Char address[30];
Char areacode[5];
Char phone_no[11];
Public:
Void registry();
Void show();
int checkcode(char AC[])
{
return srcmp(areacode,AC);
}
Write a function COPYABC() that would copy the
records having area code as “123” from
“TELEPHON.DAT” to “TELEBACK.DAT”

12. Write a program in c++ to insert a record in a binary file

13. Write a program in c++ to modify data in a binary file

14. TO BUILD A MENU DRIVEN PROGRAM FOR DIFFERENT


SEARCHING OPEATIONS USING ARRAY MANIPULATIONS
AND DIFFERENT FUNCTIONS WITH FOLLOWING
DESCRIPTIONS:-

*LINEAR_SEARCH() :TO EXECUTE LINEAR SEARCH.


*BINARY_SEARCH() : TO EXECUTE BINARY SEARCH.

15. TO BUILD A MENU DRIVEN PROGRAM FOR INSERTION


AND DELETION USING ARRAY MANIPULATIONS AND
FUNCTIONS

16. WRITE A MENU DRIVEN PROGRAM FOR DIFFERENT


SORTING OPERATIONS USING ARRAY MANIPULATIONS
AND FUNCTIONS

17. TO BUILD A PROGRAM USING FUNCTIONS TO EXECUTE


MERGING BETWEEN TWO DIFFERENT ARRAYS

18. TO BUILD A PROGRAM USING ARRAYS WITH


FUNCTIONS IN WHICH 1-D ARRAY IS CONVERTED INTO
2-D ARRAY OF INTEGERS IN THE FOLLOWING FORMAT

IF ARRAY IS 1,2,3

RESULTANT ARRAY : 1 2 3

1 2 0

1 0 0

19. TO BUILD A MENU DRIVEN PROGRAM USING STACKS


EXECUTING POP AND PUSH OPERATIONS WITH
FUNCTIONS AND STRUCTURE

20. Write a program to show insertion and deletion from a


linked queue
PROGRAM NO:-1
AIM: Define a class clothing in c++ with the following descriptions :

private members :

code of type string


type of type string
size of type intiger
material of type string
price of type float

A function calc_price( )which calculates and assigns the value of GPrice as follows ;
For the value of material as “COTTON” :

Type price (Rs)

TROUSER 1500.
SHIRT 1200.

for material other than “COTTON”, the above mentioned GPprice price gets reduced
by 25%

public members :

 A constructor to assign initial values of code ,type and material with the word
“NOT ASSIGNED “and size and price with 0.
 A function enter() to input the values of the data members code, type, size and
material and invoke the caclPrice () function.
 A function show which displays the content of all the data members for a
clothing.

SOLUTION-:

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<stdio.h>

class clothing

{ char Code[21],Type[21];

int size;

char material[21];

float price;

void calc_price( )
{

if(strcmpi(material ,"COTTON")==0)

{ if(strcmpi(Type,"TROUSER")==0)

price=1500;

else if(strcmpi(Type,"SHIRT")==0)

price=1200;

else

{ if(strcmpi(Type,"TROUSER")==0)

price=1500*0.75;

else if(strcmpi(Type,"SHIRT")==0)

price=1200*0.75;

public:

clothing( )

{ strcpy(Code,"NOT ALLOTED");

strcpy(Type,"NOT ALLOTED");

strcpy(material,"NOT ALLOTED");

size=0;

price=0;

void enter( )

{ cout<<"\nEnter the Cloth Code: ";

gets(Code);

cout<<"\nEnter the Cloth Type: ";

gets(Type);

cout<<"\nEnter the Cloth Size: ";

cin>>size;

cout<<"\nEnter the cloth material:";

gets(material);
calc_price( );

void show( )

{ clrscr();

cout<<"\n\n\t\t***********BILL************";

cout<<"\nThe Cloth Code: "<<Code;

cout<<"\nThe Cloth Type: "<<Type;cout<<"\nThe Cloth Size: "<<size;

cout<<"\nThe Cloth Material: "<<material;

cout<<"\nThe Cloth Price: "<<price;

};

void main( )

{ clrscr();

clothing C;

C.enter( );

C.show( );

getch();

}
OUTPUT:-
PROGRAM NO:-2
AIM: Program to create the class BANK to initialize, deposit, withdraw & display the
details of customer.

SOLUTION-:

# include<iostream.h>

# include<conio.h>

class bank

char name[20];

int ano;

char type;

float balance;

public:

void deposit();

void withdraw();

void enter();

void display();

};

bank b;

void bank::deposit()

float a;

cout<<"\n enter the amount you want to deposit ";

cin>>a;

balance=balance+a;

void bank::withdraw()

float b;

cout<<"\n enter the amount you want to withdraw ";

cin>>b;
if(balance>=1000)

balance=balance-b;

else

cout<<"\n account balance is low ";

void bank::enter()

cout<<"\n enter name: ";

cin>>name;

cout<<"\n account no: ";

cin>>ano;

cout<<"\n enter type of account(S or C): ";

cin>>type;

cout<<"\n balance: ";

cin>>balance;

void bank::display()

cout<<"\n name: "<<name;

cout<<"\n account no: "<<ano;

cout<<"\n savings/current: "<<type;

cout<<"\n balance: "<<balance;

void main()

clrscr();

b.enter();

b.withdraw();

b.deposit();

b.display();

getch();
}

OUTPUT-:
PROGRAM NO:-3
AIM: Write a program using classes and objects in which random values are generated.

SOLUTION-:

# include<iostream.h>

# include<conio.h>

# include<time.h>

# include<stdio.h>

# include<stdlib.h>

class admission

int ad_no;

int klass;

char name[15];

float fees;

public:

void readdata();

void display();

void draw_no();

int getad_no()

return ad_no;

a[5];

void admission::readdata()

{cout<<"\n\n\t\t*****Fill details*****";

cout<<"\n Enter the admission number: ";

cin>>ad_no;

cout<<"\n Enter the name: ";

gets(name);

cout<<"\n Enter the class: ";


cin>>klass;

cout<<"\n Enter the fees: ";

cin>>fees;

void admission::display()

cout<<"\n Admission number: "<<ad_no;

cout<<"\n Name: ";

puts(name);

cout<<"\n Class: "<<klass;

cout<<"\n Fees: "<<fees;

void admission::draw_no()

int n1=0,n2=0;

randomize();

n1=random(6)+10;

n2=random(6)+10;

cout<<"\n Randomly generated admission numbers are "<<n1<<" and "<<n2;

for(int i=0;i<5;i++)

if((a[i].getad_no()==n1)||(a[i].getad_no()==n2))

cout<<endl;

a[i].display();

void main()

clrscr();
for(int i=0;i<5;i++)

a[i].readdata();

a[i-1].draw_no();

getch();

OUTPUT-:
PROGRAM NO:-4
AIM: Program to show the working of constructors.

SOLUTION-:

# include<iostream.h>

# include<conio.h>

class one

int x;

float y;

public:

void getdata()

cin>>x;

cin>>y;

void display()

cout<<"\n x= "<<x;

cout<<"\n y= "<<y;

};

void main()

one o1;

clrscr();

o1.display();

getch();

}
OUTPUT-:
PROGRAM NO:-5
AIM: TO WRITE A PROGRAM TO SHOW CONSTRUCTOR OVERLOADING.

SOLUTION:-

#include<iostream.h>

#include<conio.h>

class OVER

{ // Variable Declaration

int a, b;

public:

//Constructor wuithout Argument

OVER()

{ // Assign Values In Constructor

a = 50;

b = 100;

cout <<"\nIm Constructor";

//Constructor with Argument

OVER(int x, int y)

{ // Assign Values In Constructor

a = x;

b = y;

cout <<"\nIm Constructor";

void Display()

{ cout <<"\nValues :"<< a <<"\t"<< b;

};

int main()

{
OVER Object(10, 20);

OVER Object2;

// Constructor invoked.

Object.Display();

Object2.Display();

// Wait For Output Screen

getch();

return 0;

OUTPUT:-
PROGRAM NO:-6
AIM:- Program to maintain the details of college students and print them using
inheritance.

SOLUTION-:

# include<iostream.h>

# include<conio.h>

# include<stdio.h>

const int LEN=25;

class person

char name[LEN];

int age;

public:

void readperson();

void displayperson()

cout<<"\nName: ";

cout.write(name,LEN);

cout<<"\t Age: "<<age<<"\n";

};

void person::readperson()

cout<<" \n Enter name of the person: ";

gets(name);

cout<<" \n Enter age: ";

cin>>age;

class student:public person

int rollno;
float average;

public:

void readstudent()

readperson();

cout<<"\n Enter roll number: ";

cin>>rollno;

cout<<"\n Enter average marks: ";

cin>>average;

void disp_rollno()

cout<<" Roll number: "<<rollno<<"\n";

float getaverage()

return average;

};

class gradstudent:public student

char subject[LEN];

char working;

public:

void readit();

void displaysubject()

cout<<" \n Subject: ";

cout.write(subject,LEN);

char workstatus()
{

return working;

};

void gradstudent::readit()

readstudent();

cout<<"\n Enter main subject: ";

gets(subject);

cout<<" Working?(Y/N): ";

cin>>working;

void main()

clrscr();

const int size=5;

gradstudent grad[size];

int year,num_working=0,non_working=0,div1=0,total=0;

float topscore=0,score,number,wperc,nwperc;

cout<<" \n Enter year: ";

cin>>year;

for(int i=0;i<size;i++)

cout<<" Enter details for graduate "<<(i+1)<<"\n";

grad[i].readit();

total++;

if((grad[i].workstatus()=='y')||(grad[i].workstatus()=='y'))

num_working++;

else non_working++;

score=grad[i].getaverage();

if(score>topscore)
{

topscore=score;

number=i;

if(score>=60.0) div1++;

i=number;

cout<<"\n"<<"\t\t*******Report for the year******** "<<year<<"\n";

cout<<" \n Working graduates: "<<num_working;

cout<<"\n Non working graduates: "<<non_working;

cout<<"\n Details of the top scorer ";

grad[i].displayperson();

grad[i].displaysubject();

nwperc=((float)non_working/(float)total)*100;

wperc=((float)div1/(float)total)*100;

cout<<"\t Average marks: "<<grad[i].getaverage()<<"\n";

cout<<"\t"<<nwperc<<"% of the graduates this year "<<"\n"<<wperc<<"% are first


divisioners ";

getch();

}
OUTPUT-:
PROGRAM NO:-7
AIM:-

Write a menu driven program to create at text file that

V) Count the number of words is a file.


VI) Count the number of lines is a file.
VII) Count and display the words starting with vowels and store them in a new file.
VIII) Count and display the words starting with uppercase alphabets.
IX) Count the numberof blank spaces.

SOLUTION-:
# include<fstream.h>
# include<conio.h>
# include<ctype.h>
# include<stdio.h>
void word_count()
{
ifstream ob("abc.txt");
char ch[10];
int count=0;
ob.seekg(0);
while(!ob.eof())
{
if(ob.eof())
break;
ob>>ch;
count++;
}
cout<<"\n No of words in the file are ";
cout<<count;
ob.close();
}
void line_count()
{
ifstream ob2("abc.txt");
char line[100];
int count=0;
ob2.seekg(0);
while(!ob2.eof())
{
if(ob2.eof())
break;
ob2.getline(line,100);
count++;
}
cout<<"\n No of line in the file is ";
cout<<count;
ob2.close();
}
void countvowel()
{
ifstream ob6("abc.txt");
ofstream obout("txtfile.txt");
int count=0;
char wrd[10];
ob6.seekg(0);
while(!ob6.eof())
{
if(ob6.eof())
break;
ob6>>wrd;
switch(wrd[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout<<wrd;
break;
}
}
cout<<"\n No of words starting with vowels in the file is ";
cout<<count;
ob6.close();
}
void count_no_spaces()
{
ifstream ob3("abc.txt");
int count=0;
char ch;
ob3.seekg(0);
while(!ob3.eof())
{
if(ob3.eof())
break;
ob3>>ch;
if(ch==' ');
count++;
}
cout<<"\n No of spaces in the file is ";
cout<<count;
ob3.close();
}
void count_uppercase()
{
ifstream ob4("abc.txt");
int count=0;
char word[10];
ob4.seekg(0);
while(!ob4.eof())
{
if(ob4.eof())
break;
ob4>>word;
if(isupper(word[0]))
{
count++;
cout<<word;
}
}
cout<<"\n No of words starting with upper case alphabets in the file is ";
cout<<count;
ob4.close();
}
void createfile()
{
char para[500];
ofstream ob("abc.txt");
cout<<"\n Enter some contents for file!!!!! ";
gets(para);
ob<<para;
ob.close();
}
void main()
{
clrscr();
createfile();
int choice;
char ch;
cout<<"\n Menu for operation on text file ";
cout<<"\n 1. To count the number of word present in a file. ";
cout<<"\n 2. To count the number of lines present in a file. ";
cout<<"\n 3. To count the number of words starting with vowel & display them and
copy them in new file: ";
cout<<"\n 4. To count the words starting with uppercase alphabets. ";
cout<<"\n 5. To count the number of blank spaces. ";
do
{
cout<<"\n Enter your choice(1-5): ";
cin>>choice;
switch(choice)
{
case 1:word_count();
break;
case 2:line_count();
break;
case 3:countvowel();
break;
case 4:count_uppercase();
break;
case 5:count_no_spaces();
break;
default:cout<<"\n Wrong choice ";
}
cout<<"\n Do you want to continue(y/n): ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
OUTPUT-:
PROGRAM NO:-8
AIM:-
Write a program in c++ to create a binary file with 5 records and display them.

SOLUTIONS-:
# include<fstream.h>
# include<conio.h>
# include<iostream.h>
class student
{
int rollno;
char name[10];
public:
void getdata()
{
cout<<"\n Enter roll no: ";
cin>>rollno;
cout<<"\n Enter name: ";
cin>>name;
}
void showdata()
{
cout<<"\n Roll no: ";
cin>>rollno;
cout<<"\n Name: ";
cout<<name;
}
}s1;
void createfile()
{
ofstream ob("studata.dat",ios::binary);
cout<<"\n Enter 5 records: ";
for(int i=0;i<5;i++)
{
s1.getdata();
ob.write((char*)&s1,sizeof(s1));
}
ob.close();
}
void showfile()
{
ifstream ob1("studata.dat",ios::binary);
cout<<"\n Here are the entered records: ";
ob1.seekg(0);
while(!ob1.eof())
{
ob1.read((char*)&s1,sizeof(s1));
if (ob1.eof())
break;
s1.showdata();
}
ob1.close();
}
void main()
{
clrscr();
createfile();
showfile();
getch();
}

OUTPUT-:
PROGRAM NO:-9
AIM:- Write a program in c++ to search a record in a binary file.

SOLUTION-:
# include<fstream.h>
# include<conio.h>
# include<iostream.h>
class student
{
int rollno;
char name[10];
public:
void getdata()
{
cout<<"\n Enter roll no: ";
cin>>rollno;
cout<<"\n Enter name: ";
cin>>name;
}
void showdata()
{
cout<<"\n Rollno: ";
cout<<rollno;
cout<<"\n Name: ";
cout<<name;
}
int getrno()
{
return rollno;
}
}s1;
void createfile()
{
ofstream ob("studata.dat",ios::binary);
char ch;
cout<<"\n Enter records: ";
do
{
s1.getdata();
ob.write((char*)&s1,sizeof(s1));
cout<<"\n Want to enter more (Y/N)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
ob.close();
}
void showfile()
{
ifstream ob1("studata.dat",ios::binary);
cout<<"\n Here are the entered records: ";
ob1.seekg(0);
while(!ob1.eof())
{
ob1.read((char*)&s1,sizeof(s1));
if(ob1.eof())
break;
s1.showdata();
}
ob1.close();
}
void search()
{
ifstream ob2("studata.dat",ios::binary);
int rno;
cout<<"\n Enter the roll no to be searched: ";
cin>>rno;
while(!ob2.eof())
{
ob2.read((char*)&s1,sizeof(s1));
if(ob2.eof())
break;
if(rno==s1.getrno())
{
cout<<"\n RECORD FOUND!!!! ";
s1.showdata();
}
else
{ cout<<"\n Record not found!!!";
}
}
ob2.close();
}
void main()
{
clrscr();
createfile();
showfile();
search();
getch();
}
OUTPUT-:
PROGRAM NO:-10
AIM:- Write a program in c++ to delete a record in a binary file.

SOLUTIONS-:

# include<fstream.h>

# include<conio.h>

# include<iostream.h>

# include<stdio.h>

class student

int rollno;

char name[10];

public:

void getdata()

cout<<"\n Enter roll no: ";

cin>>rollno;

cout<<"\n Enter name: ";

cin>>name;

void showdata()

cout<<"\n Roll no: ";

cout<<rollno;

cout<<"\n Enter name: ";

cout<<name;

int getrno()
{

return rollno;

s1;

void createfile()

ofstream ob("studata.dat",ios::binary);

char ch;

cout<<"\n Enter records: ";

do

s1.getdata();

ob.write((char*)&s1,sizeof(s1));

cout<<"\n Want to enter more (Y/N) ";

cin>>ch;

while(ch=='y'||ch=='Y');

ob.close();

void showfile()

ifstream ob1("studata.dat",ios::binary);

cout<<"\n Here are the entered records: ";

ob1.seekg(0);

while(!ob1.eof())

ob1.read((char*)&s1,sizeof(s1));

if(ob1.eof())

break;

s1.showdata();
}

ob1.close();

void del()

ifstream ob2("studata.dat",ios::binary);

ofstream ob3("temp.dat",ios::binary);

int rno;

char cnf;

cout<<"\n Enter the roll no to be deleted: ";

cin>>rno;

ob2.seekg(0);

while(!ob2.eof())

ob2.read((char*)&s1,sizeof(s1));

if(ob2.eof())

break;

if(rno==s1.getrno())

cout<<"\n Are you sure to delete this record: ";

cin>>cnf;

if(cnf=='n'||cnf=='Y')

ob3.write((char*)&s1,sizeof(s1));

else

ob3.write((char*)&s1,sizeof(s1));

ob2.close();

ob3.close();

remove("studata.dat");

rename("temp.dat","studata.dat");
}

void main()

clrscr();

createfile();

showfile();

del();

cout<<"\n NOW THE FILE CONTAINS!!!! ";

showfile();

getch();

OUTPUT-:
PROGRAM NO:-11
AIM:- Given Telephon.dat containing records of class directory
{
Char name[20];
Char address[30];
Char areacode[5];
Char phone_no[11];
Public:
Void registry();
Void show();
int checkcode(char AC[])
{
return srcmp(areacode,AC);
}
Write a function COPYABC() that would copy the records having area code as “123” from
“TELEPHON.DAT” to “TELEBACK.DAT”

SOLUTION-:
# include<fstream.h>
# include <conio.h>
#include<string.h>
class directory
{
char name[20];
char address[30];
char areacode[5];
char phone_no[11];
public:
void registry();
void show();
int checkcode(char AC[])
{
return strcmp(areacode,AC);
}
}
d1;
void directory::registry()
{
cout<<"\n enter name: ";
cin>>name;
cout<<"\n enter adddress: ";
cin>>address;
cout<<"\n enter area code: ";
cin>>areacode;
cout<<"\n phone no: ";
cin>>phone_no;
}
void directory::show()
{
cout<<"\n name: ";
cout<<name;
cout<<"\n address: ";
cout<<address;
cout<<"\n area code: ";
cout<<areacode;
cout<<"\n phone no: ";
cout<<phone_no;
}
void createfile()
{
ofstream ob("TELEPHON.DAT",ios::binary);
char ch;
do
{
d1.registry();
ob.write((char*)&d1,sizeof(d1));
cout<<"\n enter more records??? ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
ob.close();
}
void showfile()
{
ifstream ob1("TELEPHON.DAT",ios::binary);
ob1.seekg(0);
while(!ob1.eof())
{
ob1.read((char*)&d1,sizeof(d1));
if(ob1.eof())
break;
d1.show();
}
ob1.close();
}
void COPYABC()
{
ifstream ob2("TELEPHON.DAT",ios::binary);
ofstream ob3("TELEBACK.DAT",ios::binary);
ob2.seekg(0);
cout<<"\n COPYING RECORDS!!!! ";
while(!ob2.eof())
{
ob2.read((char*)&d1,sizeof(d1));
if(ob2.eof())
break;
if(d1.checkcode("123")==0)
ob3.write((char*)&d1,sizeof(d1));
}
ob2.close();
ob3.close();
}
void shownewfile()
{
ifstream ob4("TELEBACK.DAT",ios::binary);
ob4.seekg(0);
while(!ob4.eof())
{
ob4.read((char*)&d1,sizeof(d1));
if(ob4.eof())
break;
d1.show();
}
ob4.close();
}
void main()
{
clrscr();
createfile();
showfile();
COPYABC();
shownewfile();
getch();
}
OUTPUT-:
PROGRAM NO:-12
AIM:-Write a program in c++ to insert a record in a binary file.

SOLUTIONS-:
# include<fstream.h>
# include<conio.h>
# include<iostream.h>
# include<stdio.h>
class student
{
int rollno;
char name[10];
public:
void getdata()
{
cout<<"\n enter roll no: ";
cin>>rollno;
cout<<"\n enter name: ";
cin>>name;
}
void showdata()
{
cout<<"\n roll no: ";
cout<<rollno;
cout<<"\n enter name: ";
cout<<name;
}
int getrno()
{
return rollno;
}
}
s1,stud;
void createfile()
{
ofstream ob("studata.dat",ios::binary);
char ch;
cout<<"\n enter records: ";
do
{
s1.getdata();
ob.write((char*)&s1,sizeof(s1));
cout<<"\n want to enter more (Y/N) ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
ob.close();
}
void showfile()
{
ifstream ob1("studata.dat",ios::binary);
cout<<"\n here are the entered records: ";
ob1.seekg(0);
while(!ob1.eof())
{
ob1.read((char*)&s1,sizeof(s1));
if(ob1.eof())
break;
s1.showdata();
}
ob1.close();
}
void insert()
{
ifstream ob2("studata.dat",ios::binary);
ofstream ob3("temp.dat",ios::binary);
char last='y';
cout<<"\n enter details to insert: ";
stud.getdata();
ob2.seekg(0);
while(!ob2.eof())
{
if(ob2.eof())
break;
ob2.read((char*)&s1,sizeof(s1));
if(stud.getrno<=s1.getrno())
{
ob3.write((char*)&stud,sizeof(stud));
last='n';
break;
}
else
ob3.write((char*)&s1,sizeof(s1));
}
if(last=='y')
ob3.write((char*)&stud,sizeof(stud));
else if(!ob2.eof())
{
while(!ob2.eof())
{
ob2.read((char*)&s1,sizeof(s1));
ob3.write((char*)&s1,sizeof(s1));
}
}
ob2.close();
ob3.close();
remove("studata.dat");
rename("temp.dat","studata.dat");
}
void main()
{
clrscr();
createfile();
showfile();
insert();
cout<<"\n FILE AFTER EDITING!!!! ";
showfile();
getch();
}

OUTPUT-:
PROGRAM NO:-13
AIM:-Write a program in c++ to modify data in a binary file.

SOLUTION:-

# include<fstream.h>
# include<conio.h>
# include<iostream.h>
# include<stdio.h>
class student
{
int rollno;
char name[10];
public:
void getdata()
{
cout<<"\n enter roll no: ";
cin>>rollno;
cout<<"\n enter name: ";
cin>>name;
}
void showdata()
{
cout<<"\n roll no: ";
cout<<rollno;
cout<<"\n enter name: ";
cout<<name;
}
int getrno()
{
return rollno;
}
}
s1,stud;
void createfile()
{
ofstream ob("studata.dat",ios::binary);
char ch;
cout<<"\n enter records: ";
do
{
s1.getdata();
ob.write((char*)&s1,sizeof(s1));
cout<<"\n want to enter more (Y/N) ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
ob.close();
}
void showfile()
{
ifstream ob1("studata.dat",ios::binary);
cout<<"\n here are the entered records: ";
ob1.seekg(0);
while(!ob1.eof())
{
ob1.read((char*)&s1,sizeof(s1));
if(ob1.eof())
break;
s1.showdata();
}
ob1.close();
}
void insert()
{
ifstream ob2("studata.dat",ios::binary);
ofstream ob3("temp.dat",ios::binary);
char last='y';
cout<<"\n enter details to insert: ";
stud.getdata();
ob2.seekg(0);
while(!ob2.eof())
{
if(ob2.eof())
break;
ob2.read((char*)&s1,sizeof(s1));
if(stud.getrno<=s1.getrno())
{
ob3.write((char*)&stud,sizeof(stud));
last='n';
break;
}
else
ob3.write((char*)&s1,sizeof(s1));
}
if(last=='y')
ob3.write((char*)&stud,sizeof(stud));
else if(!ob2.eof())
{
while(!ob2.eof())
{
ob2.read((char*)&s1,sizeof(s1));
ob3.write((char*)&s1,sizeof(s1));
}
}
ob2.close();
ob3.close();
remove("studata.dat");
rename("temp.dat","studata.dat");
}
void main()
{
clrscr();
createfile();
showfile();
insert();
cout<<"\n FILE AFTER EDITING!!!! ";
showfile();
getch();
}
OUTPUT-:
PROGRAM NO:14
AIM:
TO BUILD A MENU DRIVEN PROGRAM FOR DIFFERENT SEARCHING OPEATIONS USING
ARRAY MANIPULATIONS AND DIFFERENT FUNCTIONS WITH FOLLOWING DESCRIPTIONS:-

*LINEAR_SEARCH() :TO EXECUTE LINEAR SEARCH.


*BINARY_SEARCH() : TO EXECUTE BINARY SEARCH.

SOLUTIONS-:
#include<iostream.h>
#include<conio.h>
void linear_search(int b[],int size, int n)
{
char found='n';
for(int p=0;p<size;p++)
{
if(n==b[p])
{
cout<<"\n"<<b[p];
cout<<"\n Position is"<<p+1;
found='y';
}
}
if(found=='n')
cout<<"\n Element not found";
}
void binary_search(int b[],int size, int n)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(n==b[mid])
{
cout<<"\n Element is "<<b[mid];
cout<<"\n Position is "<<mid+1;
break;
}
else if (n>b[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
}
void main()
{
clrscr();
int a[20];
int s,x,choice;
cout<<"\n Enter the size";
cin>>s;
cout<<"\n Enter the array element";
for(int i=0;i<s;i++)
{
cin>>a[i];
}
cout<<"\n Array elements are:";
for(int j=0;j<s;j++)
{
cout<<"\n"<<a[j];
}
cout<<"\n Enter the element to be searched";
cin>>x;
cout<<"\n Which searching operation do you want to use?";
cout<<"\n 1.linear search";
cout<<"\n 2.binary search";
cin>>choice;
switch(choice)
{
case 1 :linear_search(a,s,x);
break;
case 2 :binary_search(a,s,x);
break;
default:cout<<"\n Wrong choice";
}
getch();
}
OUTPUT-:
PROGRAM NO:15
AIM: TO BUILD A MENU DRIVEN PROGRAM FOR INSERTION AND DELETION USING ARRAY
MANIPULATIONS AND FUNCTIONS.

SOLUTION-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,x,c=0,e,ch,i,a[50],j;
cout<<"Enter Array lim:"<<endl;
cin>>n;
cout<<"Enter array:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter Choice 1:Deletion 2:Insertion"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter to be deleted:"<<endl;
cin>>x;
for(i=0;i<n;i++)
{

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

case 2:
cout<<"Enter What & Where to be inserted"<<endl;
cin>>e>>x;
for(i=n;i>=x;i--)
a[i]=a[i-1];
a[x-1]=e;
for(i=0;i<=n;i++)
cout<<a[i]<<endl;
break;
}
getch();
}

OUTPUT-:
PROGRAM NO:16
AIM: WRITE A MENU DRIVEN PROGRAM FOR DIFFERENT SORTING OPERATIONS USING
ARRAY MANIPULATIONS AND FUNCTIONS.

SOLUTIONS-:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch;
int i,j,x,k,z,l,m,n,o,p,a[50],small;
q:
cout<<"Enter the choice 1:Selection 2:Bubble 3:Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the limit :"<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements:"<<endl;

for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}

for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
for(k=p;k>j;k--)
{
a[k]=a[k-1];
}
a[j]=small;
}
cout<<"Sorted series"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;
case 2:
cout<<"Enter the limit:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if (a[i]>a[i+1])
{
x=a[i+1];
a[i+1]=a[i];
a[i]=x;
}
}
}
cout<<"Sorted series"<<endl;
for (i=0;i<n;i++)
{
cout<<a[i];
cout<<endl;
}
break;
case 3:
exit(0);
default:
cout<<"Wrong choice";
goto q;

}
getch();
}
OUTPUT-:
PROGRAM NO:-17
AIM: TO BUILD A PROGRAM USING FUNCTIONS TO EXECUTE MERGING BETWEEN TWO
DIFFERENT ARRAYS .

SOLUTION-:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];

cout<<"Enter Array 1 Size : ";

cin>>size1;

cout<<"Enter Array 1 Elements : ";

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

cin>>arr1[i];

cout<<"Enter Array 2 Size : ";

cin>>size2;

cout<<"Enter Array 2 Elements : ";

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

cin>>arr2[i];

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

merge[i]=arr1[i];

size=size1+size2;

for(i=0, k=size1; k<size && i<size2; i++, k++)


{

merge[k]=arr2[i];

cout<<"Now the new array after merging is :\n";

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

cout<<merge[i]<<"";

getch();

OUTPUT:-
PROGRAM NO:-18
AIM: TO BUILD A PROGRAM USING ARRAYS WITH FUNCTIONS IN WHICH 1-D ARRAY IS
CONVERTED INTO 2-D ARRAY OF INTEGERS IN THE FOLLOWING FORMAT-

IF ARRAY IS 1,2,3

RESULTANT ARRAY : 1 2 3

1 2 0

1 0 0

SOLUTION:-

#include<iostream.h>

#include<conio.h>

void fun(int AR[ ],int size);

void main()

clrscr();

int AR[50],N;

cout<<"\n ENTER THE DESIRED ARRAY SIZE:";

cin>>N;

cout<<"\n ENTER THE 1-D ARRAY ELEMENTS:";

for(int i=0;i<N;i++)

cin>>AR[i];

cout<<"\n";

fun(AR,N);

getch();

void fun(int AR[],int size)

int A2[20][20];

int i,j;

for(i=0;i<size;i++)
for(j=0;j<size;j++)

if((i+j)>=size)

A2[i][j]=0;

else

A2[i][j]=AR[j];

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

for(j=0;j<size;j++)

cout<<""<<A2[i][j];

cout<<"\n";

OUTPUT:-
PROGRAM NO:- 19
AIM: TO BUILD A MENU DRIVEN PROGRAM USING STACKS EXECUTING POP AND PUSH
OPERATIONS WITH FUNCTIONS AND STRUCTURE .

SOLUTION:-

#include<iostream.h>

#include<conio.h>

#include<process.h>

struct Node

int info;

Node* next;

}*ptr,*newptr,*save,*top;

Node * createnode(int y);

void push(Node*np);

void display(Node*np);

void pop();

void main()

clrscr ();

top =NULL;

char ch='y';

int choice;

int x;

do

cout<<"\n ****MENU****";

cout<<"\n 1: Push";

cout<<"\n 2: POP";

cout<<"\n 3: EXIT";

cout<<"\n enter your choice: ";


cin>>choice;

switch (choice)

case 1: cout<<"\n enter value of x:";

cin>>x;

newptr = createnode(x);

push(newptr);

break;

case 2: pop();

break;

case 3: exit(0);

break;

default: cout<<"\n not a valid choice";

cout<<"\n want to see again(y/n): ";

cin>>ch;

}while(ch=='y');

getch();

Node* createnode(int y)

ptr=new Node;

ptr->info=y;

ptr->next = NULL;

return ptr;

void push(Node*np)

if(top==NULL)

top =np;

else
{

save = top;

top = np ;

np->next=save;

cout<<"\n now stack contain ";

display(top);

void pop()

if(top==NULL)

cout<<"\n UNIDERFLOW ";

else

{ save=top;

top=top->next;

delete save;

cout<<"\n now stack contain ";

display(top);

void display(Node*np)

while(np!=NULL)

cout<<np->info<<" ->";

np=np->next;

cout<<"\n";

}
OUTPUT:-
PROGRAM NO:20
AIM: Write a program to show insertion and deletion from a linked queue.

SOLUTION:

#include<iostream.h>

#include<conio.h>

#include<malloc.h>

struct node

{int info;

struct node *next;

}*start, *temp;

void insert_beg(int);

void insert_end(int);

void insert_spe(int,int);

void delete_beg();

void delete_end();

void delete_spe(int);

void display();

void main()

{clrscr();

start=NULL;

int item,choice,location,element,position;

again:

cout<<"\n1.Insert at beg of linked list\t";

cout<<"2.Insert at end of linked list\n";

cout<<"3.Insert at specific location\t";

cout<<"4.Delete from beginning of List \n5.delete from end of the list \t6.Delete from
specific location of List\n7.Display linked list\t8.Exit\n";

cout<<"Enter choice : ";


cin>>choice;

switch(choice)

{case 1:cout<<"Enter item to insert : ";

cin>>item;

insert_beg(item);

goto again;

case 2:cout<<"Enter item to insert : ";

cin>>item;

insert_end(item);

goto again;

case 3:cout<<"Enter location to insert : ";

cin>>location;

cout<<"Enter item to insert : ";

cin>>item;

insert_spe(item,location);

goto again;

case 4:delete_beg();

goto again;

case 5:delete_end();

goto again;

case 6:cout<<"Enter the location to delete : ";

cin>>location;

delete_spe(location);

goto again;

case 7:cout<<"\nInserted item = ";

display();

goto again;

case 8 :cout<<;

default:break;

getch();
}

void insert_beg(int item)

{temp=(node*)malloc(sizeof(node));

temp->info=item;

temp->next=start;

start=temp;

void insert_end(int item)

{temp=(node*)malloc(sizeof(node));

temp->info=item;

temp->next=NULL;

node* current=start;

while(current->next!=NULL)

{current=current->next;

current->next=temp;

void insert_spe(int item,int location)

{temp=(node*)malloc(sizeof(node));

temp->info=item;

node* current=start;

int count=1;

while (count <location-1)

{current=current->next;

count=count+1;

temp->next=current->next;

current->next=temp;
}

void delete_beg()

{temp=start;

start=start->next;

free(temp);

void delete_end()

{temp=start;

node* current;

while(temp->next!=NULL)

current=temp;

temp=temp->next;

current->next=NULL;

free(temp);

void delete_spe(int location)

{node* current;

temp=start;

int count=1;

while(count<=location-1)

{current=temp;

temp=temp->next;

count=count+1;

current->next=temp->next;

free(temp);
}

void display()

{temp=start;

while(temp!=NULL)

{cout<<temp->info<<" ";

temp=temp->next;

cout<<"\n";

}
OUTPUT:-

You might also like