Вы находитесь на странице: 1из 61

1

//1.WAP to print the factorial of a given number.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int fact=1,num,i;
clrscr();
cout<<"\nEnter number:";
cin>>num;
i=num;
while(num)
{ fact*=num;
--num;
}
cout<<"The factorial of "<<i<<" is "<<fact<<"\n";
getch();
}

OUTPUT:

Q-2.WAP to print fibonnacci series of specified terms.


Ans#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int c,n,first=0,second=1,next;
cout<<"enter the no of yerms of fibonacci series you want"<<endl;
cin>>n;
for(c=0;c<n;c++)
{ if(c<=1)
next=c;
else
{ next=first+second;
first=second;
second=next;
}
cout<<next<<endl;
}
getch();
}

OUTPUT:

Q-3.wap to print the sum of digits of number also print reverse.


Ans#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,r,a,sum;
sum=0;
cout<<"input the no";
cin>>a;
for(n=a;n>0;n=n/10)
{ r=n%10;
sum=sum+r;
cout<<r<<"";
}
cout<<"\nsum is "<<sum;
getch();
}

OUTPUT:

Q-4.wap to check weather a no. is prime or not.


Ans#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{ clrscr();
int i,n;
cout<<"input the no";
cin>>n;
for(i=2;i<=n/2;++i)
if(n%i==0)
{ cout<<"\n not a prime number";
exit(0);
}
cout<<"\n it is a prime number";
getch();
}

OUTPUT:

Q-5.wap to check weather a string is palindrome or not.


Ans#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,j,p=1;
char arr[10];
cout<<"enter the string"<<endl;
cin>>arr;
for(n=0;arr[n]!='\0';n++);
n--;
for(j=0;j<n;j++,n--)
{ if(arr[j]!=arr[n])
p=0;
}
if(p==1)
cout<<"palindrome string"<<endl;
else
cout<<"not a palindrome string";
getch();
}

10

OUTPUT:

11

//6.WAP to count the number of occurence ofa character in a string.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{ clrscr();
char arr[20],ch;
int n,count=0;
cout<<"Input any string ";
gets(arr);
cout<<"Input any character ";
cin>>ch;
for(n=0;arr[n]!='\0';n++)
{
if(arr[n]==ch)
count++;
}
cout<<"Occorance of character "<<count;
getch();
}

12

OUTPUT:

13

//7.WAP to swap two numbers using call by reference method without using third variable
#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
b = a + b;
a = b - a;
b = b - a;
}
void main()
{
int a, b;
clrscr();
cout << "Enter first number a: ";
cin >> a ;
cout<<"Enter second number b:";
cin>>b;
swap(a, b);
cout << "The two numbers after swapping become :" << endl;
cout << "Value of a : " << a << endl;
cout << "Value of b : " << b << endl;
getch();
}

14

OUTPUT:

15

/*8.write a menu driven program to sort an array in ascending order using following sorting methods
1.Creation of an Array
2.Binary Search
3.Linear Search
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void array()
{
int a[100],j;
cout<<"enter number of element : ";
cin>>j;
cout<<"\nenter the value : \n";
for(int i=0;i<j;i++)
{ cout<<"Input any no:";
cin>>a[i];
}
for(i=0;i<j;i++)
cout<<" "<<a[i]<<" ";
}
void linear()
{
int a[100],b,c,d;
cout<<"enter the number of elements in array : \n";
cin>>d;
cout<<"enter "<<d<<" integer \n";
for(c=0;c<d;c++)
cin>>a[c];
cout<<"enter the number to search : \n";
cin>>b;
for(c=0;c<d;c++)
{
if(a[c]==b)
cout<<b<<" is present al location : "<<c+1;
}

16
if(c==d)
cout<<" is not present in array ";
}
void binary()
{
int a,first,last,mid,b,c,d[100];
cout<<"enter the number of element : \n";
cin>>b;
cout<<"enter "<<b<<" integer";
for(a=0;a<b;a++)
cin>>d[a];
cout<<"enter the value to find \n";
cin>>c;
first=0;
last=b-1;
mid=(first+last)/2;
while(first<=last)
{
if(d[mid]==c)
{
cout<<c<<" found at location "<<mid+1<<"\n";
break;
}
else
if(d[mid]<c)
first=mid+1;
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
cout<<"not found "<<c<<" is not present in the list";
}
void main()
{
clrscr();
int a;
while(1)
{ clrscr();
cout<<"menu \n 1.creation of array \n 2.linear search \n 3.binary search \n";
cout<<"\n enter your choice : ";
cin>>a;
switch(a)
{ case 1:array();
break;
case 2:linear();
break;
case 3:binary();
break;

17
case 0:exit(0);
default:cout<<"Invalid choice";
}
}
getch();
}

OUTPUT:

18

//9.WAP to merge two arrays A and B in descending order


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
clrscr();
cout<<" Enter the no. of element for 1st array : ";
cin>>n1;
for(i=0;i<n1;i++,k++)
{
cout<<" Enter element : "<<i+1<<" ";
cin>>a[i];
c[k]=a[i];
}
for(i=0;i<n1;i++)
{
for(j=i+1;j<n1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

19
cout<<"\n After sorting 1st array : ";
for(i=0;i<n1;i++)
{
cout<<"\n Element "<<i+1<<" "<<a[i];
}
cout<<"\n\n Enter the no. of element for 2nd array : ";
cin>>n2;
for(i=0;i<n2;i++,k++)
{
cout<<" Enter element : "<<i+1<<" ";
cin>>b[i];
c[k]=b[i];
}
for(i=0;i<n2;i++)
{
for(j=i+1;j<n2;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
cout<<"\n After sorting 2nd array : ";
for(i=0;i<n2;i++)
{
cout<<"\n Element"<<i+1<<" "<<b[i];
}
for(i=0;i<n1+n2;i++)
{
for(j=i+1;j<n1+n2;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
cout<<"\n\n\n After combined and sorted both array :- ";
for(i=0;i<n1+n2;i++)
{

20
cout<<"\n Element"<<i+1<<" "<<c[i];
}
getch();
}

OUTPUT:

21

//10.WAP to make a structure of student with the following details (name,age,class,marks,average,result)&get


input from the user for the data name,age,class,marks and find the average and result of the student if
average>=40 result'p' if not result'f'
#include<iostream.h>
#include<conio.h>
struct student
{ char name[40],clas,result;
float marks[5],avg;
int rno,age;
};
student a;
void main()
{ clrscr();
cout<<"\n enter name ";
cin>>a.name;
cout<<"\n enter roll no. ";
cin>>a.rno;
cout<<"\nenter class ";
cin>>a.clas;
cout<<"\nenter age ";
cin>>a.age;
cout<<"\n enter marks of 5 subjects";
float total=0;
for(int i=0;i<5;i++)
{ cout<<"\nsubject "<<i+1<<":";
cin>>a.marks[i];
}
total=(a.marks[0]+a.marks[1]+a.marks[2]+a.marks[3]+a.marks[4]);
a.avg=(total/5);
if(a.avg>=40)

22
a.result='p';
else
a.result='f';
cout<<"\n\nstudent result:\n";
cout<<"roll no.:"<<a.rno;
cout<<"\tname:"<<a.name;
cout<<"\tclass:"<<a.clas;
cout<<"\tavg:"<<a.avg;
cout<<"\ntotal marks:"<<total;
cout<<"\tage:"<<a.age;
cout<<"\t result:"<<a.result;
getch();
}

OUTPUT:

23

/
/11.WAP a program to check a valid date using structure(dd,mm,yy).
#include<iostream.h>
#include<conio.h>
struct date
{ int dd,mm,yy;
};
void main()
{ clrscr();
date d;
cout<<"Enter date (DD/MM/YYYY format): ";
cin>>d.dd>>d.mm>>d.yy;
if((d.dd>=1&&d.dd<=31)&&(d.mm>=1&&d.mm<=12))
{ cout<<"Valid date";
}
else
{ cout<<"Invalid date";
}
getch();
}

24

OUTPUT:

25

/*12.WAP to define a class worker shown below


class worker(
wname chracter(20),
wno integer,
wgratefloat,
calcwage()
public
worker();
indata();
outdata();
)
wap to create an array of n objects and display
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class worker
{ char wname[40];
int wno;
float wgrate,hrwg,twage;
int calcwage()
{ return(hrwg*wgrate);
}
public:
worker()
{ wno=0;

26
strcpy(wname,"");
wgrate=0;
hrwg=0;
twage=0;
}
void indata()
{ cout<<"Enter name:";
cin>>wname;
cout<<"Enter worker no.:";
cin>>wno;
cout<<"Enter hours work:";
cin>>hrwg;
cout<<"Enter work rate:";
cin>>wgrate;
twage=calcwage();
}
void outdata()
{ cout<<"\nName:"<<wname<<endl;
cout<<"Worker no.:"<<wno<<endl;
cout<<"Hours work:"<<hrwg<<endl;
cout<<"Work rate:"<<wgrate<<endl;
cout<<"Total wage:"<<twage<<endl;
}
};
void main()
{ clrscr();
int n;
worker obj[5];
cout<<"Number of entries:";
cin>>n;
for(int i=0;i<n;i++)
{ obj[i].indata();
}
for(i=0;i<n;i++)
{ obj[i].outdata();
}
getch();
}

27

OUTPUT:

28

/*13.WAP to do the single inheritance for the following


class person:private members name string of 40, age integer
public members:indata() to get the values for data members
outdata() to display values of data members.
class student:private members rollno integer,marks float .
public members calgrade() to calculate grade according to the following criteria:
marks
grade
<40
D
>40&&<50 C
>50&&<80 B
>80
A
*/
#include<iostream.h>
#include<conio.h>
class person
{ char name[40];
int age;
public:
void indata()
{ cout<<"Enter name ";
cin>>name;
cout<<"\nEnter age ";
cin>>age;

29
}
void outdata()
{ cout<<"\nName "<<name<<endl;
cout<<"\nage "<<age<<endl;
}
};
class student:public person
{ int rno;
char grade;
float marks;
public:
void calgrade()
{ if(marks>80)
grade='A';
else if(marks>60)
grade='B';
else if(marks>50)
grade='C';
else
grade='D';
}
void enter()
{ cout<<"\nEnter the roll number ";
cin>>rno;
cout<<"\nEnter the marks ";
cin>>marks;
calgrade();
}
void display()
{ cout<<"\nRoll no.: "<<rno<<endl;
cout<<"\nmarks: "<<marks<<endl;
cout<<"\nGrade: "<<grade<<endl;
}
};
void main()
{ clrscr();
student obj;
obj.indata();
obj.enter();
obj.outdata();
obj.display();
getch();
}

30

OUTPUT:

31

/*14.WAP to do multiple inheritance for the following classes


class game:private members:gamename string of 50
public members input(),output()
constructor function to initialize gamename with"#empty",
class person:private members:name string of 40,age integer
public members:indata(),outdata()
constructor function to initialize name with"#empty" and age with 0.
class student:private members:Roll no integer,marks float
public members:calcgrade() to calculate grade according to the following criteria:
Marks Grade
<40
D
>40&&<50 C
>50&&<80 B
>80
A
enter(),display()
constructor function to initialize rollno and marks with 0
also define destructor to display a good bye message*/
#include<iostream.h>

32
#include<conio.h>
#include<string.h>
class game
{ char gamename[50];
public:
void input()
{ cout<<"enter gamename ";
cin>>gamename;
}
void output()
{ cout<<"game name "<<gamename<<endl;
}
game()
{ strcpy(gamename,"#empty");
}
};
class person:public game
{ char name[40];
int age;
public:
void indata()
{ cout<<"Enter name ";
cin>>name;
cout<<"Enter age ";
cin>>age;
}
void outdata()
{ cout<<"Name "<<name<<endl;
cout<<"age "<<age<<endl;
}
person()
{ strcpy(name,"#empty");
age=0;
}
};
class student:public person
{ int rno;
char grade;
float marks;
public:
void calgrade()
{ if(marks>80)
grade='A';
else if(marks>60)
grade='B';
else if(marks>50)
grade='C';
else
grade='D';

33
}
void enter()
{ cout<<"Enter the roll number ";
cin>>rno;
cout<<"Enter the marks ";
cin>>marks;
calgrade();
}
void display()
{ cout<<"Roll no.: "<<rno<<endl;
cout<<"marks: "<<marks<<endl;
cout<<"Grade: "<<grade<<endl;
}
student()
{ rno=marks=0;
}
~student()
{ cout<<"Good Bye";
}
};
void main()
{ clrscr();
student obj;
obj.indata();
obj.enter();
obj.input();
obj.outdata();
obj.display();
obj.output();
getch();
}
OUTPUT:

34

/* 15.Write a menu driven program to push and pop operation in static stack using array of structure for structure
student.
struct student
{ int age;
char name[40];
};
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct student
{ int age;
char name[40];
};
const int size=20;

35
class stack
{ struct student info[size];
int top;
public:
stack()
{ top=-1;
}
void push(student);
void pop();
void disp();
};
void stack::push(student v)
{ if(top<size)
{ top++;
info[top]=v;
}
else
{ cout<<"Stack overflow";
}
}
void stack::pop()
{ if(top<0)
{ cout<<"stack underflow";
}
else
{ cout<<"Popped item";
cout<<"Name"<<info[top].name<<endl;
cout<<"Age"<<info[top].age<<endl;
top--;
}
}
void stack::disp()
{ int n;
for(n=top;n>=0;n--)
{ cout<<"\n\tName"<<info[n].name;
cout<<"\n\tAge"<<info[n].age;
}
}
void main()
{ clrscr();
int n,item;
stack obj;
student s;
while(1)
{ clrscr();
cout<<"\n\t1.Push";
cout<<"\n\t2.Pop";
cout<<"\n\t3.Disp";
cout<<"\n\t0.Exit";

36
cout<<"\n\tInput your choice";
cin>>n;
switch(n)
{ case 1:cout<<"Name";
cin>>s.name;
cout<<"Age";
cin>>s.age;
obj.push(s);
break;
case 2:obj.pop();
break;
case 3:obj.disp();
break;
case 0:exit(0);
break;
default:cout<<"\n\tInvalid choice";
}
getch();
}
}

OUTPUT:

37

/*16. Write a menu driven program to do insertion and deletion in static queue of integer
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int size=20;
class queues
{ int front,rear;
int arr[size];
public:
queues()
{ front=-1;
rear=-1;

38
}
void add(int
);
void del();
void disp();
};
void queues::add(int item)
{ if(front==-1)
{ front=0;
rear=0;
arr[rear]=item;
}
else
{
rear=rear+1;
arr[rear]=item;
}
cout<<"One record added"<<endl;
}
void queues::del()
{ if(front==-1)
{ cout<<"Queue is empty";
getch();
return;
}
else if(front==rear)
{ cout<<"Delete item"<<arr[front]<<endl;
front=-1;
rear=-1;
}
else
{ cout<<"Deleyed item"<<arr[front];
front++;
}
}
void queues::disp()
{ int n;
for(n=front;n<=rear;n++)
cout<<arr[n]<<endl;
}
void main()
{ clrscr();
int n,item;
queues obj;
while(1)
{ clrscr();
cout<<"\n\t1.Add record";
cout<<"\n\t2.Delete record";

39
cout<<"\n\t3.Display";
cout<<"\n\t0.Exit";
cout<<"\n\tInput your choice";
cin>>n;
switch(n)
{ case 1:cout<<"Input item";
cin>>item;
obj.add(item);
break;
case 2:obj.del();
break;
case 3:obj.disp();
break;
case 0:exit(0);
break;
default:cout<<"\n\tInvalid choice";
}
getch();
}
}

OUTPUT:

40

/* 17.Write a menu driven program to delete and insert a node containing books information from dynamically
allocated stack of books implemented with the help of the following structure
struct book
{ int bno;
char bname[40];
book*next;
};
*/
#include<iostream.h>

41
#include<conio.h>
#include<stdlib.h>
struct book
{ int bno;
char bname[40];
book*next;
};
class list
{ book*start;
public:
list()
{ start=NULL;
}
void push();
void pop();
void disp();
};
void list::push()
{ book*ptr;
ptr=new book;
cout<<"book no ";
cin>>ptr->bno;
cout<<"book name";
cin>>ptr->bname;
ptr->next=start;
start=ptr;
cout<<"One record pushed";
}
void list::pop()
{ book*ptr;
if(start==NULL)
{ cout<<"stack under flow";
getch();
return;
}
ptr=start;
start=ptr->next;
delete ptr;
cout<<"one record Popped";
}
void list::disp()
{ book*ptr;
if(start==NULL)
{ cout<<"list is empty";
getch();
return;
}
for(ptr=start;ptr!=NULL;ptr=ptr->next)

42
{ cout<<"\n\tBook no"<<ptr->bno;
cout<<"\n\tBook name"<<ptr->bname;
}
}
void main()
{ clrscr();
int n;
list obj;
while(1)
{ clrscr();
cout<<"\n\t1.Push";
cout<<"\n\t2.Pop";
cout<<"\n\t3.Disp";
cout<<"\n\t0.Exit";
cout<<"\n\tInput your choice";
cin>>n;
switch(n)
{ case 1:obj.push();
break;
case 2:obj.pop();
break;
case 3:obj.disp();
break;
case 0:exit(0);
break;
default:cout<<"\n\tInvalid choice";
}
getch();
}
}

OUTPUT:

43

44
/* 18.Write a menu driven program t0 delete and insert a node containing books information from dynamically
allocated circuler of books implemented with the help of the following structure
struct book
{ int bno;
char bname[40];
book*next;
};
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct book
{ int bno;
char bname[40];
book*next;
};
class list
{ book*front,*rear;
public:
list()
{ front=NULL;
rear=NULL;
}
void add();
void del();
void disp();
};
void list::add()
{ book*ptr;
ptr=new book;
cout<<"book no ";
cin>>ptr->bno;
cout<<"book name";
cin>>ptr->bname;
ptr->next=NULL;
if(front==NULL)
{ front=ptr;
rear=ptr;
}
rear->next=ptr;
rear=ptr;
cout<<"one record added";
}
void list::del()
{ book*ptr;
if(front==NULL)
{ cout<<"queues is empty";
getch();
return;

45
}
else if(front==rear)
{ ptr=front;
front=NULL;
rear=NULL;
}
else
{ ptr=front;
front=ptr->next;
rear->next=front;
delete ptr;
}
cout<<"one record deleted";
}
void list::disp()
{ book*ptr;
if(front==NULL)
{ cout<<"queues is empty";
getch();
return;
}
for(ptr=front;ptr!=NULL;ptr=ptr->next)
{ cout<<"\n\tbook no"<<ptr->bno;
cout<<"\n\tbook name"<<ptr->bname;
}
}
void main()
{ clrscr();
int n;
list obj;
while(1)
{ clrscr();
cout<<"\n\t1.Add record";
cout<<"\n\t2.Delete record";
cout<<"\n\t3.Display record";
cout<<"\n\t0.Exit";
cout<<"\n\tInput your choice";
cin>>n;
switch(n)
{ case 1:obj.add();
break;
case 2:obj.del();
break;
case 3:obj.disp();
break;
case 0:exit(0);
break;
default:cout<<"\n\tInvalid choice";
}
getch();

46
}}
OUTPUT:

47

/* 19.Write a menu driven program to delete and insert a node containing books information from circuler queues
of books implemented with the help of the following structure
struct book
{ int bno;
char bname[40];
book*next;
};
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct book
{ int bno;
char bname[40];
book*next;
};
class list
{ book*front,*rear;
public:
list()
{ front=NULL;
rear=NULL;
}
void add();
void del();
void disp();
};
void list::add()
{ book*ptr;
ptr=new book;
cout<<"book no ";
cin>>ptr->bno;
cout<<"book name";
cin>>ptr->bname;
if(front==NULL)
{ front=ptr;
rear=ptr;
rear->next=front;
}
else
{ rear->next=ptr;
ptr->next=front;
rear=ptr;
}
cout<<"one record added";
}

48
void list::del()
{ book*ptr;
if(front==NULL)
{ cout<<"queues is empty";
getch();
return;
}
if(front==rear)
{ ptr=front;
front=NULL;
rear=NULL;
delete ptr;
}
else
{ ptr=front;
front=ptr->next;
rear->next=front;
delete ptr;
}
cout<<"one record deleted";
}
void list::disp()
{ book*ptr;
if(front==NULL)
{ cout<<"queues is empty";
getch();
return;
}
ptr=front;
do
{ cout<<"\n\tbook no"<<ptr->bno;
cout<<"\n\tbook name"<<ptr->bname;
ptr=ptr->next;
}
while(ptr!=front);
}
void main()
{ clrscr();
int n;
list obj;
while(1)
{ clrscr();
cout<<"\n\t1.Add record";
cout<<"\n\t2.Delete record";
cout<<"\n\t3.Display record";
cout<<"\n\t0.Exit";
cout<<"\n\tInput your choice";
cin>>n;
switch(n)
{ case 1:obj.add();

49
break;
case 2:obj.del();
break;
case 3:obj.disp();
break;
case 0:exit(0);
break;
default:cout<<"\n\tInvalid choice";
}
getch();
}
}

50

OUTPUT:

51

/*
20.write a program to count the number of words,consonants,vowles,alphabet and digits from a text fun .txt.
*/
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void main()
{ clrscr();
ifstream r("fun.txt");
char ch;
int v,c,d,wr,al;
v=c=d=wr=al=0;
if(r.bad())
{ cout<<"\n\tFile not Found";
getch();
return;
}
while(!r.eof())
{
r.get(ch);
ch=tolower(ch);
if(isalpha(ch))
{ al++;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
else
c++;
}
else if(isdigit(ch))
{ d++;
}
else if(ch==' ')
wr++;
}
cout<<"\n\tNo of words "<<wr;
cout<<"\n\tNo of consonants "<<c;
cout<<"\n\tNo of alphabets "<<al;
cout<<"\n\tNo of digits "<<d;
getch();
r.close();
}

52

OUTPUT:

53

/*
21.Given a binary file PHONE.DAT containing records of the following structure type
class phonlist
{ private:
char Name[20],address[30],areacode[10],phoneno[10];
public:
void register();
void show();
int checkcode(char AC[])
{ return strcmp(areacode,AC);
}
};
*/
#include<fstream.h>
#include<conio.h>
#include<string.h>
class phonlist
{ private:
char Name[20],address[30],areacode[10],phoneno[10];
public:
void Register();
void show();
int checkcode(char AC[])
{ return strcmp(areacode,AC);
}
};
void phonlist::Register()
{ cout<<"Name";
cin>>Name;
cout<<"Address";
cin>>address;
cout<<"Area Code";
cin>>areacode;
cout<<"Phone no";
cin>>phoneno;
}
void phonlist::show()
{ cout<<"Name"<<Name<<endl;
cout<<"Address"<<address<<endl;
cout<<"Area Code"<<areacode<<endl;
cout<<"Phone no"<<phoneno<<endl;
}

54
void main()
{ clrscr();
phonlist obj;
ifstream r("PHONE.DAT");
ofstream w("PHONEBACK.DAT");
if(r.bad())
{ cout<<"\n\tFile not Found";
getch();
return;
}
r.read((char*)&obj,sizeof(obj));
while(!r.eof())
{ if(obj.checkcode("DEL")==0)
w.write((char*)&obj,sizeof(obj));
r.read((char*)&obj,sizeof(obj));
}
cout<<"\n\tOne File Copied";
getch();
r.close();
w.close();
}

55

OUTPUT:

56

/*22write a menu driven program to insert and delete a given number from a given position in an integer array.
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void create(int arr[],int size)
{ int n;
for(n=0;n<size;n++)
{ cout<<"Input any no.:";
cin>>arr[n];
}
}
void selection(int arr[],int size)
{ int cur,j,temp;
for(cur=0;cur<size;cur++)
{ for(j=cur+1;j<size;j++)
{ if(arr[cur]>arr[j])
{ temp=arr[cur];
arr[cur]=arr[j];
arr[j]=temp;
}
}
}
cout<<"\n\tSorted list\n";
for(j=0;j<size;j++)
cout<<"\t"<<arr[j];
}
void bubble(int arr[],int size)
{ int i,j,temp;
for(i=0;i<size;i++)
{ for(j=0;j<size;j++)
{ if(arr[j]>arr[j+1])
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"\n\tSorted list\n";

57
for(j=0;j<size;j++)
cout<<"\t"<<arr[j];
}
void insertion(int arr[],int size)
{ int i,j,p,temp;
arr[0]=0;
for(i=1;i<size;i++)
{ p=i-1;
temp=arr[i];
while(temp<arr[p])
{ arr[p+1]=arr[p];
p--;
}
arr[p+1]=temp;
}
cout<<"\n\tSorted list\n";
for(j=1;j<size;j++)
cout<<"\t"<<arr[j];
}
void main()
{
clrscr();
int a,n,arr[50];
while(1)
{ clrscr();
cout<<"\n 1.creation of array \n 2.Selection sort \n 3.Bubble sort \n 4.Insertion sort \n 0.Exit\n";
cout<<"\n Enter your choice : ";
cin>>a;
switch(a)
{ case 1:cout<<"Enter number of elements in array want to insert ";
cin>>n;
create(arr,n);
break;
case 2:selection(arr,n);
break;
case 3:bubble(arr,n);
break;
case 4:insertion(arr,n);
break;
case 0:exit(0);
default:cout<<"Invalid choice";
}
}
getch();
}

58

OUTOUT:

59

23. /*write a menu driven program to insert and delete a given number from a given position in an integer array.
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void insert(int arr[],int &size,int index,int item)
{ int n;
for(n=size;n>index;n--)
{ arr[n]=arr[n-1];
}
arr[n]=item;
size++;
}
void disp(int arr[],int &size)
{ int n;
for(n=0;n<size;n++)
cout<<"\t"<<arr[n];
}
void del(int arr[],int &size,int index)
{ int n;
for(n=index;n<size;n++)
arr[n]=arr[n+1];
size--;
}
void main()
{
clrscr();
int a,n,arr[50],item,index,i;
while(1)
{ clrscr();
cout<<"\n 1.Create an array \n 2.Insert in an array \n 3.Delete from an array \n 4.Display array \n
0.Exit\n";
cout<<"\n Enter your choice : ";
cin>>a;
switch(a)
{ case 1:cout<<"How many items you wan to add : ";

60
cin>>n;
for(i=0;i<n;i++)
{ cout<<"Input any no.: ";
cin>>arr[i];
}
break;
case 2:cout<<"Input new item you want to insert :";
cin>>item;
cout<<"Input the no whrer you want to insert :";
cin>>index;
insert(arr,n,item,index);
break;
case 3:cout<<"\n\tInput index no that you want to delete :";
cin>>index;
del(arr,n,index);
break;
case 4:disp(arr,n);
break;
case 0:exit(0);
default:cout<<"Invalid choice";
}
}
getch();
}

61

OUTPUT:

Вам также может понравиться