Question

In: Computer Science

Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...

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);
   }
}

Solutions

Expert Solution

/*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,
   }
}


Related Solutions

int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
Explain the code below in details. void accountInfo() { system("cls"); int ch; printf("1-Total Amount Claimed by...
Explain the code below in details. void accountInfo() { system("cls"); int ch; printf("1-Total Amount Claimed by LifeTime Claim Limit subscriber\n"); printf("2-Total number of Annual Claim Limit who have exhausted all their eligible amount\n"); scanf("%d", & ch); if (ch == 1) { int totalAmountClaimedByLifeTimeSubs = 0; for (int i = 0; i < patientCount; i++) { if (patients[i].annualClaim == false) { for (int j = 0; j < patientCount; j++) { if (claims[j].id == patients[i].id) { totalAmountClaimedByLifeTimeSubs += claims[j].amountClaimed; } }...
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30)...
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30) + 21; int s = 0; while(n>1){ if(n & 1){ n = (n<<2) - n + 1; } else { n = n >> 1; } if(s == t && m == t){ return; } s++; } failure("Seems you forgatz the essence of Collatz"); } Hash value: 2002296385 what input values should n, m have in order for the function phase05 to work?
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
What is the value of n after this code runs? 1 Point int n = 0;...
What is the value of n after this code runs? 1 Point int n = 0; int j = 7; if ((j!=0) && (n < 25)) { n = 1; if (j > 4) { n = 2; } else { n = 3; } } else { n = 4; if (j%2 >= 2) { n = 5; } else { n = 6; } }
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT