So, I am trying to make a console program that analyzes data(well kind of). The problem is when I go to print the median it tells me it is unassigned(well the debugger says this actually). What did I do wrong?
using System;
using System;
class midpointFormula
{
static void Main()
{
double min, max, range, median, quartile1, quartile3, internalQuartileRange, IQR15;
int dataPoints, dataPoints1, count, count1;
double[] dataSet;
bool isEven;
count = 0;
Console.WriteLine("This program allows you to find several diffrent values from the data");
Console.Write("Please enter the amount of data points you would like to use: ");
dataPoints = int.Parse(Console.ReadLine());
dataPoints1 = dataPoints - 1;
dataSet = new double[dataPoints];
#region Enter Data
while (count < dataPoints)
{
count1 = count + 1;
Console.Write("Please enter the {0} datapoint: ", count1);
dataSet[count] = double.Parse(Console.ReadLine());
++count;
}
#endregion
min = dataSet[0];
max = dataSet[dataSet.Length - 1];
range = max - min;
#region Even Check
if (dataPoints % 2 == 0)
{
isEven = true;
}
else
{
isEven = false;
}
#endregion
if (isEven == true)
{
int medianPoint;
medianPoint = (dataPoints / 2) - 1;
median = dataSet[medianPoint];
}
else
#region Output
Console.WriteLine("Minimum is : {0}", min);
Console.WriteLine("Maximum is : {0}", max);
Console.WriteLine("Range is : {0}", range);
Console.WriteLine("Median is : {0}", median);
#endregion
Console.ReadKey();
}
}
Note: Yes, I know if there is an uneven amount of datapoints it will not calculate the data points, I am still working on that.
Because
medianis only assigned a value ifisEvenis true. If it is not, it will be unitialized. Since it is never acceptable to use a variable that even might be unassigned, the compiler prohibits it.P.S. It’s not the debugger that is reporting this error, it is the compiler.
P.P.S. What is going on with your else statement? — based on indent it looks like an incomplete thought and an obvious bug. (
Console.WriteLine("Minimum is : {0}", min);will only run ifisEvenis false, but the rest of those statements will run since they’re not enclosed by braces.)