Detecting sequence of numbers -
Skiaffo - 11.03.2012
Hello, I need some help with the detection of sequences of numbers.
In my script I have 7 numbers. Now, I need to find a way to make the script understand if there are atleast 5 of those 7 numbers that are in sequence.
For example, if the numbers are:
5 9 7 5 3 6 5, no sequence found.
4 9 3 6 2 5 11, found a sequence of atleast 5 numbers with maximum value of 6.
Thank you.
ps: if it can be useful, the numbers will range only from 2 to 14.
Re: Detecting sequence of numbers -
eesh - 11.03.2012
if you tell me the var you store the numbers in i can write you a code
EDIT:
pawn Код:
new numbers[7]= { 3,6,17,4,8,11,13 };
new count;
for(new i;i< 7;i++)
{
if(numbers[i] > 2 && numbers[i] < 14) count++;//count goes up if number is greater than 2 and less than 14
}
printf("%d",count);
//since 17 is grater than 14, itll print 6.
Re: Detecting sequence of numbers -
Skiaffo - 11.03.2012
You didn't understand what I am asking for, I will try to explain it in a better way.
I have 7 numbers. The values of these 7 numbers will range only from 2 to 14. Now, I need the script to detect if there are atleast 5 of the 7 numbers that are in sequence, and then it has to store the highest number of this sequence.
I will make more examples:
3 5 13 6 9 12 8, script will not detect any sequence.
9 10 8 3 7 14 6, script will detect a sequence of 5 numbers, with the highest number being 10.
8 10 11 4 5 6 7, script will detect a sequence of 5 numbers, with the highest number being 8.
That's what I need my script to do, was I clear enough now?
Re: Detecting sequence of numbers -
RyDeR` - 11.03.2012
Sort all the numbers (QuickSort for bigger arrays recommended):
pawn Код:
new
aiNumbers[] = {
4, 9, 3, 6, 2, 5, 11
}
;
for(new i = 0; i < sizeof(aiNumbers); ++i) {
for(new j = 0; j < i; ++j) {
if(aiNumbers[i] < aiNumbers[j]) {
aiNumbers[i] ^= aiNumbers[j], aiNumbers[j] ^= aiNumbers[i], aiNumbers[i] ^= aiNumbers[j];
}
}
}
Detect the ones with the highest sequences:
pawn Код:
new
iSeq[2] = { 1, 1 },
iMax[2] = { cellmin, cellmin }
;
for(new i = sizeof(aiNumbers) - 1; i > -1; --i) {
if(i) {
if((aiNumbers[i] - aiNumbers[i - 1]) <= 1) {
iSeq[0]++;
iMax[0] = max(iMax[0], aiNumbers[i]);
} else {
if(iSeq[1] < iSeq[0]) {
iSeq[1] = iSeq[0];
iMax[1] = iMax[0];
iSeq[0] = 1;
iMax[0] = cellmin;
}
}
} else {
if(iSeq[1] < iSeq[0]) {
iSeq[1] = iSeq[0];
iMax[1] = iMax[0];
}
}
}
The number of sequences are now stored in
iSeq[1] and the maximum amount in
iMax[1]:
pawn Код:
if(iSeq[1] >= 5) {
printf("Found a sequence of %d numbers with maximum value of %d.", iSeq[1], iMax[1]);
}
And this code will finally print:
Код:
Found a sequence of 5 numbers with maximum value of 6.