In: Computer Science
Please explain this code line by line
void printperm(int *A,int n,int rem,int j)
{
if(n==1)
{
for(int k=0;k<j;k++)
cout<<A[k]<<" +
";
cout<<rem<<"\n";
return;
}
for(int i=0;i<=rem;i++)
{
if(i<=rem)
A[j]=i;
printperm(A,n-1,rem-i,j+1);
}
}
/*There int *A is integer type pointer that store the address of array A,
int n int rem or int j are the local variable/parameterized variable */
void printperm(int *A,int n,int rem,int j)
{
if(n==1) //check if n is equal to 1 if true
then execute if block,
{
for(int k=0;k<j;k++)
//execute for loop from k = 0 to less then to
j,
cout<<A[k]<<" +
"; //print the array value with +
symbol,
cout<<rem<<"\n";
//print rem value with new line,
return;
}
for(int i=0;i<=rem;i++) // execute
for loop from I = 0 to less then rem value,
{
if(i<=rem)
//execute if block till when i is less then
rem,
A[j]=i;
//assign i value to array jth index,
printperm(A,n-1,rem-i,j+1); //here function is calling to
itself, this is knowing as recurrsive function,
}
}