Content of Homework Should Start From This Page Only
Content of Homework Should Start From This Page Only
Content of Homework Should Start From This Page Only
Q-1b) Code a problem of your choice with both recursion & iteration version &
report the complexity of both versions
Ans: The code of calculating the factorial using recursion is given as:
Output:
Enter the number= 7
The factorial of 7=5040
}
int fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
fact = fact*i;
return fact;
cout<<"The factorial of"<<n<<"="<<fact;
}
Output:
Enter the number= 5
The factorial of 5=120
Q-2 Write an algorithm to find the height of the tree.
Ans: Algo to find the height of the tree
Count(LEFT.RIGHT,ROOT,DEPTH)
1. if(ROOT=NULL),then
setDEPTH:=0 and return.
2. Call Count(LEFT,RIGHT,LEFT[ROOT],DEPTHL).
3. Call Count(LEFT,RIGHT,RIGHT[ROOT],DEPTHR).
4. if(DEPTHL>DEPTHR), then
set DEPTH:=DEPTHL.
else
set DEPTH:=DEPTHR.
5. return.
s=a[i];
if(++i==6)
return s;
if(s>a[i])
s=a[i];
a[i]=s;
small(a);
}
void main()
{
int arr[6]={14,12,16,8,18,13};
int sm;
sm=small(arr);
cout<<”The smallest element is =”<<sm;
}
b.) Also find Kth Smallest element both by sorting the array and by maintaining
the array as a Heap and extracting root K times.
getch();
}
Output:
Enter how many elements 6
Enter the elements 12 3 5 56 1 9
Elements of the array= 12 3 5 56 1 9
After the sorting array =1 3 5 9 12 56
Q-4 Imagine a grid of 100 horizontal and vertical lines having 100x100 points. A
rectangle is then specified by two points, Upper_Left and Lower_Right. The
rectangle is visualized by the diagonal connecting these two points and lines parallel
to grid axes. Given two arbitrary rectangles specify an algorithm to find rectangle
defined by intersection of the two rectangles.
Ans: The code for this is given as:
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,u,l;
static int n=1;
clrscr();
for(i =0;i<10;i++)
{
for(j=0;j<10;j++)
{
a[i][j] =n;
n++;
}
}
cout<<"The Array ="<<endl;
for(i =0;i<10;i++)
{
for(j=0;j<10;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<"Enter the uper & lower value of edge";
cin>>u>>l;
for(i =u-1;i<l;i++)
{
for(j=0;j<10;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
Output:
The Array =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
Q-5 a.) Implement a recursive algorithm to find all 2N- 1 binary numbers for a given
N.
Ans:
#include<iostream.h>
#include<conio.h>
#include<math.h>
Void main( )
{
Void BinToDec(int num, int base) ;
int n;
I int base = 2;
Cout<<”Enter the Number =”;
Cin>>n;
BinToDec(n, base) ;
}
void BinToDec(int num, int base)
{
if (num > 0)
{
BinToDec(num / base, base);
Cout<<base;
}
}
Ans: #include<iostream.h>
#include<conio.h>
void swap(char *p,char *q){
char c;
c=*p;
*p=*q;
*q=c;
}
void perm(char *a,int m,int n)
{
if(m==n){
for(int i=0;i<=n;i++)
cout<<a[i];
}
else
{
for(int j=n;j<=m;j++)
{
swap(a[j],a[n]);
perm(a,m,n+1);
swap(&a[j],&a[n]);
}
}
}
int main()
{
char *a = "banana";
perm(a,strlen(a)-1,0);
getch();
return 0;
}
Output:
ba b nab aa an nb na nn