Hello. I need help to find longest repetitive sequence of numbers in array and put that sequence in new array.For example, I have array[10]={0,1,1,1,1,0,0,0,1,1} so the longest sequence will be 1,1,1,1 and this numer must be in new array. array1[4]={1,1,1,1}.Thanks for your help!
int a[26] = { 0,1,0,1,0,1,0,0,0,0,1,1,1,1,0,0,0,1,0,0,1,1,1,0,0,1 }; int indexmax = 0; int countmax = 0; int main(void) { for (int i = 0; i < 26; i++) { while (dat[i] == 1) i++; int index = i; int count = 0; while(dat[i] == 0) {i++; count++;} if (count > countmax) { countmax = count; indexmax = index; } } }
I write code like this, I get the answer what I need, but when change array size or put more zeores I get bad answer.
For example my array now is size 26. I get the index=6 and longest sequence = 4. But how I say when changing size of array like:
dat[27] = { 0,1,0,1,0,1,0,0,0,0,1,1,1,1,0,0,0,1,0,0,1,1,1,0,0,1,1 };
I get answer that index=27 and countmax=252.
What is the problem?
The first problem is that that code is buggy as heck. For starters, it has too many magic numbers, and way too few buffer index range checks.
But the biggest problem is that your whole algorithm is wrong. You say you want the longest sequence of repeated numbers, but you actually only count sequences of zeroes.
And to top it all off, none of this has anything to do with Keil tools, so you're also asking this in the wrong place.