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

  1. Computer Programing & Logic Design

    Submit pseudocode and a flowchart for the following programming exercise: Test Average and Grade Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average …
  2. college

    Write a C++ program to fill an array with 6 numbers, and then reverse the values of this array. Note: By using a function with two parameters with the name of the array and the size of this array.
  3. Programming Logic

    I need to create a program that has (at least) two variables, char and num. I need to use nested loops to print char, a character, in the form of the following pattern on as many lines and as many times as specified by num. For example, …
  4. Programming Logic

    I need to create a program that has (at least) two variables, char and num. I need to use nested loops to print char, a character, in the form of the following pattern on as many lines and as many times as specified by num. For example, …
  5. Programming

    I need your help to write a program using visual basic 2010 for the following:- write a program that calculates the average of up to 50 numbers input by the user and stored in an arrey. The program should use a class named Statistics …
  6. Programming

    Here is the question that I need help to write the code for the following program in Visual Basic 2010. I am trying to write the code but it has a run time error. If your could helps me to write the code it would be greatly appreciated. …
  7. computer programming

    write a program that can read 10 interger values into an array and find the total for that array in the program , write 3 fiunctions: a function to read values , a function to find the total of element value and a function to print …
  8. computer programming c++

    write a program that can read 10 interger values into an array and find the total for that array in the program , write 3 fiunctions: a function to read values , a function to find the total of element value and a function to print …
  9. computer science

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should …
  10. programming

    Write a program which allows user to enter 10 student names and scores for Java programming 1 and Java programming 2. Allow user to choose which course they want to enter the record for. Use the loop to ask input from user. The program …

More Similar Questions