By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,938 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,938 IT Pros & Developers. It's quick & easy.

sorting consecutive values

P: 132
Hi everyone,

I have an array which has 7 values.
I would like to check if they are consecutive.

So this is the array:

Expand|Select|Wrap|Line Numbers
  1. my_array[0] = 2;
  2. my_array[1] = 5;
  3. my_array[2] = 6;
  4. my_array[3] = 7;
  5. my_array[4] = 8;
  6. my_array[5] = 9;
  7. my_array[6] = 13;
So I would like it to count how many consecutive numbers there are. In this case, it's 5 (5, 6, 7, 8, 9).

Anybody has any idea how to accomplish this?
Apr 27 '09 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,639
loop through the array and increase a counter variable each time the value is consecutive to the previous/next one.
Apr 27 '09 #2

Expert Mod 15k+
P: 16,027
Based on your example, you will also need to keep a variable which checks the largest number of consecutive numbers, otherwise you'd only get 1 (the first one).

Alternatively, you could just check for one set of consecutive numbers, so if it's greater than one, it's a set.
Apr 28 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.