c# - End of method reached but compiler jumps back in -


just fun trying change making problem - more or less. problem doubled results , through using debugger found out compiler jumps method again when should finish.

private static void main(string[] args) { string temp = console.readline(); int input;  if (int.tryparse(temp, out input)) {     if(input == 0)     {         system.environment.exit(environment.exitcode);     }     else     {         changemaking(input);     } } else {     console.writeline("not number."); } }  private static int changemakinghelper(int input, int euro) {     return input / euro; }  static int[] euro = { 1, 2, 5, 10, 20, 50, 100, 200, 500 }; static int counter = 0; static list<int[]> result = new list<int[]>(); static int[] tempresult = new int[euro.length];  private static void changemaking(int input) {     (int = euro.length -1; >= 0; i--)     {         if(euro[i] <= input)         {             tempresult[i] = changemakinghelper(input, euro[i]);             input = input - euro[i];             if((input % euro[i] != 0))             {                 changemaking(input % euro[i]);             }         }     }     result.add(tempresult); } 

for example if input 11 after for-loop completes , adding tempresult result compiler jumps part:

            if((input % euro[i] != 0))             {                 changemaking(input % euro[i]);             } 

my expected behavior input 11 1 array values {1, 0, 0, 1, 0, 0, 0, 0, 0} doubled.

the issue using static variables pass data between function calls. don't that. use return values instead.

public static void main() {     var result = changemaking(11);     console.writeline(string.join(", ", result)); }  private static int changemakinghelper(int input, int euro) {     return input / euro; }  static readonly int[] euro = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };  private static int[] changemaking(int input) {     var result = new int[euro.length];     (int = euro.length -1; >= 0; i--)     {         if (euro[i] <= input)         {             result[i] += changemakinghelper(input, euro[i]);             input = input - euro[i];             if (input % euro[i] != 0)             {                 var tempresult = changemaking(input % euro[i]);                 // transfer results local result array                 for(int j = 0; j < result.length; j++)                     result[j] += tempresult[j];             }             // add break statement here don't add lower values twice!             break;         }     }     return result; } 

fiddle: https://dotnetfiddle.net/7wnlwn

by way, reason ended 2 arrays in output because called changemaking recursively, got called twice, called result.add(tempresult) twice. getting rid of static variables have shown above fixes that.

also, there's more efficient way without recursion, require restructuring algorithm bit, i'll leave figure out :)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -