c++ - Shouldn't this recursive method end as soon as you get to the return? -


i know why output is:

a-b-b-b-b-0 

when think should a-1.

shouldn't recursive method end return? , why doesn't here?

i put letters '-' clarify returns being met not stopping there.

#include <stdio.h>             #include <iomanip> #include <iostream>  using namespace std;  void printv(int mask[], int elements[], int n) {   int i;   printf("{ ");   (i = 0; < n; i++)     if (mask[i])       printf("%d ", elements[i]);   printf("}"); }  int next(int mask[], int size) {   int i;   (i = 0; (i < size) && mask[i]; i++)     mask[i] = 0;    if (i < size) {     mask[i] = 1;     return 1;   }   return 0; }  void nsubsets(int mask[], int elements[], int size, int n) {   int sum = 0;   int temp[10], count = 0;    (int = 0; < 10; i++) //this must here     temp[i] = 0;    (int = 0; < size; i++)   {     if (mask[i])     {       count++;        (int k = 0; k < 44; k++)         if (temp[k] == 0)         {           temp[k] = elements[i];           sum += elements[i];           break;         }     }   }    if (sum == n)   {     cout << "{ ";     (int l = 0; l < count; l++)       cout << temp[l] << " ";     cout << "}";   } }  int isemptyset(int mask[], int elements[], int size, int n, int sizerecursion) {   int sum = 0;   int temp[10], count = 0;    (int = 0; < 10; i++) //this must here     temp[i] = 0;    (int = 0; < size; i++)   {     if (mask[i])     {       count++;        (int k = 0; k < 44; k++)         if (temp[k] == 0)         {           temp[k] = elements[i];           sum += elements[i];           break;         }     }   }    if (sum == n)   {     cout << "a-";      return 1;   }    sizerecursion--;    if (sizerecursion > 0)   {     next(mask, size);     isemptyset(mask, elements, size, n, sizerecursion);   }   cout << "b-";   return 0; }  int main() {   int n, size = 10;   int elements[size];     size = 6; n = 5;   elements[0] = 5;   elements[1] = 2;   elements[2] = 3;   elements[3] = 2;   elements[4] = 1;   elements[5] = 1;    int mask[10];   int i;   (i = 0; < size; ++i)     mask[i] = 0;    cout << "subsets of elements: ";   printv(mask, elements, size); //this prints first subset   while (next(mask, size))     printv(mask, elements, size);    n = 3;    cout << "\nsubsets equal " << n << "\n";    while (next(mask, size))     nsubsets(mask, elements, size, n);    cout << "\n" << isemptyset(mask, elements, size, n, size);    return 0; } 

shouldn't recursive method end return? , why doesn't here?

no, way works in general when function call returns, returns that function call, , immediate caller may continue execution. doesn't matter whether function recursive or not, each function call separate , each call needs hit return statement @ point (unless return type void).

when have code

  ...    if(sizerecursion > 0)   {     next(mask,size);     isemptyset(mask, elements, size, n,sizerecursion);      }      cout<<"b-";   return 0; } 

what's going happen is, recursive call isemptyset returns, going go right cout << "b-"; line , return 0;. if don't want should put in else block, , maybe modify line calls isemptyset returns value returned call.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -