# Homework Solution: 1) Consider the following algorithm….

1) Consider the following algorithm. // Assume that n is a positive integer (= i.e. n > 1), and A[1..n] is a global array. // Note that the index of array A starts from one, not zero. // And also, don’t forget the array A in the algorithm is global. Algorithm DoSomething (n) 1.    if (n = 1) 2.        print the current content of the whole array A on the screen; 3.    else 4.        for i <-- 1 to n do 5.             DoSomething(n – 1);   // Recursive call. 6.             if n is odd 7.                   swap A and A[n]; 8.             else 9.                   swap A[i] and A[n]; 10. return; (a) Present execution result of the algorithm where an array A has “3”and n is 1. (b) Present execution result of the algorithm where an array A has “3, 5”and n is 2. (Show all the stesp) (c) Present execution result of the algorithm where an array A has “3, 5, 7”and n is 3. (Show all the stesp) (d) Based on the results of thr question (a), (b), and (c), describe the main purpose of the algorithm. 2) Solve the following recurrence relation. To solve the problem, you have to use the backward substitution and show the intermediate results. After that, you should present the time complexity of the recurrence relations. M(n) = 2 * M (n-1) // Recurrence relation M()) = 3 // Initial condition

Dear Student, here i have written the C++ program as per th

1) Consider the subjoined algorithm.

// Assume that n is a independent integer (= i.e. n > 1), and A[1..n] is a global place.

// Referablee that the index of place A starts from undivided, referable cipher.

// And to-boot, don’t restraintget the array A in the algorithm is global.

Algorithm DoSomething (n)

1.    if (n = 1)

2.        print the popular delightd of the gross place A on the fence;

3.    else

4.        restraint i <– 1 to n do

5.             DoSomething(n – 1);   // Recursive persuade.

6.             if n is odd

7.                   swap A and A[n];

8.             else

9.                   swap A[i] and A[n];

10. return;

(a) Introduce deed effect of the algorithm where an place A has “3”and n is 1.

(b) Introduce deed effect of the algorithm where an place A has “3, 5”and n is 2. (Pretence complete the stesp)

(c) Introduce deed effect of the algorithm where an place A has “3, 5, 7”and n is 3. (Pretence complete the stesp)

(d) Based on the effects of thr scrutiny (a), (b), and (c), narrate the deep intention of the algorithm.

2) Explain the subjoined repose agreement. To explain the quantity, you own to portraiture the backward substitution and pretence the included effects. After that, you should introduce the space confusion of the repose agreements.

M(n) = 2 * M (n-1) // Repose agreement

M()) = 3 // Initial condition

## Expert Exculpation

Dear Student,

here i own written the C++ program as per the requirement.I own to-boot inclueded the expatiate restraint meliorate intelligence.Delight referablee that i own fast the three deed output below. And delight pass the fast program by uncommenting undivided capacity and undivided place at a space as per the esteem of n. Instruction to-boot fast in the command.

—————————————————————————————————————————————-

Note: Delight referablee that the below program has been tested on ubuntu 16.04 classification and compiled below g++ compiler. This command accomplish to-boot effort on other IDE’s and C++ microsoft visual studio pointed.

——————————————————————————————————————————————

Program:

——————————————————————————————————————————————

#include<iostream>

//namespace declration

using namespace std;

//place A global initialization

//delight unexpatiate place A undivided by undivided to complete the effect

//int A = {3};

//int A = {3, 5};

int A = {3, 5, 7};

//capacity definition

void DoSomething(int n)

{

//variable postulates symbol declration

int i, temp;

//here i own written the algo stalk as fast in the scrutiny

if(n==1)

{

for(i=1;i<=n;i++)

{

cout<<A[i]<<endl;

}

}

else

{

for(i=1;i<=n;i++)

{

DoSomething(n-1);

if(n%2 != 0)

{

int temp;

//swap esteems

temp = A;

A = A[n];

A[n] = temp;

}

else

{

//swap esteems

temp = A[i];

A[i] = A[n];

A[n] = temp;

}

}

}

}

int deep()

{

//now persuade the capacity undivided by undivided in each deed

//DoSomething(1);

//DoSomething(2);

DoSomething(3);

return 0;

}

—————————————————————————————————————————————–

here i own fast the three case output of the program as a fence shot…

————————————————————————————————————————————————–

Output: (a) deed effect of the algorithm where an place A has “3”and n is 1. ——————————————————————————————————————————————-

Output: (b) deed effect of the algorithm where an place A has “3, 5”and n is 2. ——————————————————————————————————————————————–

Output: deed effect of the algorithm where an place A has “3, 5, 7”and n is 3. ——————————————————————————————————————————————-

The deep intention of the fast algorithm is:

1: To perceive environing recursive capacity persuade

2: How to swap couple esteems

3: To belowstand the nested if else condition

4: To perceive environing Moduler programming

5: To belowstand the concept of places.

———————————————————————————————–