/* @brief Loads numbers from input, accumulates their sum, sum of their squares and their count; calculated standard deviation and prints it */ private static int Main(string[] args) { MathComponents math = new MathComponents(); bool conversionSuccessful; string input; decimal square = 0; decimal linearAccumulator = 0; decimal quadraticAccumulator = 0; int count = 0; while (!string.IsNullOrEmpty(input = Console.ReadLine())) { conversionSuccessful = Decimal.TryParse(input, out decimal currentNumber); if (!conversionSuccessful) { Console.WriteLine("Conversion error"); return(-1); } count++; linearAccumulator = CheckNull(math.Add(linearAccumulator, currentNumber)); square = CheckNull(math.Exponentiate(currentNumber, 2)); quadraticAccumulator = CheckNull(math.Add(quadraticAccumulator, square)); } if (count < 2) { Console.WriteLine("Minimum of 2 numbers required"); return(-1); } decimal average = CheckNull(math.Divide(1, count)); average = CheckNull(math.Multiply(average, linearAccumulator)); decimal deviationLeft = CheckNull(math.Subtract(count, 1)); deviationLeft = CheckNull(math.Divide(1, deviationLeft)); decimal deviationRight = CheckNull(math.Exponentiate(average, 2)); deviationRight = CheckNull(math.Multiply(count, deviationRight)); deviationRight = CheckNull(math.Subtract(quadraticAccumulator, deviationRight)); decimal deviation = CheckNull(math.Multiply(deviationLeft, deviationRight)); deviation = CheckNull(math.Root(2, deviation)); /* DEBUG INFO * Console.WriteLine("N: " + count); * Console.WriteLine("sum x: " + linearAccumulator); * Console.WriteLine("sum x^2: " + quadraticAccumulator); * Console.WriteLine("average x: " + average); * // END INFO */ Console.WriteLine(deviation); //Console.ReadLine(); return(0); }
/** * @brief Tests to check addition functionality * @brief testing if length is max 9 significant digits */ public void TestAddition() { // no mathematical rules for addition MathComponents NewMath = new MathComponents(); // tests to check addition functionality decimal?result = NewMath.Add(1, 1); Assert.AreEqual(2, result); result = NewMath.Add(0, 0); Assert.AreEqual(0, result); result = NewMath.Add(1.00m, 1.00m); Assert.AreEqual(2, result); result = NewMath.Add(-1, -1); Assert.AreEqual(-2, result); result = NewMath.Add(0.7m, 0.3m); Assert.AreEqual(1, result); result = NewMath.Add(7.65m, -2.65m); Assert.AreEqual(5, result); // test length of returning value (max 9 significant digits) result = NewMath.Add(0.58647m, 5.78952411m); Assert.AreEqual(RoundOff(6.37599411m), result); result = NewMath.Add(-111547546, 111547546); Assert.AreEqual(0, result); result = NewMath.Add(5687412.85m, -85456297); Assert.AreEqual(RoundOff(-79768884.15m), result); result = NewMath.Add(0.25541577m, 5.74589362m); Assert.AreEqual(RoundOff(6.00130939m), result); result = NewMath.Add(123456789, 0.98765432m); Assert.AreEqual(RoundOff(123456789.98765432m), result); // test error handling result = NewMath.Add(-555555555, -555555555); Assert.AreEqual(null, result); result = NewMath.Add(900000000, 900000000); Assert.AreEqual(null, result); }