plditallo
plditallo

Reputation: 701

Out of range double dimension array

Techies-- This code works when I have a single channel or when I have an even split. It almost works when I have a remainder...in that it will create the next channel and it will pull in the item, but then goes out of range. These lines are the heart of the trouble:

    items_per_batch = batchcount / (int)channels; //
    subsets = batch.Split(items_per_batch); 

items_per_batch is really used to give the Split extension an idea of a general number on how many items to split by. If it sees a remainder, it just creates another subset array. What i really need to do is keep track of the length of items. I tried:

    int idx2 = subset.GetLength(1) 

At one point, but the forloop using that value also went out of range. Anyone have any suggestions?

    static void channelassign()
    {
        int THRESHOLD = 2;
        string[] batch = new string[]
        { "item1", "item2", "item3", "item4","item5","item6","item7" };
        int batchcount = batch.Count();
        int items_per_batch;
        string[][] subsets;
        int idx1;
        int idx2;


        if (THRESHOLD != 0) //avoid accidental division by 0.
        {

            float channels = batchcount / THRESHOLD;
            if (channels < 1)
            {
                channels = 1; // only 1 channel is needed
                items_per_batch = batchcount; // process all items
                idx1 = 1; // fix value to a single channel
                idx2 = (batchcount - 1); // true start of array is 0
                subsets = batch.Split(batchcount); //splits correctly
            }
            else
            {
              // decide how many items will be included per batch
              channels =  (int)Math.Round(channels, 
                  MidpointRounding.ToEven); //determines channel number
              items_per_batch = batchcount / (int)channels; //
              subsets = batch.Split(items_per_batch); 
              idx1 = subsets.GetLength(0); // gets channel# assigned by split
              // idx2 = subsets.GetLength(1); // gets items back from splits

            }

            //distribute contents of batch amongst channels


            for (int channel = 0; channel < idx1; channel++)
            {
                for (int i = 0; i < items_per_batch; i++)
                {
                    Console.WriteLine(" Channel:" + channel.ToString() + " 
                       ItemName: {0} ", subsets[channel][i]);
                }
            }


        }
        else
        {
            Console.WriteLine("Threshold value set to zero. This 
               is an invalid value. Please set THRESHOLD.");
        }

Upvotes: 0

Views: 136

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183858

The division

float channels = batchcount / THRESHOLD;

is performed on ints, so your float channels always has an integer value, equal to

floor(batchcount / THRESHOLD)

But that's not the cause of your problem, that is

for (int channel = 0; channel < idx1; channel++)
{
    for (int i = 0; i < items_per_batch; i++)

that if batchcount is not a multiple of channels, some channels have fewer than items_per_batch items. Thus the inner loop then tries to access a subsets[channel][i] that doesn't exist.

Upvotes: 2

Related Questions