Need help understanding sample question – ExceptionHandling

Source: http://ift.tt/1J6RebX

Redditors, I need help understanding what needs to be done to create a solution for this sample question. I believe I will have to throw ArgumentExcpetion in each Method, and my first assumption is to use a TryCatch(ArgumentException) but I am still unclear what the question means by for each value or combination? Once an exception occurs (ex. DivideByZero) the program ends, so how am I suppose to evaluate each value / combination? Also I am not sure if I need to setup user input(Console.ReadLine()) to pull in the values for IList<int> or just modify the predefined lists?

The time limit in the sample says 20 minutes

How would you approach this question?

I am not looking for solutions only suggestions.

THANKS!

Question:
The code works correctly for the given example, but users have complained that it doesn't work for some of their cases. The methods must throw ArgumentException for every illegal argument value or combination. Expected arguments are two same-length arrays. Fix the bugs.

For example, SumProduct called with arrays {3, 6} and {4, 2} returns the sum of products 34+62=24 while WeightedMean called with values {3, 6} and weights {4, 2} should return the weighted mean (34+62)/(4+2)=4.

Code Sample:

using System; using System.Linq; using System.Collections.Generic;

public class ArrayMath { public static long SumProduct(IList<int> a, IList<int> b) { int sum = 0; for(int i = 0; i < a.Count; i++) sum += a[i] * b[i];

 return sum; } public static double WeightedMean(IList<int> values, IList<int> weights) { return SumProduct(values, weights) / weights.Sum(); } public static void Main(string[] args) { var a = new int[] { 3, 6 }; var b = new int[] { 4, 2 }; Console.WriteLine(SumProduct(a, b)); Console.WriteLine(WeightedMean(a, b)); } 

}

by xxsiriusxburnxx via /r/csharp

Leave a Reply