For whatever reason, when I run my program I get the 'Index out of range' error for my integer list, despite the fact that I've used code similar to this function and it works fine. I'm not sure what it is I'm missing, could someone help?
Here's the code:
public void Histogram() //Function that creates a histogram
{
List<int> histo = new List<int>();
int histogram;
int a = 1;
int z = 5;
//userNumber is a list containing a set of integers
for ( int x = 0; x < userNumber.Count; x++)
{
histogram = userNumber[x];
if (histogram >= a && histogram <= z)
{
histo[x] += 1; //This is where the error occurs
}
else
{
a += 5;
z += 5;
histo.Add(1);
}
}
a = 1;
z = 5;
for( int h = 0; h < histo.Count; h++)
{
histogram = h;
Console.Write("{0} - {1} ", a, z);
for (int x = 0; x < histogram; x++)
{
Console.Write("*");
}
a += 5;
z += 5;
Console.WriteLine("\n");
}
}