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

Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
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;...
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; } }...
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]...
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)|');
Hello sir can you explain me this code in details. void claimProcess() { int id; bool...
Hello sir can you explain me this code in details. void claimProcess() { int id; bool found = false; system("cls"); printf("Enter patient ID for which you want to claim insurrence: "); scanf("%d", & id); int i; for (i = 0; i < patientCount; i++) { if (patients[i].id == id) { found = true; break; } } if (found == false) { printf("subscriber not found\n"); return; } int numOfDaysHospitalized, suppliesCost, surgicalFee, otherCharges; bool ICU; printf("How many days were you haspitalized: ");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT