-3

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");
    }

}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

4 Answers4

4

Probably this line throws the exception:

histo[x] += 1;

Your list hist doesn't have any integers in it or you are trying to access an index higher than there are items in the list. Because of that the exception is thrown....

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • 1
    it also might be because there are elements but they are less than X. whenever the if condition evaluates to true you are trying to access an element that is not exists – Selman Genç Oct 07 '14 at 12:35
  • @Selman22 yes but in the particular case this is not true he should be sure that histo list have same number of elements like userNumber.Count! Whatever – mybirthname Oct 07 '14 at 12:37
  • @ThatOneCanadian no problem, also like I said in previous comment. Make sure that you have same number of elements in both lists ! – mybirthname Oct 07 '14 at 12:41
  • 1
    what I'm trying to say this can happen on the second or third iteration as well. we can't know that. but your answer assumes it happens in the first iteration which is not %100 correct – Selman Genç Oct 07 '14 at 12:41
2

The problem is that you have not initialized histo properly for use with indexes: it has zero elements, so you cannot index it with x.

Initialize your list with one element right away, like this,

List<int> histo = new List<int> { 0 };

and replace

histo[x] += 1

with

histo[histogram / 5] += 1

to fix this problem.

I added division by five, because it appears that you are incrementing the bounds by 5 every time that you encounter a number that does not fit in your histogram.

You should also remove z += 5; from the code that builds the histogram: you should be expanding the range, rather than moving it up by 5.

An even better approach would be to size your histo upfront, and skip modifying a and z in the first loop altogether:

List<int> histo = new List<int>(Enumerable.Range(0, userNumber.Max() / 5));
foreach (var h in userNumber) {
    histo[h/5]++;
}
// Print your histogram in the usual way
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The reason of the error is that you're trying to read from an empty list:

  // histo doesn't have any items at all: histo.Count == 0 
  List<int> histo = new List<int>();

  ...

  // You're trying to read a non existing item (histo doesn't have any items)
  histo[x] += 1;

possible amendment:

  // histo has userNumber.Count items which are equal to 0
  List<int> histo = new int[userNumber.Count].ToList();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Suppose that the firsta userNumber falls into this code:

if (histogram >= a && histogram <= z)
{
    histo[x] += 1; //This is where the error occurs
}

You never added a number into histo so it will throw Index Out of Range, because histo is empty.

To avoid this you have to be sure to Add anything before using such accessors.

Luizgrs
  • 4,765
  • 1
  • 22
  • 28