Lexicon
Lexicon

Reputation: 2627

Comparing to items in an Array c++

alright guys, this should be an easy one...

I have an int array and I want to see if the the numbers in the array are in succession.

For some reason when I do this (below) my array goes from int values to ascii and gets all funky and doesn't work. Any suggestions would be greatly appreciated. In this exampleit should return true.

int numArray[5] = {1,2,3,4,5};

for( int i = 0 ; i < 4 ; i++ )
{

    if ( numArray[i] == numArray[i+1] - 1 )
    {
        continue;
    }
    else
    {
        return false;
    }
}
return true;

Upvotes: 0

Views: 181

Answers (1)

Salvatore Previti
Salvatore Previti

Reputation: 9050

I would write something like this, a little simpler to read.

bool issequential(const int* array, int size)
{
    for (int i = 1; i < size; ++i)
        if (array[i - 1] + 1 != array[i])
            return false;
    return true;
}

Upvotes: 3

Related Questions