Created
February 22, 2020 16:18
-
-
Save httpsterio/0647dc9c9e173aa49ba362c90434a623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
public class t3_4_miidi | |
{ | |
public static void Main() | |
{ | |
//double[] luvut = {-1, -2, -3, 2, 5, -10}; // keskiarvo == 0.2 | |
//double m1 = Miidi(luvut); // -1 | |
double m2 = Miidi(new double[] { 1 }); // 1 | |
//double m3 = Miidi(new double[] { 3, 3 }); // 3 | |
//double m4 = Miidi(new double[] { }); // 0 | |
//Console.WriteLine(m1); | |
Console.WriteLine(m2); | |
//Console.WriteLine(m3); | |
//Console.WriteLine(m4); | |
} | |
/// <summary> | |
/// Returns the closest array value to the average of all of the values | |
/// </summary> | |
/// <param name="numberArray">Input array</param> | |
/// <returns>Double value from the input array</returns> | |
public static double Miidi(double[] numberArray) | |
{ | |
double[] absoluteValues = new double[numberArray.Length]; | |
// Returns 0 if input array is empty or null | |
if (!(numberArray?.Length > 0)) | |
{ | |
return 0; | |
} | |
// Calculates the average value of the input array | |
double AverageSum() | |
{ | |
double summa = 0; | |
foreach (double arrayValue in numberArray) | |
{ | |
summa += arrayValue; | |
} | |
return (summa / numberArray.Length); | |
} | |
// Returns the absolute valua of the average | |
double AverageAbsolute() | |
{ | |
if (AverageSum() < 0) | |
{ | |
return AverageSum() * -1; | |
} | |
else return AverageSum(); | |
} | |
// Creates an array with the absolute values (if less than zero) | |
for (int i = 0; i < absoluteValues.Length; i++) | |
{ | |
if (numberArray[i] < 0) | |
{ | |
absoluteValues[i] = numberArray[i] * -1; | |
} | |
else | |
{ | |
absoluteValues[i] = numberArray[i]; | |
} | |
} | |
// Variable used to store the shortest distance between avg and array value | |
double shortestDistance = AverageAbsolute(); | |
int shortestArrayIndex = 0; | |
// Method that returns the distance between the average and given array index. | |
double distanceCalc(int index) | |
{ | |
if (AverageSum() - numberArray[index] < 0.01 ) | |
{ | |
return 0; | |
} | |
if (AverageSum() >= 0) | |
{ | |
if (AverageSum() > numberArray[index] && numberArray[index] < 0) | |
{ | |
return AverageAbsolute() + absoluteValues[index]; | |
} | |
else if (AverageSum() > numberArray[index] && numberArray[index] >= 0) | |
{ | |
return AverageSum() - absoluteValues[index]; | |
} | |
else if (AverageSum() < numberArray[index] && numberArray[index] >= 0) | |
{ | |
return absoluteValues[index] - AverageAbsolute(); | |
} | |
else | |
{ | |
Console.WriteLine("UNCAUGHT VALUES IN FIRST IFELSE-HÄSSÄKKÄ"); | |
return 666; | |
} | |
} | |
else | |
{ | |
if (AverageSum() < numberArray[index] && numberArray[index] < 0) | |
{ | |
return AverageAbsolute() - absoluteValues[index]; | |
} | |
else if (AverageSum() > numberArray[index] && numberArray[index] < 0) | |
{ | |
return absoluteValues[index] - AverageAbsolute(); | |
} | |
else if (AverageSum() < numberArray[index] && numberArray[index] >= 0) | |
{ | |
return AverageSum() + absoluteValues[index]; | |
} | |
else | |
{ | |
Console.WriteLine("UNCAUGHT VALUES IN SECOND IFELSE-HÄSSÄKKÄ"); | |
return 666; | |
} | |
} | |
} | |
for (int i = 0; i < numberArray.Length; i++) | |
{ | |
if (distanceCalc(i) < shortestDistance) | |
{ | |
shortestDistance = distanceCalc(i); | |
shortestArrayIndex = i; | |
} | |
} | |
return numberArray[shortestArrayIndex]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment