Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the following C ++ code, what does function P do? please explain it step by s

ID: 3548978 • Letter: I

Question

In the following  C ++ code, what does function P do? please explain it step by step to better understand

#include <iostream>

//Function declarations

void P(int [],int,int);

void Print(int [],int);

void Swap(int [],int,int);

void Rotate_Left(int v[],int,int);

using namespace std;

int main()

{

int N;

cout<<"Please enter 'N'"<<endl;

cout<<">>";

cin>>N;

int v[100];

for (int i=0; i<N; i++)

{

v[i]=i+1;//initialise

}

P(v,0,N);

cin.get();

cin.get();

return 0;

}

/*==================================

Function P

==================================*/

void P(int v[],int start, int n)

{

Print(v,n);

if (start<n)

{

int i,j;

for(i=n-2; i>=start; i--)

{

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

{

Swap(v,i,j);

P(v,i+1,n);

}

Rotate_Left(v,i,n);

}

}

}

2

/*==============================

Function Print

=============================*/

void Print(int v[],int size)

{

if (v!=0)

{

for (int i=0; i<size; i++)

{

cout<<v[i];

}

cout<<""<<endl;

}

}

/*==============================

Function swap

==============================*/

void Swap(int v[],int i,int j)

{

int t;

t = v[i];

v[i] = v[j];

v[j] = t;

}

/*===========================

Function rotate left

===========================*/

void Rotate_Left(int v[],int go,int n)

{

int tmp = v[go];

for (int i=go; i<n-1; i++)

{

v[i] = v[i+1];

}

v[n-1] = tmp;

}

Explanation / Answer

consider array V[9] = [ 0 1 2 3 4 6 7 8 9 ]


N=9


function P is called


void P(int v[],int start, int n) //array V, Start value, total size n(=9) are sent to function.

{

Print(v,n); // function Print is called and the values in V[9] are printed 0"1"2"3"4"6"7"8"9

if (start<n) // if value of start(=1) is less than n

{

int i,j; //i,j are initialized

for(i=n-2; i>=start; i--) // i =7 last but second value of array V

{

for(j=i+1; j<n; j++) // j = 8

{

Swap(v,i,j); // value of i and j are swapped by swap function

P(v,i+1,n); // again function P is called with array V,

}

Rotate_Left(v,i,n); //Rotate_left function is called

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote