Home > Software engineering >  How to search through a jagged array
How to search through a jagged array

Time:09-17

This code should search an array of decimals for elements that are in a specified range, and return the number of occurrences of the elements that matches the range criteria.

The problem is that I am having trouble in accessing the jagged array, my code:

public static int GetDecimalsCount(decimal[] arrayToSearch, decimal[][] ranges)
{
    if (arrayToSearch is null)
    {
        throw new ArgumentNullException(nameof(arrayToSearch));
    }
    else if (ranges is null)
    {
        throw new ArgumentNullException(nameof(ranges));
    }
    else
    {
        int sum = 0;
        for (int i = 0; i < arrayToSearch.Length; i  )
        {
            for (int j = 0; j < ranges.Length; j  )
            {
                for (int n = 0; n < ranges[j].Length; n  )
                {
                    if (arrayToSearch[i] >= ranges[j][n] && arrayToSearch[i] <= ranges[j][n   1])
                    {
                        sum  ;
                    }
                }
            }
        }

        return sum;
    }
}

The ranges are from lowest to highest so it will always be arrays of two decimals

I was also confident this at least should work:

if (arrayToSearch[i] >= ranges[j][0] && arrayToSearch[i] <= ranges[j][1])

How does it not compare the array, I don't understand.

EDIT 1: Data in Arrays from Test

Inserting some of the test code, if you need full I can send it. It's a little long and it has unimportant test cases for other assignments.

private static readonly decimal[] ArrayWithFiveElements = { 0.1m, 0.2m, 0.3m, 0.4m, 0.5m };
private static readonly decimal[] ArrayWithFifteenElements = { decimal.MaxValue, -0.1m, -0.2m, decimal.One, -0.3m, -0.4m, -0.5m, decimal.Zero, 0.1m, 0.2m, 0.3m, 0.4m, 0.5m, decimal.MinusOne, decimal.MinValue };

[Test]
public void DecimalCounter_FiveElementsOneRange_ReturnsResult()
{
    // Arrange
    decimal[][] ranges =
    {
        new[] { 0.1m, 0.2m },
    };

    // Act
    int actualResult = DecimalCounter.GetDecimalsCount(DecimalCounterTests.ArrayWithFiveElements, ranges);

    // Assert
    Assert.AreEqual(2, actualResult);
}

[Test]
public void DecimalCounter_FiveElementsTwoRanges_ReturnsResult()
{
    // Arrange
    decimal[][] ranges =
    {
        new[] { 0.1m, 0.2m },
        new[] { 0.4m, 0.5m },
    };

    // Act
    int actualResult = DecimalCounter.GetDecimalsCount(DecimalCounterTests.ArrayWithFiveElements, ranges);

    // Assert
    Assert.AreEqual(4, actualResult);
}

CodePudding user response:

After fixing the errors pointed out in the comments I don't find any problem in the code ; https://dotnetfiddle.net/F6Yjy0

public static int GetDecimalsCount(decimal[] arrayToSearch, decimal[][] ranges)
{
    if (arrayToSearch == null)
    {
        throw new ArgumentNullException(nameof(arrayToSearch));
    }
    else if (ranges == null)
    {
        throw new ArgumentNullException(nameof(ranges));
    }
    else
    {
        int sum = 0;
        for (int i = 0; i < arrayToSearch.Length; i  )
        {
            for (int j = 0; j < ranges.Length; j  )
            {
                //for (int n = 0; n < ranges[j].Length; n  )
                //{
                    if (arrayToSearch[i] >= ranges[j][0] && arrayToSearch[i] <= ranges[j][1])
                    {
                        sum  ;
                    }
                //}
            }
        }

        return sum;
    }
}

The innermost loop is rather pointless; it only runs once and can be replaced with indexing 0/1. Removing it also removes the OOB problem

  • Related