0

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;
7
  • int numArray[5]...; numArray.size() is definitely not C++. Please post code the actual code that you're having trouble with. Commented Oct 23, 2011 at 3:33
  • 1
    The code you posted is nonsensical, as is your comment about ASCII. Please post an actual, complete program that demonstrates your problem. See sscce.org. Commented Oct 23, 2011 at 3:34
  • C++ arrays are not objects like in JavaScript. They don't have a method called size. You'll have to hard code the size of the array or store it in a variable. Commented Oct 23, 2011 at 3:35
  • The code returns true codepad.org/YhLVHmsn Commented Oct 23, 2011 at 3:42
  • 1
    "my array goes from int values to ascii and gets all funky and doesn't work" is not a very useful error description. Commented Oct 23, 2011 at 7:46

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I I hate getting caught up in the easy stuff.
Oh thanks, in reality i never used python before, would be nice to see how is this in python, can u provide it just for the sake of curiosity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.