Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Department of Software Engineering

Data Structures & Algorithms Lab


Lab # 3

SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1
Give answers to the following.
1.  Write the output of the following program. 

#include<iostream> 
using namespace std; 
int mystery(int,int); 
int main() 

int x=3,y=2; 
cout<<"Result = "<<mystery(x,y); 
return 0; 

int mystery(int a, int b) 

if(b==1) 
return a; 
else 
return a + mystery(a, b-1); 

Output:

Result = 6

In this program, when there is time of execution of function, it will directly move to else
statement. It is because b is not equal to 1 (b! =1). In the else statement it will return 3 +
mystery (3, 2-1) = 3 + mystery (3, 1). So in this, b becomes equal to 1 (b=1) and now if
statement executes which returns 3. 3+3 =6.

2.  Let J and K be integers and suppose Q(J, K) is recursively defined by : 

Q(J,K) = { 5, J < K

Q(J − K, K + 2) + J, J ≥K

Trace and Find Q (5, 3).


Program:
#include<iostream>
using namespace std;
int Q(int J, int K)
{
if (J < K)
{
return 5;
}
if (J >= K)
{
return Q(J - K, K + 2) + J;
}
}
int main()
{
int j = 5, k = 3;
cout << "Result = " << Q(j, k);
system("pause");
return 0;
}

Output:
Result = 10

This program works like this;


Q (5-3, 3+2) + 5;
Q (2,5) + 5; 5 + 5 =10;

3. Let ‘a’ and ‘b’ be integers and suppose Q(a, b) is recursively defined by :

Q(a, b) = { 0, a < b

Q(a − b, b) + 1, b ≤ a

Find Q (14,3).

Program:
#include<iostream>
using namespace std;
int Q(int a, int b)
{
if (a < b)
{
return 0;
}
if (b <= a)
{
return Q(a - b, b) + 1;
}
}
int main()
{
int a = 14, b = 3;
cout << "Result = " << Q(a, b);
system("pause");
return 0;

Output:

Result = 4

Q(11,3) +1; //=4

Q(8,3) +1; //=3

Q(5,3) +1; //=2

Q(2,3) +1; //=1

4. Identify the problem with following recursive function. 


void recurse( int count ) 

 cout<< count <<"\n"; 
 recurse ( count + 1 ); 
}

This recursion function will executes infinitely. It will not stop due missing of base case
(If Else statements). It will run in infinite loop.

5. Given the following function, write the output if the user enters ‘abcz’ as input. 
void rev() 

char c; 
cin>>c;

if(c!='z'){ 
rev(); 
cout<<c; 

Output:

cba

Task 2
Exercise 1:
#include<iostream>
using namespace std;
int sum(int a[], int size)
{
if (size == 0)
{
return 0;
}
else
{
return a[size - 1] + sum(a, size - 1);
}
}
int main()
{
int const size = 5;
int arr[size];
cout << "Enter elements in array: " << endl;
for (int i = 0; i < size; i++)
{
cout << "Element # " << (i + 1) << " is ";
cin >> arr[i];
}
int result;
result = sum(arr, size);
cout << "SUM IS: " << result;
cout << endl;
system("pause");
return 0;

Exercise 2:
#include<iostream>
using namespace std;
int print(int N)
{
if (N == 0)
{
return 0;
}
else
{
cout << N << endl;
return print(N - 1);
}
}
int main()
{
int n;
cout << "Enter integer for recursion: ";
cin >> n;
cout << endl;
print(n);
system("pause");
return 0;
}

Exercise 3:
#include<iostream>
using namespace std;
int Ackermann(int m, int n)
{
if (m == 0)
{
return (n + 1);
}
if (m != 0 && n == 0)
{
return Ackermann(m - 1, 1);
}
if (m != 0 && n != 0)
{
return Ackermann((m - 1), Ackermann(m, n - 1));
}
}
int main ()
{
int M, N;
cout << "Enter 1st integer: ";
cin >> M;
cout << "Enter 2nd integer: ";
cin >> N;
cout << endl;
cout << "Result of Ackermann function is: " << Ackermann(M, N);
cout << endl;
system("pause");
return 0;
}

Exercise 4:
#include<iostream>
using namespace std;
int binomial(int m, int n)
{
if (m == 0)
{
return 1;
}
if (n == 0)
{
return 1;
}
else
{
return binomial(n - 1, m) + binomial(n - 1, m - 1);
}
}
int main()
{
cout << "1st condition binomial(0,4): " << binomial(0, 4);
cout << endl;
cout << "2nd condition binomial(4,0): " << binomial(4, 0);
cout << endl;
cout << "3rd condition binomial(2,4): " << binomial(2, 4);
cout << endl;
system("pause");
return 0;

You might also like