Professional Documents
Culture Documents
IF ARRAY IS 1,2,3
RESULTANT ARRAY : 1 2 3
1 2 0
1 0 0
private members :
A function calc_price( )which calculates and assigns the value of GPrice as follows ;
For the value of material as “COTTON” :
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( )
gets(Code);
gets(Type);
cin>>size;
gets(material);
calc_price( );
void show( )
{ clrscr();
cout<<"\n\n\t\t***********BILL************";
};
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;
cin>>a;
balance=balance+a;
void bank::withdraw()
float b;
cin>>b;
if(balance>=1000)
balance=balance-b;
else
void bank::enter()
cin>>name;
cin>>ano;
cin>>type;
cin>>balance;
void bank::display()
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*****";
cin>>ad_no;
gets(name);
cin>>fees;
void admission::display()
puts(name);
void admission::draw_no()
int n1=0,n2=0;
randomize();
n1=random(6)+10;
n2=random(6)+10;
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:
OVER()
a = 50;
b = 100;
OVER(int x, int y)
a = x;
b = y;
void Display()
};
int main()
{
OVER Object(10, 20);
OVER Object2;
// Constructor invoked.
Object.Display();
Object2.Display();
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>
class person
char name[LEN];
int age;
public:
void readperson();
void displayperson()
cout<<"\nName: ";
cout.write(name,LEN);
};
void person::readperson()
gets(name);
cin>>age;
int rollno;
float average;
public:
void readstudent()
readperson();
cin>>rollno;
cin>>average;
void disp_rollno()
float getaverage()
return average;
};
char subject[LEN];
char working;
public:
void readit();
void displaysubject()
cout.write(subject,LEN);
char workstatus()
{
return working;
};
void gradstudent::readit()
readstudent();
gets(subject);
cin>>working;
void main()
clrscr();
gradstudent grad[size];
int year,num_working=0,non_working=0,div1=0,total=0;
float topscore=0,score,number,wperc,nwperc;
cin>>year;
for(int i=0;i<size;i++)
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;
grad[i].displayperson();
grad[i].displaysubject();
nwperc=((float)non_working/(float)total)*100;
wperc=((float)div1/(float)total)*100;
getch();
}
OUTPUT-:
PROGRAM NO:-7
AIM:-
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()
cin>>rollno;
cin>>name;
void showdata()
cout<<rollno;
cout<<name;
int getrno()
{
return rollno;
s1;
void createfile()
ofstream ob("studata.dat",ios::binary);
char ch;
do
s1.getdata();
ob.write((char*)&s1,sizeof(s1));
cin>>ch;
while(ch=='y'||ch=='Y');
ob.close();
void showfile()
ifstream ob1("studata.dat",ios::binary);
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;
cin>>rno;
ob2.seekg(0);
while(!ob2.eof())
ob2.read((char*)&s1,sizeof(s1));
if(ob2.eof())
break;
if(rno==s1.getrno())
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();
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:-
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();
cin>>size1;
cin>>arr1[i];
cin>>size2;
cin>>arr2[i];
merge[i]=arr1[i];
size=size1+size2;
merge[k]=arr2[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 main()
clrscr();
int AR[50],N;
cin>>N;
for(int i=0;i<N;i++)
cin>>AR[i];
cout<<"\n";
fun(AR,N);
getch();
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;
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";
switch (choice)
cin>>x;
newptr = createnode(x);
push(newptr);
break;
case 2: pop();
break;
case 3: exit(0);
break;
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;
display(top);
void pop()
if(top==NULL)
else
{ save=top;
top=top->next;
delete save;
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;
}*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<<"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";
switch(choice)
cin>>item;
insert_beg(item);
goto again;
cin>>item;
insert_end(item);
goto again;
cin>>location;
cin>>item;
insert_spe(item,location);
goto again;
case 4:delete_beg();
goto again;
case 5:delete_end();
goto again;
cin>>location;
delete_spe(location);
goto again;
display();
goto again;
case 8 :cout<<;
default:break;
getch();
}
{temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=start;
start=temp;
{temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=NULL;
node* current=start;
while(current->next!=NULL)
{current=current->next;
current->next=temp;
{temp=(node*)malloc(sizeof(node));
temp->info=item;
node* current=start;
int count=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);
{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:-