fi12
fi12

Reputation: 485

myArray count isn't functioning as expected

The arrayTwelveLEngth variable isn't working as expected. When I placed a breakpoint on the amount = 1; line above, I hovered over arrayTwelve, and found that it was empty with 0 elements. Immediately after, I then hovered about arrayTwelveLength, expecting to see 0, but instead it seems that the arrayTwelveLength had a value of 1876662112. I don't know how it got that value, and I need to solve that problem. What am I doing wrong?

NSMutableArray *redValues = [NSMutableArray array];
NSMutableArray *arrayTwelve = [NSMutableArray array];
__block int counter = 0;
__block NSInteger u;
NSUInteger redValuesLength = [redValues count];
__block int arrayTwelveLength = 0;
__block float diffForAverage, fps, averageTime, bloodSpeed;    
float average;
__block int amount = 1;
__block float totalTwelve, totalThirteen;
__block NSUInteger totalNumberOfFramesInSmallArrays = 0;
__block NSUInteger totalNumberOfFramesNotInSmallArrays;

for (u = (counter + 24); u < (redValuesLength - 24); u++)
    {
        diffForAverage = average - [redValues[u + 1] floatValue];
        float test = [redValues[u] floatValue];
        arrayTwelveLength = [arrayTwelve count];
        if (diffForAverage > -1 && diffForAverage < 1)
        {
            totalTwelve += [redValues[u + 1] floatValue];
            amount++;
            [arrayTwelve addObject:@(test)]; 
            counter++;
        }

        else
        {
            if (arrayTwelveLength >= 8)
            {
                counter++;
                break;
            }

            else
            {
                [arrayTwelve removeAllObjects];
                totalTwelve = [redValues[u + 1] floatValue];
                counter++;
                amount = 1;
            }
        }
    }

    amount = 1; // I added a breakpoint here
    totalThirteen = [redValues[u + 1] floatValue];
    average = totalThirteen / amount;
    if (counter == redValuesLength)
    {
        totalNumberOfFramesNotInSmallArrays = redValuesLength - totalNumberOfFramesInSmallArrays - 25 - (redValuesLength - counter);
        fps = redValuesLength / 30;
        averageTime = totalNumberOfFramesNotInSmallArrays / fps;
        bloodSpeed = 3 / averageTime;

        [_BloodSpeedValue setText:[NSString stringWithFormat:@"%f", bloodSpeed]];
    }

    if (arrayTwelveLength == NULL)
    {
        arrayTwelveLength = 0;
    }

    totalNumberOfFramesInSmallArrays += arrayTwelveLength;

Upvotes: 1

Views: 46

Answers (1)

Mindaugas
Mindaugas

Reputation: 1735

You have problems with unsigned/signed types and with your data set the first for loop should not even enter, because your for loop index variable u (== 24) < (redValuesLength (== 0) - 24) but, because redValuesLength being Unsigned type it wraps around and you get:

(unsigned long)0 - (unsigned long)24 = -24 modulo ULONG_MAX + 1= 18446744073709551592

Also, you are not initialising average before usage.

Upvotes: 1

Related Questions