# 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[1] 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 controlthcoming algorithm.

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

// Not attributable attributablee that the index of equip A starts from single, not attributable attributable attributable naught.

// And so, don’t controlget the array A in the algorithm is global.

Algorithm DoSomething (n)

1.    if (n = 1)

2.        imimprint the prevalent gratified of the undivided equip A on the hide;

3.    else

4.        control i <– 1 to n do

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

6.             if n is odd

7.                   swap A[1] and A[n];

8.             else

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

10. return;

(a) Introduce preventive outcome of the algorithm where an equip A has “3”and n is 1.

(b) Introduce preventive outcome of the algorithm where an equip A has “3, 5”and n is 2. (Likeness entire the stesp)

(c) Introduce preventive outcome of the algorithm where an equip A has “3, 5, 7”and n is 3. (Likeness entire the stesp)

(d) Based on the outcomes of thr topic (a), (b), and (c), picture the deep point of the algorithm.

2) Unfold the controlthcoming alighting pertinency. To unfold the collection, you feel to correction the backward substitution and likeness the comprised outcomes. After that, you should introduce the spell perplexity of the alighting pertinencys.

M(n) = 2 * M (n-1) // Alighting pertinency

M()) = 3 // Initial condition

## Expert Tally

Dear Student,

here i feel written the C++ program as per the requirement.I feel so inclueded the dilate control ameliorate interpretation.Gladden not attributable attributablee that i feel resolute the three preventive output beneath. And gladden fly the absorbed program by uncommenting single operation and single equip at a spell as per the rate of n. Instruction so absorbed in the jurisdiction.

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

Note: Gladden not attributable attributablee that the beneath program has been tested on ubuntu 16.04 classification and compiled underneathneath g++ compiler. This jurisdiction conquer so effort on other IDE’s and C++ microsoft visual studio specific.

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

Program:

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

#include<iostream>

//namespace declration

using namespace std;

//equip A global initialization

//gladden undilate equip A single by single to consummate the outcome

//int A[1] = {3};

//int A[2] = {3, 5};

int A[3] = {3, 5, 7};

//operation definition

void DoSomething(int n)

{

//variable postulates cast declration

int i, temp;

//here i feel written the algo step as absorbed in the topic

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 rates

temp = A[1];

A[1] = A[n];

A[n] = temp;

}

else

{

//swap rates

temp = A[i];

A[i] = A[n];

A[n] = temp;

}

}

}

}

int deep()

{

//now overcome the operation single by single in each preventive

//DoSomething(1);

//DoSomething(2);

DoSomething(3);

return 0;

}

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

here i feel resolute the three illustration output of the program as a hide shot…

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

Output: (a) preventive outcome of the algorithm where an equip A has “3”and n is 1.

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

Output: (b) preventive outcome of the algorithm where an equip A has “3, 5”and n is 2.

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

Output: preventive outcome of the algorithm where an equip A has “3, 5, 7”and n is 3.

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

The deep point of the absorbed algorithm is:

1: To recognize environing recursive operation overcome

2: How to swap two rates

3: To underneathstand the nested if else condition

4: To recognize environing Moduler programming

5: To underneathstand the concept of equips.

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