Question

Problem 1 1. Consider the following function (K is the size of array A and L...

Problem 1

1. Consider the following function (K is the size of array A and L is the size of array B)

bool func (int A[], int K, int B[], int L, int start)

{

  1. if (L > K-start)
  2. return false;
  3. for (int i =0; i < L; i++)
  4. {
  5. if(A[start+i] != B[i])
  6. return false
  7. }
  8. return true;

}

  1. What are the input and output variables to this function.
  2. Trace it for the following call:

int F[] = {1, 3, 5, 2, 3, 4, 6}; int S[] = {2, 3, 4}; bool res = funct (F, 7, S, 3, 3);

  1. You could also trace it for res = funct(F, 7, S, 3, 2);
  2. Discover what does the function funct do?

2.

  1. Write a function that accepts two arrays and returns true if the second array is within the first array and returns false if not.
  2. Write a main function where you make a call to your function with appropriate variables of your choice.  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Friend,

As per the HomeworkLib rule, I have answered Q1. I have answered all parts in details.

Please repost other question in separate post.

Please let me know in case of any issue.

Q. What are the input and output variables to this function.

Ans: Input: - two arrays: A and B
           - size of each array, K and L
           - starting point - 'start', this will be used as starting point in array A

Q. Trace it for the following call:
   int F[] = {1, 3, 5, 2, 3, 4, 6}; int S[] = {2, 3, 4};
   bool res = funct (F, 7, S, 3, 3);

Ans:
   Here K = 7, L = 3, start = 3

   hence we have to compare following elements of array A and B:

       A[0 + 3] == B[0] => 2 == 2 => true
       A[1+3] == B[1] => 3 == 3 => true
       A[2+3] == B[2] => 4 == 4 => true

       Clearly all corresponding numbers are equals in A and B, so function 'funct' will return 'true'

Q. You could also trace it for res = funct(F, 7, S, 3, 2);

Ans:
   Here K = 7, L = 3, start = 2
   hence we have to compare following elements of array A and B:

       A[0 + 2] == B[0] => 5 == 2 => false
      
       So, 'funct' will return 'false'

Q.Discover what does the function funct do?
Ans: function 'funct' returns 'true' if all the elements of A[start], A[start+1] to A[start+L-1] are
   equals to corresponding elements of B[0], B[1], ... B[L-1].

   Means, it checks whether B is a subarray of A starting from 'start' or not.

Add a comment
Know the answer?
Add Answer to:
Problem 1 1. Consider the following function (K is the size of array A and L...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( )...

    TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( ) : int findMin ) int findScore (value: int): bool res(): int Write the constructor method. It fills the array with random number from 0-100. Find below the code to get your started 1. public TestScores () Random rand -new Random); for (int i-0 i<SIZE i+) socresi] -rand.nextInt (101); Finding the maximum value in an array. Write the method findMax. It returns the maximum value...

  • Given an array of strings, return true if each string's size is equal or greater than...

    Given an array of strings, return true if each string's size is equal or greater than the one before, otherwise return false. The array will be length 2 or more. Example 1: If the names array contains… “Edwin” “Satish” “Solomon” “Massoud” …then the function call stringsIncreasing (names, 4) returns true. Example 2: If the names array contains… “Janet” “Linda” “Jackie” “Marta” …then the function call stringsIncreasing (names, 4) returns false since string Jackie comes before Marta and has more letters...

  • In class Recursion problems bool anyNegative(int al , int size) This function returns true if there...

    In class Recursion problems bool anyNegative(int al , int size) This function returns true if there are any negative numbers in the array, grity false otherwise. int firstNegative(int al I, int size) This function returns the position of the first negative number in the array & EAO If there are no negative numbers it returns int countNegative(int al I, int size) This function returns the number of negative numbers in the array a. 12 bst Stringsint indexO bout Strings nt...

  • in C++, please show the work step by step. thanks 1.Write pseuodocode for a function that...

    in C++, please show the work step by step. thanks 1.Write pseuodocode for a function that finds the minimum element in an array. 2. Translate your pseudocode from 1 into C++ code. Add a main function that calls the function appropriately. 3.First, wrap your head around what this function does: bool isfact(int n){ bool is=false; int fact=1; for(int i=1; i<n+2; i++){ if(fact==n) is=true; fact=fact*i; } return is; } Using this function, write another function void factarr(int a[], bool fact[], int...

  • using c++ Write a function that returns true if a given integer array is sorted(in ascending...

    using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);

  • c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

    c++ please. Write a recursive method that takes the following parameters: * a sorted array of ints * an int representing the size of the array * an int representing a value to be searched for Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.

  • • bool test(int a, int& b) { bool res = false; if(a > b) { b...

    • bool test(int a, int& b) { bool res = false; if(a > b) { b = a%2; res = true; } return res; } int main() { int x = 45, y = 43; test(x, y); cout << x << " " << y << endl; return 0; } Question: The output of the above code is: ________________ •Write a function that takes an integer as its sole parameter and returns -1, 0 or 1 depending upon whether the...

  • C++ Need the step count for this function. int binarySearch(const int array[], int size, int value)...

    C++ Need the step count for this function. int binarySearch(const int array[], int size, int value) { int first = 0, last = size − 1, middle, position = −1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle − 1; else first = middle + 1; } return position; }

  • The aim of this exercise is to check the presence of a number in an array....

    The aim of this exercise is to check the presence of a number in an array. Specifications: • The items are integers arranged in ascending order. • The array can contain up to 1 million items. • The array is never NULL. Implement the method bool Answer::exists(int ints[], int size, int k) so that it returns true if k belongs to ints, otherwise the method should return false. size contains the size of ints. Important note: Try to save CPU...

  • C programming Write a function that gets a 2-d array of given dimensions of ints. It...

    C programming Write a function that gets a 2-d array of given dimensions of ints. It returns true if the array contains two rows with exactly the same values in the same order, and returns false otherwise, bool contains_equal_rows(int height, int width, const int ar[height] [width]) Examples: On input {{1,2,3,4}, {2,3,4,1}, {1,2,3,4}} it returns true. On input {{1,2,3,4}, {2,3,4,5}, {3,4,5,6}} it returns false. On input {{1,1,1,1}, {2,2,2,2}, {1,1,1,6}} it returns false.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT