I am using the same code in my previous post but now I am trying to debug errors like
System.ArgumentNullException: 'Value cannot be null. (Parameter 'values')'
when I do
static void Main(string[] arg){
int[] numbers = new int[] { 2, 4 };
Console.WriteLine(String.Join(",", HowSum(7, numbers)));
}
How can I fix this when HowSum() returns a NULL?
Here is the original post for reference:
class HowSumSlow {
static int[] HowSum(int targetSum, int[] numbers)
{
int[] empty = new int[] { };
if (targetSum == 0) return empty;
if (targetSum < 0) return null;
foreach( var num in numbers){
var remainder = targetSum - num;
int[] remainderResult = HowSum(remainder, numbers);
if (remainderResult != null){
return remainderResult.Append(num).ToArray();
}
}
return null;
}
static void Main(string[] arg) {
int[] numbers = new int[] { 2, 3, 5 };
Console.WriteLine(String.Join(",", HowSum(8, numbers)));
}
}
int[] result = HowSum(8, numbers); Console.WriteLine(result is null ? "null" : String.Join(",", result));HowSum(7, 2, 3, 5)? Do you expect{2, 2, 2}, with a remainder, or{2, 2, 3}?HowSum(7, [2,3,5])would only show{2,2,3}, instead of both {2,2,3} and {2,5}{2, 2, 3}would have been correct from my options anyway.