Question

10 pts Question 2 Let A[1.n] and B[1.m] be two arrays, where n 2 mz 1. Use pseudocode to write an algorithm which prints TRUE
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

  1. Ask the user to enter elements into the first array
  2. Ask the user to enter elements into the second array
  3. Calculate the size of the first array
  4. Calculate the size of the second array
  5. Check whether the second array is greater than the first array, if Yes then return false.
  6. Take a counter j and initialize it to zero
  7. Run a loop till statement 11: for (int i = 0; (i < array1_size) && (j != array2_size) ; i++)

  8. Check if (array1[i] == array2[j]) then increment the value of j else Initialize the value of j to 0. // if elements of first array matches with the second

  9. Check if (i > array1_size - array2_size) then break

  10. Check if (j == array2_size) the return true else return false.

Add a comment
Answer #2

Algorithm for the given condition can be stated as:

Function checkArray(Parameter 1 as First array,Parameter 2 as second array,Parameter 3 as size of first array (let n),Parameter 4 as size of second array (let m))

Two pointers to traverse the arrays(i and j)

Traverse both arrays simultaneously

WHILE  i < n AND j < m

IF element matches increment both pointers(i++ && j++)

IF array B is completely traversed( j == m)

RETURN TRUE

ENDIF

ENDIF

ELSE

increment i and reset j(i++ && j= 0 )

ENDELSE

ENDWHILE

RETURN FALSE

ENDFunction

This algorithm Time complexity is O(n)

Here is a implementation of above algorithm in a proper function for further references:-

bool checkArray(int A[], int B[], int n, int m)

{

    // Two pointers to traverse the arrays

    int i = 0, j = 0;

  

    // Traverse both arrays simultaneously

    while (i < n && j < m) {

  

        // If element matches

        // increment both pointers

        if (A[i] == B[j]) {

  

            i++;

            j++;

  

            // If array B is completely

            // traversed

            if (j == m)

                return true;

        }

        // If not,

        // increment i and reset j

        else {

            i++;

            j = 0;

        }

    }

  

    return false;

}

Add a comment
Know the answer?
Add Answer to:
10 pts Question 2 Let A[1.n] and B[1.m] be two arrays, where n 2 mz 1....
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
  • Question3 10 pts Let A [L.n] be a max-heap with n > 1 and consider the index ị such that l 〈 i 〈 n . Assume that a...

    Question3 10 pts Let A [L.n] be a max-heap with n > 1 and consider the index ị such that l 〈 i 〈 n . Assume that all the elements of A are distinct. Write the pseudocode of an algorithm which replaces A [i] by A i 100 and then re-arranges the elements of A into a max-heap. The running time of your algorithm must be O (log, n Upload Choose a File Question3 10 pts Let A [L.n]...

  • n 10 The following set of jobs must be processed ed serially through a two-step system....

    n 10 The following set of jobs must be processed ed serially through a two-step system. The times at out of each process are in hours. If Johnson's Rule is used to sequence the jobs then Job A would complete processing on operation 2 at Job Process Process 2 A 12 9 B 11 C 7 6 D 10 14 E 5 8 Select one: A. hour 38. LO A 12 8 11 C 7 6 D 10 14 E...

  • A program that averages two arrays of N elements where N is a whole number. A...

    A program that averages two arrays of N elements where N is a whole number. A process of temperature control has two temperature sensors A and B. From each sensor N samples are taken and They store in a different arrangement. It is necessary to average the values ​​of the two sensors in another arrangement and print it on the screen. The voltage range of the sensors ranges from 0 to 5 volts (0 0C to 100 0C) Make with...

  • 8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za...

    8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...

  • Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] ·...

    Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....

  • 4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of...

    4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of adjacent repeated values. write a Java class (RunsPrinter. java) with one main method that generates a sequence of 20 random die tosses in an array (and not an ArrayList) and then prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 124 3 (2 2 2 2) 3 6 (5 5) 6 3 1 Examples...

  • Question 1 please 1. True or false: (15 pts) {(-1)" tan (TC/2-3/n} is oscillating. (b) 1/2-1/4+1/6-1/8+1/10-........

    Question 1 please 1. True or false: (15 pts) {(-1)" tan (TC/2-3/n} is oscillating. (b) 1/2-1/4+1/6-1/8+1/10-..... converges conditionally. A convergent sequence is always Cauchy. {1/n) is a Cauchy sequence. (1-3)-(1-31/2)+(1-313)-(1-314 )+.....diverges. 2. Find limit sup and limit inf of the following sequences: (10 pts) (a){c+4) sin ng (b) {(1+m+)"} Limsup= limsup= Lmitinf= liminf= 3. Prove that either the following sequence has a limit or not. (20 pts) (a) 2n (b) n2+4n+2 n+6vn n-1

  • 1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark...

    1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark 1729 is O(1) b) 3 marks 2n2-4n -3 is O(n2) 2. [3 marks] Using the definition of Big-Oh, prove that 2n2(n 1) is not O(n2) 3. 6 marks Let f(n),g(n), h(n) be complexity functions. Using the definition of Big-Oh, prove the following two claims a) 3 marks Let k be a positive real constant and f(n) is O(g(n)), then k f(n) is O(g(n)) b)...

  • Write program(Only in C, no C++) as follow. With comment Algorithm 1 MaxSubSeqDivideNConquer(S[..r] Input: A sequence,...

    Write program(Only in C, no C++) as follow. With comment Algorithm 1 MaxSubSeqDivideNConquer(S[..r] Input: A sequence, S of r - 1 integers Output: The sum, start and end of a maximum contiguous subsequence S[start..end 1: if ( r then (S 0) then if 2: return 0, -1, 0 3: else 4: return 5: end if 6: 7: else mid (r/2 (таzSum,mid, start, end) (таzSummid+1,r, start,end,) (marSuffix, left.Index) (maxPrefia, rightIndea) 8: MaxSubSeq DivideNConquer (S[l, mid) MarSubSeq DivideNConquer (S[mid1, r) marSuffix (S[l,...

  • All of question 2 please 1. True or false: (15 pts) {(-1)" tan (TC/2-3/n} is oscillating....

    All of question 2 please 1. True or false: (15 pts) {(-1)" tan (TC/2-3/n} is oscillating. (b) 1/2-1/4+1/6-1/8+1/10-..... converges conditionally. A convergent sequence is always Cauchy. {1/n) is a Cauchy sequence. (1-3)-(1-31/2)+(1-313)-(1-314 )+.....diverges. 2. Find limit sup and limit inf of the following sequences: (10 pts) (a){c+4) sin ng (b) {(1+m+)"} Limsup= limsup= Lmitinf= liminf= 3. Prove that either the following sequence has a limit or not. (20 pts) (a) 2n (b) n2+4n+2 n+6vn n-1

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