/* @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 root functionality * @brief root only from positive numbers * @brief testing if length is max 9 significant digits */ public void TestRoot() { // rules: root only from positive numbers MathComponents NewMath = new MathComponents(); // tests to check root functionality decimal?result = NewMath.Root(1, 1); Assert.AreEqual(1, result); result = NewMath.Root(2, 25); Assert.AreEqual(5, result); result = NewMath.Root(3.00m, 125.00m); Assert.AreEqual(5, result); result = NewMath.Root(0.25m, 30); Assert.AreEqual(810000, result); result = NewMath.Root(-4, 16); Assert.AreEqual(0.5m, result); result = NewMath.Root(-0.5m, 8); Assert.AreEqual(0.015625m, result); result = NewMath.Root(0.5m, 10); Assert.AreEqual(100, result); result = NewMath.Root(4, 22667121); Assert.AreEqual(69, result); result = NewMath.Root(0.5m, 12586); Assert.AreEqual(158407396, result); // test undefined values result = NewMath.Root(0, 752041.44221m); Assert.AreEqual(null, result); result = NewMath.Root(4, -125); Assert.AreEqual(null, result); result = NewMath.Root(-3, -12.4m); Assert.AreEqual(null, result); result = NewMath.Root(2.4m, -23.25874m); Assert.AreEqual(null, result); // test length of returning value (max 9 significant digits) result = NewMath.Root(2, 0.3m); Assert.AreEqual(RoundOff(0.54772255m), result); result = NewMath.Root(3, 5); Assert.AreEqual(RoundOff(1.7099759466766m), result); result = NewMath.Root(0.3m, 65); Assert.AreEqual(RoundOff(1104191.8114525m), result); // test error handling result = NewMath.Root(0.5m, 50000); Assert.AreEqual(null, result); result = NewMath.Root(0.2m, 600); Assert.AreEqual(null, result); }