Nicholas Ramsay
Nicholas Ramsay

Reputation: 475

Index was outside the bounds of the array - For loop

NOTE: This is not a duplicate, similar questions by name and subject do not explain this.

I am trying to sort an array of numbers into numerical order while also collapsing any numbers that increment by one from the previous, so 921,922,923 becomes 921-923.

The code that I wrote was:

static string FormatReceiptNumbers(int[] numbers)
{
    int[] sortedNumbers = numbers;
    Array.Sort(sortedNumbers);
    string output = "";

    //Sort through each element
    for(int x = 0; x < sortedNumbers.Length; x++)
    {
            //Check for Incrementing value
            if(x > 0 & sortedNumbers[x] == sortedNumbers[x - 1] - 1)
            {
                //Check if incrementing value doesn't keep going
                for(int y = x; y < sortedNumbers.Length; y++)
                {
                    //check if incrementing values end
                    if(sortedNumbers[y] != sortedNumbers[y - 1] - 1)
                    {
                        output = output + " - " + sortedNumbers[y];
                        //Check if at end of array
                        if(y != sortedNumbers.Length)
                        {
                            x = y; //Keep going
                            break;
                        }
                        else
                        {
                            output.Remove(0, 2);
                            return output;
                        }
                    }
                }
            }
            //if no incrementing value is found add number to input
            else
            {
                output = output + ", " + sortedNumbers[x].ToString();
            }
    }

    //Each addition to the string adds ", " to beginning, remove the first one, and return value
    return output.Remove(0, 2);
}

This is what I had to use:

The function should turn this:

892, 893, 894, 895, 906, 920, 845

To This:

845, 892 - 895, 906, 910

This is how it was executed just for extra information:

    public static void Main(string[] args)
    {
        int[] input = {892, 893, 894, 895, 906, 845};
        Console.WriteLine(FormatReceiptNumbers(input));
        Console.ReadLine();
    }

However, upon execution, it returns this:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Rextester.Program.FormatReceiptNumbers(Int32[] numbers)
   at Rextester.Program.Main(String[] args)

NOTE: I'm using an online compiler which explains the Rextester I am pretty certain this is not the cause of the issue.

If someone could help me understand the cause of the issue I would greatly appreciate it. I don't see how x (or y?) can go out of bounds as I have set the limit to the array length. Thanks in advance!

Upvotes: 1

Views: 1875

Answers (3)

Novice Programmer
Novice Programmer

Reputation: 41

In case you need it. :)

static void Main(string[] args)
{
    int[] input = { 892, 893, 894, 895, 906, 920, 845 };
    string output = FormatReceiptNumber(input);
    Console.WriteLine(output);
    Console.ReadKey();
}

private static string FormatReceiptNumber(int[] numbers)
{
    Array.Sort(numbers);
    string output = "";
    for (int loop = 0; loop < numbers.Length - 1; loop++)
    {
        int? start = null, end = null;
        while (numbers[loop + 1] - numbers[loop] == 1)
        {
            if (start == null)
            {
                start = numbers[loop];
            }
            end = numbers[loop + 1];
            loop++;
        }
        if (start != null && end != null)
        {
            output += start + "-" + end + ", ";
        }
        else
        {
            output += numbers[loop].ToString() + ", ";
        }
    }
    output += numbers[numbers.Length - 1].ToString();
    return output;
}

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29026

It was because of the condition that you are using(if(x > 0 & sortedNumbers[x] == sortedNumbers[x - 1] - 1)). Even though both & and && are used for comparison, The & in comparison will always evaluates the second condition as well, where as && will not evaluates the second condition if first condition evaluates to false.

it should be like this:

 if (x > 0 && sortedNumbers[x] == sortedNumbers[x - 1] - 1)

Upvotes: 1

Ian
Ian

Reputation: 30813

The problem is with this line:

if (x > 0 & sortedNumbers[x] == sortedNumbers[x - 1] - 1)

You should use double and signs && (that is logical and) instead of single and sign & (which is binary and)

When x = 0, you have sortedNumbers[-1], making the array have out-of bounds index -> this is still executed and not sort circuited if you use & instead of &&.

Upvotes: 1

Related Questions