Professional Documents
Culture Documents
DS Lab 02 (2020-Bse-051)
DS Lab 02 (2020-Bse-051)
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.
Q(J,K) = { 5, J < K
Q(J − K, K + 2) + J, J ≥K
Output:
Result = 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
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;