Post a New Question

programming

posted by .

I really need help in this it makes me confuse.I should make a program to find a pattern...so can somebody help me...
Write a function to recognize an pattern within another array. Your function should have the following prototype.


const int *find_pattern_in_int_array(const int *pattern, const int *array);


There are two parameters. The first one is pattern, which is an int pointer that points to the starting address of the pattern. The pattern ends with an int 0. The second parameter is the int array within which you need to locate the pattern. The array also ends with an int 0. If the pattern can be found in array, return the start address of last one within array. Otherwise you just return NULL. Note that you cannot access out of the array bound so you need to pay attention to the length of the pattern and the array, since the length of the array is not necessarily smaller than the length of the pattern.


You only need to write the function. The grading system will use various main programs to test the correctness of you function.
Please submit the function with #include<stdio.h>. DO NOT submit code with int main().


Sample Input/Output 1
Let's suppose you have pattern pointing to {8, 7, 0} and array pointing to {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}.
In this case, you should return a pointer pointing to the second 8.


Sample Input/Output 2
pattern : {1, 2, 1, 0}
array : {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}.
In this case, you should return NULL.

  • programming -

    The confusion may be coming from trying to take in too many things at once.

    At root, all this is asking is for you to write a function to find a sequence like {8, 7} in a sequence like (1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1} - which is essentially the same process as finding "the" in "I thought the problem was harder than it was."

    I'd focus on finding the sequence, get the general approach clear in my head, and then worry about the details and the bureaucracy.

    How do you find a sequence - pattern - within another sequence - array? In general, you can:

    1. Look along array, looking for the first element of pattern (loop through array looking for 8)

    2. When you've found the start of pattern in array, compare subsequent elements in both to see if they are the same.

    3. Keep doing this until you've reached the end of array. The last one you found is the answer, or NULL if none found.

    It's the same logic that's used in strstr(), for example, to find "wo" in "hello, world". They're just using arrays of integers here, which are less familiar than arrays of chars, but really very little else is different.

    Don't get hung up on the const. It just means that the values specified can be read, but not changed.

    You can't include your main() in your project, but if I were doing this, the first thing I'd do would be to write that main() using the first example - something like

    const int *find_pattern_in_int_array(const int *pattern, const int *array);

    int main()
    {
    const int pattern[] = {8, 7, 0};
    const int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    int *return_value;
    return_value = find_pattern_in_int_array(pattern, array);
    }

    to provide me a testbed for the function.

    Does this help any? If you want to provide some code or ask a specific question, I'm sure I or somebody here will be happy to have a look at it.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

More Related Questions

Post a New Question