Im trying to implement in c a function that's called Sniffer which gets two inputs and returns the correspond matrix. First input is an integer binary array values (just zero or one) second input is your searched word ( the word you choose it as argument to your function ) the functionally of the function ( what the function does) : searching for occurrence of the given word within your given binary array. at each occurrence of your word there's always follows 8 bits following it, assume that always the input is correct (it means that there's no possibility two occurrence occur one after the other without at least there's space 8bits (8 binary values)!). then the function must return a matrix(integer matrix) of those followed 8bit for each occurrence of the word sorted corresponded by every row of the matrix. (the functions returns just the first 8bit followed each occurrence of the Searched word) this means: First row has first 8 followed bit on first occurrence of the word. Second row has first 8 followed bit on second occurrence of the word. Third row has first 8 followed bit on third occurrence of the word. Fourth row has first 8 followed bit on fourth occurrence of the word. etc ...
I will elaborate by examples: function structure is Code:
int ** SnifferPattern(int* givenArray , int* SearchedWord);
//it returns the corresponded matrix so used int** ;
#1
- Code: givenArray={1,0,1,0,1,1,1,1,1,1,1,1,0,0}; SearchedWord={1,0,1,0}; so the function returns a matrix(size 1x8 - 8 corresponds to 8followed bit) the first row is {1,1,1,1,1,1,1,1} which it's the first 8 bit followed the word 1010 the matrix here with one row because there's just one occurrence of the SearchedWord in the given array.
example 2: #2
- Code: givenArray={1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1,0,1,0}; SearchedWord={1,1,0,0} so the function returns a matrix the first row is {1,1,1,1,1,1,1,1} which it's the first 8 bit followed the word 1010 for the first occurrence. for the second occurrence we see the word appear , so the second row of the returned matrix(size 2x8) will include the first 8bit followed the word 1010 of the second occurrence. so second row of the matrix is {1,0,1,0,1,0,1,0} so the returned matrix (size 2x8) has two rows (because there's two occurrences of the SearchedWord) which each row corresponds to each occurrence of the SearchedWord.
third example: #3 Code:
- givenArray={1,1,1,0,1,1,1,1,1,1,1,1,0,0}; SearchedWord={0,1,0} so the function returns a matrix zero row (like an array with zero values) size 1x8 (8 columns is corresponded to 8followed bit). there's no occurrence of the SearchedWord within the givenArray so we return a zero matrix. There's no overlap between the occurrences of the searchedWords , so we assume always correctness of input.
I will explain my algorithm (a pleasure if there's another suggestions more compatible to my case) My algorithm is searching for the word at every occurrence and then take at every occurrence the first followed 8bit I take them and store them in a matrix's rows . but it sounds much hard to complete with this.
what I succeeded / tried to implement in C is this:
int ** SnifferPattern(int* s ; int* w)
{
// s is the given array, w is the searched Word , number occurrences
// here 2 , so it should be two prints on the output of the program.
int n;
int a[1000];
int i=0;
int j;
int k = 0;
int l;
int found = 0;
int t = 0;
a[k++] = i;
j = 0;
for (i = 0; i < k; i++)
{
n = a[i] - j;
if (n == (sizeof(w)/sizeof(w[0])))
{
t = 0;
for (l = 0; w[l]; l++)
{
if (s[l + j] == w[l])
{
t++; // Matched a character.
}
}
if (t == (sizeof(w)/sizeof(w[0])))
{
found++; // We found a match!
printf("word occurred at location=%d \n", j); // Pint location
}
}
j = a[i] + 1;
}
}
int main(){
int s[1000] = {1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1};
int w[1000] = {1,0,1};
// s is the given array, w is the searched Word , number occurrences
// here 2 , so it should be two prints on the output of the program.
SnifferPattern(s , w)
//I should print all the matrix's row in the main function .
return 0;
}
Any help please with my program implementation to get correct outputs?
thanks for any assistance
Aucun commentaire:
Enregistrer un commentaire