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; }}
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements one by one for (i = 0; i < c; i++) { // Checking if the picked element is already printed for (j = 0; j <= i; j++) { // If current element is already there in the array, break from j loop if (arr[i] == arr[j]) { break; } } // If it is not printed earlier and is within 10-100, then...
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; }
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int...
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int arr[M][N]){ int i, j; for(int i =0; i < M; i++){ for(int j=0; j<N; j++){ printf(" %d ", arr[i][j]);//print all the array elements } printf("\n"); } } void populateRandom2D(int arr[M][N]){ int i, j; int min, max; printf("Enter min number in the array:\n"); scanf("%d", &min); printf("Enter max number in the array:\n"); scanf("%d", &max); for(int i =0; i < M; i++){ for(int j=0; j< N; j++){...
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;...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n";...
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n"; FILE * fp; fp = fopen("patients.txt", "r"); char c; if (fp == NULL) { printf("file not found"); return; } while (fp != NULL) { if (fgets(line, MAX_LENGTH - 1, fp) == NULL) break; if (line[1] == '\0') break; patients[patientCount].id = atoi(strtok(line, delimiter)); strcpy(patients[patientCount].name, strtok(NULL, delimiter)); patients[patientCount].age = atoi(strtok(NULL, delimiter)); patients[patientCount].annualClaim = strtok(NULL, delimiter); patients[patientCount].plan = atoi(strtok(NULL, delimiter)); strcpy(patients[patientCount].contactNum, strtok(NULL, delimiter)); strcpy(patients[patientCount].address, strtok(NULL, delimiter)); strcpy(line,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT