All Ds Solution

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 42

DS 1 : Write an algorithm, draw a flowchart and write a program in to insert an

element in array at given location.


#include<iostream.h>
#include<conio.h>
class insert
// class is defined as integer as per the requirement of user. Insert is the class
name
{
public:
int data[10],i,loc,element; // Data members (variables)
void getdata();
// user defined function
void atlocation();
// user defined functoin
void display();
};
void insert::getdata() // Function definition(what the function will do is defined)
{
clrscr();
cout<<"\n\t\t Enter the element in an array ==>"<<endl; // prints the statement endl ends
the line
for(i=1;i<=5;i++)
// for loop executes five times
{
cout<<"\n\t\t ";
// printing blank line and space
cin>>data[i];
// accepting the value in the array.
}
}
void insert::atlocation()
// function defined
{
cout<<"\n\t\t At which position you want to insert new element ==> ";
cin>>loc;
// accepting location where element is to insert.
cout<<"\n\t\t Which element you want to insert ==> ";
cin>>element;
// accepting the element which is to be insert.
for(i=6;i>loc;i--)
{
data[i]=data[i-1];
// accepting value in the
}
data[i]=element;
}

void insert::display()
{
for(i=1;i<=6;i++)
{
cout<<"\n\t\t "<<data[i];
}
}
int main()
{
insert d;
d.getdata();
d.atlocation();
d.display();
getch();
return 0;
}
/*

OUTPUT==>
Enter the element in an array ==>
10
20
30
40
50
At which position you want to insert new element ==> 3
Which element you want to insert ==> 13
10
20
13
30
40
50
*/

DS 2 : Write an algorithm, draw a flowchart and write a program in C++ to delete an

element in array from given location.


#include<iostream.h>
#include<conio.h>
class deletion
{
public:
int data[10],i,loc,element;
void getdata();
void atlocation();
void display();
};
void deletion::getdata()
{
clrscr();
cout<<"\n\t\t Enter the element in an array ==> \n\n ";
for(i=1;i<=5;i++)
{
cout<<"\n\t\t ";
cin>>data[i];
}
}
void deletion::atlocation()
{
cout<<"\n\t\t From which position you want to delete an element ==> ";
cin>>loc;
for(i=loc;i<=5;i++)
{
data[i]=data[i+1];
}
}
void deletion::display()
{
cout<<"\n\n\t\t After deletion ==>";
for(i=1;i<=4;i++)
{
cout<<"\n\n\t\t "<<data[i];
}
}

main()
{
deletion d;
d.getdata();
d.atlocation();
d.display();
getch();
}
/*

OUTPUT
Enter the element in an array ==>
10
20
30
40
50
From which position you want to delete an element ==> 3
After deletion ==>
10
20
40
50

*/

DS 3 : Write an algorithm, draw a flowchart and write a program in C++ to perform

the push operation on stack using array.


#include<iostream.h>
#include<conio.h>
class stack
{
public:
int stack[10],i,limit,top,element;
void push();
void getdata();
void item();
void display();
};
void stack::push()
{
clrscr();
cout<<"\n\n\t\t Enter the stack limit==>";
cin>>limit;
for(i=1;i<=limit;i++)
{
cout<<"\n\t\t Enter the element==>";
cin>>element;
top=top+1;
cout<<"\n\t\t top==>"<<top<<endl;
stack[top]=element;
}
}
void stack::display()
{
cout<<"\n\n\t\t Stack has values==> \n\n";
for(i=limit;i>=1;i--)
{
cout<<"\n\t\t "<<stack[i];
}
}
main()
{
stack S;
S.push();
S.display();
getch();
}

/*

OUTPUT==>
Enter the stack limit==>6
Enter the element==>10
top==>1
Enter the element==>20
top==>2
Enter the element==>30
top==>3
Enter the element==>40
top==>4
Enter the element==>50
top==>5
Enter the element==>60
top==>6
Stack has values==>
60
50
40
30
20
10

*/

DS 4 : Write an algorithm, draw a flowchart and write a program in C++ to perform

the pop operation on stack using array.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack is over flow";
return;
}
stk[++top]=x;
cout <<"inserted:" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack is under flow";
return;
}
cout <<"deleted:" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)

cout <<stk[i] <<" ";


}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element:";
cin >> ch;
st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4: exit(0);
}
}
return (0);
}
/*
OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice:2
Stack is under flow
1.push 2.pop 3.display 4.exit
Enter ur choice:1
enter the element:2
inserted:2
1.push 2.pop 3.display 4.exit
Enter ur choice:1
enter the element:3

inserted:3
1.push 2.pop 3.display 4.exit
Enter ur choice:2
deleted:3
1.push 2.pop 3.display 4.exit
Enter ur choice:1
enter the element:5
inserted:5
1.push 2.pop 3.display 4.exit
Enter ur choice:3
52
1.push 2.pop 3.display 4.exit
Enter ur choice:4

DS 5 : Write an algorithm, draw a flowchart and write a program in C++ to perform

insertion operation on queue.


#include<iostream.h>
#include<conio.h>
class queue
{
public:
int data[5],i,ele,front,rear,n;
void getdata();
int insert();
void display();
};
void queue::getdata()
{
cout<<" Enter the limit of queue ";
cin>>n;
}
int queue::insert()
{
for(i=1;i<=n+1;i++)
{
if((front==0) && (rear==0))
{
front=1;
rear=1;
}
else
{
if(rear==n)
{
cout<<" \n\n\t\tSorry, Queue is full";
getch();
return(0);
}
else
{
rear=rear+1;
}
}
cout<<" Enter the element ==> ";
cin>>ele;

data[rear]=ele;
}
}
void queue::display()
{
cout<<"\n\n Queue has elements ==>\n\n";
for(i=1;i<=n;i++)
{
cout<<"| "<<data[i];
}
}
main()
{
clrscr();
int i;
queue q;
q.front=0;
q.rear=0;
q.getdata();
q.insert();
q.display();
getch();
}
Output
Enter the limit of queue 4
Enter the element ==> 1
Enter the element ==> 2
Enter the element ==> 3
Enter the element ==> 4
Sorry, Queue is full
Queue has elements ==>
| 1| 2| 3| 4

DS 6 : Write an algorithm, draw a flowchart and write a program in C++ to perform

deletion operation on queue.


#include<iostream.h>
#include<conio.h>
class queue
{
public:
int i,front,rear,data[10],element,N,j;
void getdata();
void deletion();
};
void queue::getdata()
{
cout<<"\n\n\t\t Enter the Limit ==> ";
cin>>N;
cout<<"\n\t\t Insert elements ==\n\n";
for(i=1;i<=N;i++)
{
cout<<"\t\t";
cin>>data[i];
}
}
void queue::deletion()
{
front=0;
rear=N;
for(i=1;i<=N+1;i++)
{
if(front==rear)
{
cout<<" \n\n\t\t Sorry, Queue is empty...";
getch();
break;
}
else
{
if(front==N)
{
front=1;
}

else
{
front=front+1;
cout<<"\n\n Queue Status ==> ";
for(j=front;j<=rear;j++)
{
cout<<" "<<data[j];
}
}
}
}
}
main()
{
clrscr();
queue q;
q.getdata();
q.deletion();
getch();
return(0);
}
/* OUTPUT :Enter the Limit ==> 5
Insert elements ==
41
52
63
78
96
Queue Status ==>

41 52 63 78 96

Queue Status ==>

52 63 78 96

Queue Status ==>

63 78 96

Queue Status ==>

78 96

Queue Status ==>

96

Sorry, Queue is empty... */


/* OUTPUT :Enter the Limit ==> 6
Insert elements ==
12
32
45
65
78
96
Queue Status ==>

12 32 45 65 78 96

Queue Status ==>

32 45 65 78 96

Queue Status ==>

45 65 78 96

Queue Status ==>

65 78 96

Queue Status ==>

78 96

Queue Status ==>

96

Sorry, Queue is empty...


*/

DS 7 : Write an algorithm, draw a flowchart and write a program in C++ to find a

factorial of a given number using recursion.


#include<iostream.h>
#include<conio.h>
class factorial
{
public:
int facto,num;
void getdata();
int fact(int num);
void display();
};
void factorial::getdata()
{
cout<<"Enter the number";
cin>>num;
}
int factorial::fact(int num)
{
if(num==0)
{
return(facto);
}
else
{
facto=facto*num;
num=num-1;
facto= fact(num);
}
return(facto);
}
void factorial::display()
{
cout<<"The factorial of "<<num<<" is ==> "<<facto;
}
main()
{
clrscr();
factorial f;
f.facto=1;
int abc;
f.getdata();

abc= f.fact(f.num);
cout<<" Factorial ==> "<<abc;
//f.display();
getch();
return(0);
}
/* Output :Enter the number 6
The factorial of 6 is 720.
*/

DS 8 : Write an algorithm, draw a flowchart and write a program in C++ to print

multiplication table using recursion.


#include<iostream.h>
#include<conio.h>
class factorial
{
public:
int facto,num;
void getdata();
int fact(int num);
void display();
};
void factorial::getdata()
{
cout<<"Enter the number";
cin>>num;
}
int factorial::fact(int num)
{
if(num==0)
{
return(facto);
}
else
{
facto=facto*num;
num=num-1;
facto= fact(num);
}
return(facto);
}
void factorial::display()
{
cout<<"The factorial of "<<num<<" is ==> "<<facto;
}
main()
{
clrscr();
factorial f;
f.facto=1;

int abc;
f.getdata();
abc= f.fact(f.num);
cout<<" Factorial ==> "<<abc;
//f.display();
getch();
return(0);
}
Output ==>
1

10

10

12

14

16

18

20

12

15

18

21

24

27

30

12

16

20

24

28

32

36

40

10

15

20

25

30

35

40

45

50

12

18

24

30

36

42

48

54

60

14

21

28

35

42

49

56

63

70

16

24

32

40

48

56

64

72

80

18

27

36

45

54

63

72

81

90

10

20

30

40

50

60

70

80

90

100

DS 9 : Write an algorithm, draw a flowchart and write a program in C++ to print

Fibonacci series using recursion.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class fibo
{
public:
int f1,f2,f3,n;
void getdata();
void process(int);
};
void fibo::getdata()
{
cout<<"\nEnter number of terms find fibonacci ==> ";
cin>>n;
f1=0,f2=1;
n=n-2;
cout<<"\n\n"<<f1<<"\t"<<f2;
}
void fibo::process(int n)
{
if (n==0)
{
f3=n;
}
else
{
f3=f1+f2;
cout<<"\t "<<f3;
f1=f2;
f2=f3;
n=n-1;
process(n);
}
}
void main()
{
clrscr();
fibo f;

f.getdata();
f.process(f.n);
getch();
}
/* OUTPUT :Enter number of terms find fibonacci ==> 6
0

OUTPUT :Enter number of terms find fibonacci ==> 3


0

OUTPUT:Enter number of terms find fibonacci ==> 8


0

13

Enter number of terms find fibonacci ==> 5


0
*/

DS 10 : Write an algorithm, draw a flowchart and write a program in C++ for linear

search.
#include<iostream.h>
#include<conio.h>
class linear
{
public:
int data[8],i,element,loc;
void getdata();
void search();
void display();
};
void linear::getdata()
{
clrscr();
cout<<"\n\n\t\tEnter the elements in an array \n";
for(i=1;i<=5;i++)
{
cout<<"\t\t";
cin>>data[i];
}
cout<<"\n\n\t\twhich element you want to search ==> ";
cin>>element;
}
void linear::search()
{
loc=0;
for(i=1;i<=8;i++)
{
if(data[i]==element)
{
loc=i;
break;
}
}
}
void linear::display()
{

if(loc==0)
{
cout<<"\n\n\t\tSorry,"<<element<<" is not present";
}
else
{
cout<<"\n\n\t\t"<<element<<" is present at location "<<loc;
}
}
main()
{
linear l;
l.loc=0;
l.getdata();
l.search();
l.display();
getch();
return 0;
}
/* OUTPUT ==> 1
Enter the elements in an array
11
4
55
22
66
which element you want to search ==> 55
55 is present at location 3

OUTPUT ==> 2
Enter the elements in an array
1
44
11
55
22
which element you want to search ==> 100
Sorry,100 is not present
*/

DS 11 : Write an algorithm, draw a flowchart and write a program in C++ for binary

search.
#include<iostream.h>
#include<conio.h>
class BinSearch
{
public:
int beg,end,mid,i,data[10],loc,elmt;
void getValue();
void calResult();
void showResult();
};
void BinSearch::getValue()
{
cout<<"Enter values in Array==>";
for(i=1;i<=7;i++)
{
cin>>data[i];
}
}
void BinSearch::calResult()
{
beg=1,end=7,mid=0,loc=0;
cout<<"Enter the element u want to search==>";
cin>>elmt;
for(i=1;i<=7;i++)
{
mid = int(beg+end)/2;
if(data[mid]==elmt)
{
loc = mid;
break;
}
else
{
if(data[mid]>elmt)
{
end=mid-1;
}
else
{

beg=mid+1;
}
}
}
}
void BinSearch::showResult()
{
if(loc==0)
{
cout<<" Sorry, Element not found ";
}
else
{
cout<<"Element found at==>"<<loc;
}
}
main()
{
clrscr();
BinSearch bs;
bs.getValue();
bs.calResult();
bs.showResult();
getch();
}
/* OUTPUT :Enter values in Array==>10
20
30
40
50
60
70
Enter the element u want to search==>50
Element found at==>5
*/

DS 1 2: Write an algorithm, draw a flowchart and write a program in C++ for bubble

sort.
#include<iostream.h>
#include<conio.h>}
class BubblSort
{
public:
int data[10],j,i,ptr,limit;
void getValue();
void calSorting();
void showResult();
};
void BubblSort::getValue()
{
cout<<"\n\n\tEnter limit for Array==>";
cin>>limit;
cout<<"\n\n\tEnter values in Array==>";
for(i=1;i<=limit;i++)
{
cout<<"\n\n\t";
cin>>data[i];
}
}
void BubblSort::calSorting()
{
for(j=1;j<=limit-1;j++)
{
ptr=1;
while(ptr<=limit-j)
{
if(data[ptr]>data[ptr+1])
{
int temp = data[ptr];
data[ptr] = data[ptr+1];
data[ptr+1] = temp;
}
ptr = ptr+1;
}
cout<<"\n\n\tPass : "<<j<<" ==> ";
for(i=1;i<=limit;i++)
{

cout<<"\t"<<data[i];
}
}
}
void BubblSort::showResult()
{
cout<<"\n\n\tArray after Sorting....";
cout<<"\n\n\n";
for(i=1;i<=limit;i++)
{
cout<<"\t"<<data[i];
}
}
main()
{
clrscr();
BubblSort bs;
bs.getValue();
bs.calSorting();
bs.showResult();
getch();
}

/* OUTPUT :Enter limit for Array==>5


Enter values in Array==>
11
20
2
45
36
Pass : 1 ==>

11

20

36

45

Pass : 2 ==>

11

20

36

45

Pass : 3 ==>

11

20

36

45

Pass : 4 ==>

11

20

36

45

Array after Sorting....


2
*/

11

20

36

45

DS 13: Write an algorithm, draw a flowchart and write a program in C++ for selection

sort.
#include<iostream.h>
#include<conio.h>
class selection
{
public:
int data[10],k,j,loc,temp,n,min;
void getdata();
void sort();
void minimum();
};
void selection::getdata()
{
cout<<"\t\t Enter the value of N ==> ";
cin>>n;
for(k=1;k<=n;k++)
{
cout<<"\t\t";
cin>>data[k];
}
}
void selection::sort()
{
for(k=1;k<=n-1;k++)
{
minimum();
temp=data[k];
data[k]=data[loc];
data[loc]=temp;
cout<<"\n Pass : "<<k<<" ==> ";
for(j=1;j<=n;j++)
{
cout<<"\t"<<data[j];
}
getch();
}

}
void selection::minimum()
{
min=data[k];
loc=k;
for(j=k+1;j<=n;j++)
{
if(min>data[j])
{
min=data[j];
loc=j;
}
}
}
main()
{
clrscr();
selection s;
s.getdata();
s.sort();
getch();
return(0);
}
/* OUTPUT :Enter the value of N ==> 8
77
33
44
11
88
22
66
55
Pass : 1 ==>
Pass : 2 ==>
Pass : 3 ==>
Pass : 4 ==>
Pass : 5 ==>
Pass : 6 ==>
Pass : 7 ==>

11
11
11
11
11
11
11

33
22
22
22
22
22
22

44
44
33
33
33
33
33

77
77
77
44
44
44
44

88
88
88
88
55
55
55

22
33
44
77
77
66
66

66
66
66
66
66
77
77

55
55
55
55
88
88
88

DS 14 : Write an algorithm, draw a flowchart and write a program in C++ for insertion

Sort.
#include<iostream.h>
#include<conio.h>
class sort
{
public:
int data[10],i,temp,ptr,n,j;
void getdata();
void sorting();
};
void sort::getdata()
{
cout<<"\n\t\t Enter the limit ==> ";
cin>>n;
cout<<"\n\t\t Insert Elements ==> \n\n";
for(i=1;i<=n;i++)
{
cout<<"\t\t";
cin>>data[i];
}
}
void sort::sorting()
{
data[0]=-10; // Insert a smallest value.
for(i=2;i<=n;i++)
{
temp=data[i];
ptr=i-1;
while(temp<data[ptr])
{
data[ptr+1]=data[ptr];
ptr=ptr-1;
}
data[ptr+1]=temp;
cout<<"\n\n Pass : "<<i-1<<"==> ";
for(j=1;j<=n;j++)
{
cout<<" "<<data[j];
}

getch();
}
}
main()
{
clrscr();
sort s;
s.getdata();
s.sorting();
getch();
return(0);
}
/* OUTPUT :Enter the limit ==> 5
Insert Elements ==>
4
5
23
6
1
Pass : 1==>

4 5 23 6 1

Pass : 2==>

4 5 23 6 1

Pass : 3==>

4 5 6 23 1

Pass : 4==>

1 4 5 6 23

*/
/* OUTPUT :Enter the limit ==> 8
Insert Elements ==>

77
33
44
11
88
22
66
55
Pass : 1==>

33 77 44 11 88 22 66 55

Pass : 2==>

33 44 77 11 88 22 66 55

Pass : 3==>

11 33 44 77 88 22 66 55

Pass : 4==>

11 33 44 77 88 22 66 55

Pass : 5==>

11 22 33 44 77 88 66 55

Pass : 6==>

11 22 33 44 66 77 88 55

Pass : 7==>
*/

11 22 33 44 55 66 77 88

DS 15: Write an algorithm, draw a flowchart and write a program in C++ for merge

sort.
#include<iostream.h>
#include<conio.h>
class merge
{
public :
int a[10],b[10],c[10],na,nb,nc,ptr,r,s,i;
void getdata();
void sorting();
void display();
};
void merge::getdata()
{
cout<<"\n\t\t Enter the limit of 1st array ==> ";
cin>>r;
cout<<"\n\t\t Enter 1st array ==> \n\n";
for(i=1;i<=r;i++)
{
cout<<"\t\t";
cin>>a[i];
}
cout<<"\n\t\t Enter the limit of 2nd array ==> ";
cin>>s;
cout<<"\n\t\t Enter 2nd array ==> \n\n";
for(i=1;i<=s;i++)
{
cout<<"\t\t";
cin>>b[i];
}
}
void merge::sorting()
{
na=1;nb=1;ptr=1;
while((na<=r)&&(nb<=s))
{
if(a[na]<b[nb])
{
c[ptr]=a[na];

ptr=ptr+1;
na=na+1;
}
else
{
c[ptr]=b[nb];
ptr=ptr+1;
nb=nb+1;
}
}
if(na>r)
{
for(i=0;i<=s-nb;i++)
{
c[ptr+i]=b[nb+i];
}
}
else
{
for(i=0;i<=r-na;i++)
{
c[ptr+i]=a[na+i];
}
}
}
void merge::display()
{
cout<<"\n\n\t\t Array after merge sort ==> \n\n";
for(i=1;i<=r+s;i++)
{
cout<<"\n\t\t"<<c[i];
}
}
main()
{
clrscr();
merge m;
m.getdata();
m.sorting();
m.display();
getch();
return(0);

}
/* OUTPUT :Enter the limit of 1st array ==> 5
Enter 1st array ==>
1
3
5
7
9
Enter the limit of 2nd array ==> 5
Enter 2nd array ==>
2
4
6
8
10
Array after merge sort ==>
1
2
3
4
5
6
7
8
9
10
*/
/* OUTPUT :Enter the limit of 1st array ==> 5
Enter 1st array ==>

5
10
15
20
25
Enter the limit of 2nd array ==> 5
Enter 2nd array ==>
4
8
16
32
64
Array after merge sort ==>
4
5
8
10
15
16
20
25
32
64
*/

DS 16 : Write an algorithm, draw a flowchart and write a program in C++ for Quick

sort.
#include<process.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class quicks
{
public:
int a[20],top,loc,beg,end,n;
int lower[20],upper[20];
void getdata();
void display();
void quick();
void quicksort();
};
void quicks::getdata()
{
cout<<"\n\t\tEnter number of elements ==> ";
cin>>n;
cout<<"\n\t\tEnter Elements :\n";
for(int i=0;i<n;i++)
{
cout<<"\t\t\t\t";
cin>>a[i];
}
}
void quicks::display()
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
void quicks::quick()
{
int temp,left,right,flag;
left=beg;
right=end;
loc=beg;
flag=0;

while(flag!=1)
{
while(a[loc]<=a[right]&&loc!=right)
{
right--;
}
if(loc==right)
{
flag=1;
return;
}
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while(a[loc]>=a[left]&&loc!=left)
{
left++;
}
if(loc==left)
{
flag=1;
return;
}
if(a[loc]<a[left])
{
temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
}
}
}
void quicks::quicksort()
{
top++;
lower[top]=0;
upper[top]=n-1;
while(top!=-1)

{
beg=lower[top];
end=upper[top];
top--;
quick();
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
}
void main()
{
clrscr();
quicks ob;
ob.top=-1;
ob.getdata();
cout<<"\n\n\t\tBefore sorting ==> ";
ob.display();
cout<<"\n\n\t\tAfter sorting ==> ";
ob.quicksort();
ob.display();
getch();
}

/* OUTPUT :Enter number of elements ==> 5


Enter Elements :
7
5
1
3
69
Before sorting ==> 7 5 1 3 69
After sorting ==> 1 3 5 7 69
OUTPUT:Enter number of elements ==> 7
Enter Elements :
22
33
11
55
44
66
77
Before sorting ==> 22 33 11 55 44 66 77
After sorting ==> 11 22 33 44 55 66 77
OUTPUT:Enter number of elements ==> 8
Enter Elements :
44
66

22
99
101
75
26
15
Before sorting ==> 44 66 22 99 101 75 26 15
After sorting ==> 15 22 26 44 66 75 99 101
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy